feat(config): setup config with defaults
This commit is contained in:
parent
e9fcd4ef8d
commit
3eeeb9966e
7 changed files with 115 additions and 1 deletions
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
2
core/commands/commands.go
Normal file
2
core/commands/commands.go
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// Package commands provides the commands interface for the application.
|
||||
package commands
|
||||
1
core/handler/command_handler.go
Normal file
1
core/handler/command_handler.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package comms
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
// Package types contains relevant types for ipc
|
||||
// Package types types contains relevant types for ipc
|
||||
package types
|
||||
|
||||
import "encoding/json"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
dendrite
Executable file
BIN
dendrite
Executable file
Binary file not shown.
Loading…
Add table
Reference in a new issue