From 1c8e19b83f7cc5cbb54fc9f3c5e5227007f7d871 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Sun, 22 Mar 2026 14:43:54 +0100 Subject: [PATCH] feat(server): initialize loop --- cmd/main.go | 8 +++-- core/commands/commands.go | 2 -- core/handler/command_handler.go | 1 - core/handlers/doc.go | 2 ++ core/handlers/init.go | 47 ++++++++++++++++++++++++++ core/server.go | 60 ++++++++++++++++++++++++++++----- persistence/db_migrator.go | 6 ++-- 7 files changed, 109 insertions(+), 17 deletions(-) delete mode 100644 core/commands/commands.go delete mode 100644 core/handler/command_handler.go create mode 100644 core/handlers/doc.go create mode 100644 core/handlers/init.go diff --git a/cmd/main.go b/cmd/main.go index 5b14fba..4615708 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,12 +5,14 @@ import ( "log" "path/filepath" - migrate "github.com/KristianJBorgwarth/dendrite.daemon/persistence" + "github.com/KristianJBorgwarth/dendrite.daemon/persistence" + "github.com/KristianJBorgwarth/dendrite.daemon/core" _ "modernc.org/sqlite" ) func main() { - + server := core.NewServer() + server.Run() } func runMigration() { @@ -24,7 +26,7 @@ func runMigration() { migrationsDir := filepath.Join("persistence", "migrations") - if err := migrate.Run(db, migrationsDir); err != nil { + if err := persistence.ApplyMigrations(db, migrationsDir); err != nil { log.Fatal(err) } diff --git a/core/commands/commands.go b/core/commands/commands.go deleted file mode 100644 index 87d254d..0000000 --- a/core/commands/commands.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package commands provides the commands interface for the application. -package commands diff --git a/core/handler/command_handler.go b/core/handler/command_handler.go deleted file mode 100644 index 9015f58..0000000 --- a/core/handler/command_handler.go +++ /dev/null @@ -1 +0,0 @@ -package comms diff --git a/core/handlers/doc.go b/core/handlers/doc.go new file mode 100644 index 0000000..083c4b4 --- /dev/null +++ b/core/handlers/doc.go @@ -0,0 +1,2 @@ +// Package handlers contains commands for handling incoming requests +package handlers diff --git a/core/handlers/init.go b/core/handlers/init.go new file mode 100644 index 0000000..dfa2fde --- /dev/null +++ b/core/handlers/init.go @@ -0,0 +1,47 @@ +package handlers + +import ( + "encoding/json" + + "github.com/KristianJBorgwarth/dendrite.daemon/config" +) + +type initializeParams struct { + VaultPath string `json:"vaultPath"` + TemplateDir string `json:"templateDir"` + + ScratchNote struct { + Dir string `json:"dir"` + TemplateName string `json:"templateName"` + } `json:"scratchNote"` + + DailyNote struct { + Dir string `json:"dir"` + TemplateName string `json:"templateName"` + FilenameFormat string `json:"filenameFormat"` + } `json:"dailyNote"` +} + +func Initialize(raw json.RawMessage) (*config.Config, error) { + var params initializeParams + if err := json.Unmarshal(raw, ¶ms); err != nil { + return nil, err + } + + cfg := &config.Config{ + VaultPath: params.VaultPath, + TemplateDir: params.TemplateDir, + ScratchNote: config.ScratchConfig{ + Dir: params.ScratchNote.Dir, + TemplateName: params.ScratchNote.TemplateName, + }, + DailyNote: config.DailyConfig{ + Dir: params.DailyNote.Dir, + TemplateName: params.DailyNote.TemplateName, + FilenameFormat: params.DailyNote.FilenameFormat, + }, + } + cfg.SetDefaults() + + return cfg, nil +} diff --git a/core/server.go b/core/server.go index 76495f9..03a65ea 100644 --- a/core/server.go +++ b/core/server.go @@ -1,5 +1,5 @@ -// Package server contains core functionality for dendrite server -package server +// Package core contains core functionality for dendrite server +package core import ( "bufio" @@ -10,13 +10,17 @@ import ( "os" "strconv" "strings" + + "github.com/KristianJBorgwarth/dendrite.daemon/config" + "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers" "github.com/KristianJBorgwarth/dendrite.daemon/core/models" ) type Server struct { - in *bufio.Reader - out io.Writer - log *slog.Logger + in *bufio.Reader + out io.Writer + log *slog.Logger + config *config.Config } func NewServer() *Server { @@ -27,6 +31,31 @@ func NewServer() *Server { } } +func (s *Server) writeResponse(id int, result any, err error) { + resp := types.Response{ + Jsonrpc: "2.0", + ID: id, + } + + if err != nil { + resp.Error = map[string]any{ + "code": -1, + "message": err.Error(), + } + } else { + resp.Result = result + } + + data, marshalErr := json.Marshal(resp) + if marshalErr != nil { + s.log.Error("failed to marshal response", "error", marshalErr) + return + } + + fmt.Fprintf(s.out, "Content-Length: %d\r\n\r\n", len(data)) + s.out.Write(data) +} + // Run starts the server loop, continuously reading and processing incoming messages func (s *Server) Run() error { for { @@ -70,7 +99,7 @@ func (s *Server) readContentLength() (int, error) { break } - if after, ok :=strings.CutPrefix(line, "Content-Length:"); ok { + if after, ok := strings.CutPrefix(line, "Content-Length:"); ok { val := strings.TrimSpace(after) return strconv.Atoi(val) } @@ -80,9 +109,24 @@ func (s *Server) readContentLength() (int, error) { func (s *Server) handleRequest(req types.Request) { s.log.Info("handling request", "method", req.Method, "id", req.ID) + + var ( + result any + err error + ) + + switch req.Method { + case "initialize": + s.config, err = handlers.Initialize(req.Params) + result = map[string]any{"status": "ok"} + default: + err = fmt.Errorf("unknown method: %s", req.Method) + + } + + s.writeResponse(req.ID, result, err) } func (s *Server) handleNotification(notif types.Notification) { s.log.Info("handling notification", "method", notif.Method) -} - +} diff --git a/persistence/db_migrator.go b/persistence/db_migrator.go index 867d049..438dc43 100644 --- a/persistence/db_migrator.go +++ b/persistence/db_migrator.go @@ -1,5 +1,5 @@ -// Package migrate handles database schema migrations -package migrate +// Package persistence handles database schema migrations +package persistence import ( "database/sql" @@ -8,7 +8,7 @@ import ( "sort" ) -func Run(db *sql.DB, dir string) error { +func ApplyMigrations(db *sql.DB, dir string) error { _, err := db.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY);`) if err != nil { return err