diff --git a/cmd/main.go b/cmd/main.go index 7648822..8839763 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,11 +5,13 @@ import ( "os" "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers" + "github.com/KristianJBorgwarth/dendrite.daemon/core/logging" "github.com/KristianJBorgwarth/dendrite.daemon/core/server" _ "modernc.org/sqlite" ) func main() { + logging.Init() server := server.NewServer() server.RegisterHandler("initialize", handlers.NewInitializeHandler()) diff --git a/core/frontmatter/parser.go b/core/frontmatter/parser.go index fe32e6b..9bcb523 100644 --- a/core/frontmatter/parser.go +++ b/core/frontmatter/parser.go @@ -3,15 +3,17 @@ package frontmatter import ( "bytes" "errors" + "log/slog" + "gopkg.in/yaml.v3" ) type FrontMatter struct { - Title string `yaml:"Title"` - Tags []string `yaml:"Tags"` - Created string `yaml:"Created"` - Updated string `yaml:"Updated"` - Author string `yaml:"Author"` + Title string `yaml:"title"` + Tags []string `yaml:"tags"` + Created string `yaml:"created"` + Updated string `yaml:"updated"` + Author string `yaml:"author"` } func ParseTags(file []byte) ([]string, error) { @@ -19,6 +21,7 @@ func ParseTags(file []byte) ([]string, error) { if err != nil { return nil, err } + slog.Debug("parsed front matter", "fm", fm) return fm.Tags, nil } @@ -33,6 +36,7 @@ func parseFrontMatter(file []byte) (*FrontMatter, error) { if idx := bytes.Index(content, []byte("---")); idx != -1 { content = bytes.TrimSpace(content[:idx]) } + slog.Debug("yaml content to parse", "content", string(content)) var fm FrontMatter if err := yaml.Unmarshal(content, &fm); err != nil { diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 43a2199..10cd3cd 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -3,6 +3,7 @@ package handlers import ( "context" "encoding/json" + "log/slog" "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" "github.com/KristianJBorgwarth/dendrite.daemon/core/models" @@ -15,7 +16,6 @@ type createNoteCommand struct { Title string `json:"title"` TemplatePath string `json:"templatePath"` Path string `json:"path"` - Vars map[string]string `json:"vars"` } type CreateNoteHandler struct{ @@ -40,11 +40,15 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } + slog.Debug("rendered template", "data", string(data)) + tags, err := frontmatter.ParseTags(data) if err != nil { return nil, err } + slog.Debug("parsed tags", "tags", tags) + tx, err := h.uow.Begin() if err != nil { return nil, err @@ -60,6 +64,8 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } + slog.Debug("Creating tags", "tags", tags) + if err = tagRepo.Upsert(ctx, tagModels); err != nil { return nil, err } diff --git a/core/logging/logger.go b/core/logging/logger.go index c8ce991..a26e39c 100644 --- a/core/logging/logger.go +++ b/core/logging/logger.go @@ -2,7 +2,6 @@ package logging import ( - "io" "log/slog" "os" ) @@ -13,9 +12,7 @@ func Init() { panic(err) } - writer := io.MultiWriter(os.Stdout, logFile) - - handler := slog.NewTextHandler(writer, &slog.HandlerOptions{ + handler := slog.NewTextHandler(logFile, &slog.HandlerOptions{ Level: slog.LevelDebug, }) diff --git a/dendrite b/dendrite index 8eda9ac..ec9a22a 100755 Binary files a/dendrite and b/dendrite differ diff --git a/persistence/db_context.go b/persistence/db_context.go index 4195b0a..2798960 100644 --- a/persistence/db_context.go +++ b/persistence/db_context.go @@ -21,11 +21,11 @@ func InitializeDBContext(vaultPath string) (error) { return nil } -func GetDBContext() (*DBContext, error) { +func GetDBContext() (*DBContext) { if dbContext == nil { panic("DBContext is not initialized. Call InitializeDbContext first.") } - return dbContext, nil + return dbContext } func CloseDBContext() error { diff --git a/persistence/repositories/uow.go b/persistence/repositories/uow.go index 3f0abe5..2966427 100644 --- a/persistence/repositories/uow.go +++ b/persistence/repositories/uow.go @@ -8,21 +8,17 @@ import ( ) type UnitOfWork struct { - dbContext *sql.DB Transaction *sql.Tx FileStore *store.FileStore } func NewUnitOfWork() *UnitOfWork { - db, err := persistence.GetDBContext() - if err != nil { - panic("failed to get DB context: " + err.Error()) - } - return &UnitOfWork{dbContext: db.DB, FileStore: store.NewFileStore()} + return &UnitOfWork{FileStore: store.NewFileStore()} } 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 { return nil, err } diff --git a/test/test_integration/main_test.go b/test/test_integration/main_test.go index 8551caf..94fd8dd 100644 --- a/test/test_integration/main_test.go +++ b/test/test_integration/main_test.go @@ -29,10 +29,7 @@ func NewDBFixture() *DBFixture { dbPath := filepath.Join(os.TempDir(), ".index", "index.db") - dbContext, err := persistence.GetDBContext() - if err != nil { - panic("failed to get DB context: " + err.Error()) - } + dbContext := persistence.GetDBContext() return &DBFixture{ DB: dbContext.DB,