diff --git a/cmd/main.go b/cmd/main.go index d541f0a..efec73d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,7 +7,6 @@ import ( func main() { logging.Init() - - srv := server.New(nil, ":8080") + srv := server.New(":6969") srv.Start() } diff --git a/core/handlers/init.go b/core/handlers/init.go index f6ff12b..d5384f5 100644 --- a/core/handlers/init.go +++ b/core/handlers/init.go @@ -6,7 +6,7 @@ import ( "github.com/KristianJBorgwarth/dendrite.daemon/persistence" ) -type initializeParams struct { +type initializeCommand struct { VaultPath string `json:"vaultPath"` TemplateDir string `json:"templateDir"` @@ -23,7 +23,7 @@ type initializeParams struct { } func Initialize(raw json.RawMessage) (*config.Config, error) { - var params initializeParams + var params initializeCommand if err := json.Unmarshal(raw, ¶ms); err != nil { return nil, err diff --git a/core/http/server.go b/core/http/server.go index 7c55fd1..17cb995 100644 --- a/core/http/server.go +++ b/core/http/server.go @@ -2,17 +2,21 @@ package server import ( + "encoding/json" "log/slog" "net/http" + "github.com/KristianJBorgwarth/dendrite.daemon/config" + "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers" "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" ) type Server struct { - addr string + addr string + config *config.Config } -func New(log *slog.Logger, addr string) *Server { +func New(addr string) *Server { return &Server{ addr: addr, } @@ -22,7 +26,47 @@ func (s *Server) Start() error { slog.Info("server started") mux := http.NewServeMux() - mux.HandleFunc("/rpc", rpc.RPCHandler) + mux.HandleFunc("/rpc", s.RPCHandler) return http.ListenAndServe(s.addr, mux) } + +func (s *Server) RPCHandler(w http.ResponseWriter, r *http.Request) { + var req rpc.Request + + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "Invalid JSON", http.StatusBadRequest) + return + } + + resp := rpc.Response{ + Jsonrpc: "2.0", + ID: req.ID, + } + + switch req.Method { + case "initialize": + cfg, err := handlers.Initialize(req.Params) + if err != nil { + resp.Error = map[string]any{ + "code": -1, + "message": err.Error(), + } + } else { + s.config = cfg + resp.Result = map[string]any{"status": "ok"} + } + default: + slog.Warn("Unknown method", "method", req.Method) + resp.Error = map[string]any{ + "code": -32601, + "message": "method not found", + } + } + + w.Header().Set("Content-Type", "application/json") + + if err := json.NewEncoder(w).Encode(resp); err != nil { + slog.Error("failed to write response", "error", err) + } +} diff --git a/core/rpc/rpc_handler.go b/core/rpc/models.go similarity index 54% rename from core/rpc/rpc_handler.go rename to core/rpc/models.go index 49db38f..ca359f5 100644 --- a/core/rpc/rpc_handler.go +++ b/core/rpc/models.go @@ -3,9 +3,6 @@ package rpc import ( "encoding/json" - "net/http" - - "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers" ) type Request struct { @@ -28,28 +25,3 @@ type Notification struct { Params json.RawMessage `json:"params,omitempty"` } -func RPCHandler(w http.ResponseWriter, r *http.Request) { - var req Request - - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - http.Error(w, "Invalid JSON", http.StatusBadRequest) - return - } - - resp := Response{ - Jsonrpc: "2.0", - ID: req.ID, - } - - switch req.Method { - case "initialize": - result, err := handlers.Initialize(req.Params) - if err != nil { - resp.Error = err.Error() - } else { - resp.Result = result - } - default: - resp.Error = "Method not found" - } -} diff --git a/persistence/db_migrator.go b/persistence/db_migrator.go index e2cb22a..87541af 100644 --- a/persistence/db_migrator.go +++ b/persistence/db_migrator.go @@ -3,6 +3,7 @@ package persistence import ( "database/sql" + "log/slog" "os" "path/filepath" "sort" @@ -20,6 +21,7 @@ func InitializeIndex(vaulPath string) error { migrationsDir := filepath.Join("persistence", "migrations") if err := applyMigrations(db, migrationsDir); err != nil { + slog.Error("failed to apply migrations", "error", err) return err } return nil diff --git a/persistence/repositories/doc.go b/persistence/repositories/doc.go new file mode 100644 index 0000000..0660717 --- /dev/null +++ b/persistence/repositories/doc.go @@ -0,0 +1,6 @@ +// Package repositories provides an interface for accessing dendrite index and configuration +package repositories + + + +