Merge pull request #11 from KristianJBorgwarth/fix/db-context-load
fix: dbcontext load issue and remove logging from stdout
This commit is contained in:
commit
f600752d87
8 changed files with 25 additions and 23 deletions
|
|
@ -5,11 +5,13 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/logging"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/server"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/server"
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
logging.Init()
|
||||||
server := server.NewServer()
|
server := server.NewServer()
|
||||||
|
|
||||||
server.RegisterHandler("initialize", handlers.NewInitializeHandler())
|
server.RegisterHandler("initialize", handlers.NewInitializeHandler())
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,17 @@ package frontmatter
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FrontMatter struct {
|
type FrontMatter struct {
|
||||||
Title string `yaml:"Title"`
|
Title string `yaml:"title"`
|
||||||
Tags []string `yaml:"Tags"`
|
Tags []string `yaml:"tags"`
|
||||||
Created string `yaml:"Created"`
|
Created string `yaml:"created"`
|
||||||
Updated string `yaml:"Updated"`
|
Updated string `yaml:"updated"`
|
||||||
Author string `yaml:"Author"`
|
Author string `yaml:"author"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseTags(file []byte) ([]string, error) {
|
func ParseTags(file []byte) ([]string, error) {
|
||||||
|
|
@ -19,6 +21,7 @@ func ParseTags(file []byte) ([]string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
slog.Debug("parsed front matter", "fm", fm)
|
||||||
return fm.Tags, nil
|
return fm.Tags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,6 +36,7 @@ func parseFrontMatter(file []byte) (*FrontMatter, error) {
|
||||||
if idx := bytes.Index(content, []byte("---")); idx != -1 {
|
if idx := bytes.Index(content, []byte("---")); idx != -1 {
|
||||||
content = bytes.TrimSpace(content[:idx])
|
content = bytes.TrimSpace(content[:idx])
|
||||||
}
|
}
|
||||||
|
slog.Debug("yaml content to parse", "content", string(content))
|
||||||
|
|
||||||
var fm FrontMatter
|
var fm FrontMatter
|
||||||
if err := yaml.Unmarshal(content, &fm); err != nil {
|
if err := yaml.Unmarshal(content, &fm); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
||||||
|
|
@ -15,7 +16,6 @@ type createNoteCommand struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
TemplatePath string `json:"templatePath"`
|
TemplatePath string `json:"templatePath"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Vars map[string]string `json:"vars"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateNoteHandler struct{
|
type CreateNoteHandler struct{
|
||||||
|
|
@ -40,11 +40,15 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("rendered template", "data", string(data))
|
||||||
|
|
||||||
tags, err := frontmatter.ParseTags(data)
|
tags, err := frontmatter.ParseTags(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("parsed tags", "tags", tags)
|
||||||
|
|
||||||
tx, err := h.uow.Begin()
|
tx, err := h.uow.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -60,6 +64,8 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("Creating tags", "tags", tags)
|
||||||
|
|
||||||
if err = tagRepo.Upsert(ctx, tagModels); err != nil {
|
if err = tagRepo.Upsert(ctx, tagModels); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
@ -13,9 +12,7 @@ func Init() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
writer := io.MultiWriter(os.Stdout, logFile)
|
handler := slog.NewTextHandler(logFile, &slog.HandlerOptions{
|
||||||
|
|
||||||
handler := slog.NewTextHandler(writer, &slog.HandlerOptions{
|
|
||||||
Level: slog.LevelDebug,
|
Level: slog.LevelDebug,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
BIN
dendrite
BIN
dendrite
Binary file not shown.
|
|
@ -21,11 +21,11 @@ func InitializeDBContext(vaultPath string) (error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDBContext() (*DBContext, error) {
|
func GetDBContext() (*DBContext) {
|
||||||
if dbContext == nil {
|
if dbContext == nil {
|
||||||
panic("DBContext is not initialized. Call InitializeDbContext first.")
|
panic("DBContext is not initialized. Call InitializeDbContext first.")
|
||||||
}
|
}
|
||||||
return dbContext, nil
|
return dbContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func CloseDBContext() error {
|
func CloseDBContext() error {
|
||||||
|
|
|
||||||
|
|
@ -8,21 +8,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type UnitOfWork struct {
|
type UnitOfWork struct {
|
||||||
dbContext *sql.DB
|
|
||||||
Transaction *sql.Tx
|
Transaction *sql.Tx
|
||||||
FileStore *store.FileStore
|
FileStore *store.FileStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUnitOfWork() *UnitOfWork {
|
func NewUnitOfWork() *UnitOfWork {
|
||||||
db, err := persistence.GetDBContext()
|
return &UnitOfWork{FileStore: store.NewFileStore()}
|
||||||
if err != nil {
|
|
||||||
panic("failed to get DB context: " + err.Error())
|
|
||||||
}
|
|
||||||
return &UnitOfWork{dbContext: db.DB, FileStore: store.NewFileStore()}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UnitOfWork) Begin() (tx *sql.Tx, err error) {
|
func (u *UnitOfWork) Begin() (tx *sql.Tx, err error) {
|
||||||
tx, err = u.dbContext.Begin()
|
dbContext := persistence.GetDBContext()
|
||||||
|
tx, err = dbContext.DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,7 @@ func NewDBFixture() *DBFixture {
|
||||||
|
|
||||||
dbPath := filepath.Join(os.TempDir(), ".index", "index.db")
|
dbPath := filepath.Join(os.TempDir(), ".index", "index.db")
|
||||||
|
|
||||||
dbContext, err := persistence.GetDBContext()
|
dbContext := persistence.GetDBContext()
|
||||||
if err != nil {
|
|
||||||
panic("failed to get DB context: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
return &DBFixture{
|
return &DBFixture{
|
||||||
DB: dbContext.DB,
|
DB: dbContext.DB,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue