feat(config): setup config with defaults

This commit is contained in:
Kristian Borgwarth 2026-03-22 13:01:19 +01:00
parent e9fcd4ef8d
commit 3eeeb9966e
7 changed files with 115 additions and 1 deletions

View file

@ -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"
}
}

View file

@ -0,0 +1,2 @@
// Package commands provides the commands interface for the application.
package commands

View file

@ -0,0 +1 @@
package comms

View file

@ -1,4 +1,4 @@
// Package types contains relevant types for ipc
// Package types types contains relevant types for ipc
package types
import "encoding/json"

View file

@ -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, &notif); 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)
}

BIN
dendrite Executable file

Binary file not shown.