diff --git a/config/config.go b/config/config.go index e69de29..f5db6eb 100644 --- a/config/config.go +++ b/config/config.go @@ -0,0 +1,46 @@ +// Package config provides configuration management for the application, allowing for easy loading and parsing of configuration files, environment variables, and command-line arguments. It supports various formats such as JSON, YAML, and TOML, and provides a unified interface for accessing configuration values throughout the application. +package config + +type ScratchConfig struct { + Dir string + TemplateName string +} + +type DailyConfig struct { + Dir string + TemplateName string + FilenameFormat string +} + +type Config struct { + VaultPath string + TemplateDir string + ScratchNote ScratchConfig + DailyNote DailyConfig +} + +func (c *Config) SetDefaults() { + if c.TemplateDir == "" { + c.TemplateDir = "templates" + } + + if c.ScratchNote.Dir == "" { + c.ScratchNote.Dir = "scratch" + } + + if c.ScratchNote.TemplateName == "" { + c.ScratchNote.TemplateName = "scratch.md" + } + + if c.DailyNote.Dir == "daily" { + c.DailyNote.Dir = "daily" + } + + if c.DailyNote.TemplateName == "" { + c.DailyNote.TemplateName = "daily.md" + } + + if c.DailyNote.FilenameFormat == "" { + c.DailyNote.FilenameFormat = "2006-01-02.md" + } +} diff --git a/core/commands/commands.go b/core/commands/commands.go new file mode 100644 index 0000000..87d254d --- /dev/null +++ b/core/commands/commands.go @@ -0,0 +1,2 @@ +// Package commands provides the commands interface for the application. +package commands diff --git a/core/comms/command_handler.go b/core/comms/command_handler.go deleted file mode 100644 index e69de29..0000000 diff --git a/core/handler/command_handler.go b/core/handler/command_handler.go new file mode 100644 index 0000000..9015f58 --- /dev/null +++ b/core/handler/command_handler.go @@ -0,0 +1 @@ +package comms diff --git a/core/models/types.go b/core/models/types.go index 559aa0e..149a10e 100644 --- a/core/models/types.go +++ b/core/models/types.go @@ -1,4 +1,4 @@ -// Package types contains relevant types for ipc +// Package types types contains relevant types for ipc package types import "encoding/json" diff --git a/core/server.go b/core/server.go index 22285a6..76495f9 100644 --- a/core/server.go +++ b/core/server.go @@ -3,9 +3,14 @@ package server import ( "bufio" + "encoding/json" + "fmt" "io" "log/slog" "os" + "strconv" + "strings" + "github.com/KristianJBorgwarth/dendrite.daemon/core/models" ) type Server struct { @@ -21,3 +26,63 @@ func NewServer() *Server { log: slog.Default(), } } + +// Run starts the server loop, continuously reading and processing incoming messages +func (s *Server) Run() error { + for { + length, err := s.readContentLength() + if err != nil { + return err + } + + body := make([]byte, length) + if _, err := io.ReadFull(s.in, body); err != nil { + return err + } + + s.log.Debug("received", "body", string(body)) + + // Try request first + var req types.Request + if err := json.Unmarshal(body, &req); err == nil && req.Method != "" { + s.handleRequest(req) + continue + } + + // fallback: notification + var notif types.Notification + if err := json.Unmarshal(body, ¬if); err == nil { + s.handleNotification(notif) + continue + } + } +} + +func (s *Server) readContentLength() (int, error) { + for { + line, err := s.in.ReadString('\n') + if err != nil { + return 0, err + } + + line = strings.TrimSpace(line) + if line == "" { + break + } + + if after, ok :=strings.CutPrefix(line, "Content-Length:"); ok { + val := strings.TrimSpace(after) + return strconv.Atoi(val) + } + } + return 0, fmt.Errorf("missing Content Length") +} + +func (s *Server) handleRequest(req types.Request) { + s.log.Info("handling request", "method", req.Method, "id", req.ID) +} + +func (s *Server) handleNotification(notif types.Notification) { + s.log.Info("handling notification", "method", notif.Method) +} + diff --git a/dendrite b/dendrite new file mode 100755 index 0000000..c732010 Binary files /dev/null and b/dendrite differ