feat(rpc): handler and request
This commit is contained in:
parent
9669d2bd5c
commit
e18577b53d
6 changed files with 58 additions and 35 deletions
|
|
@ -7,7 +7,6 @@ import (
|
|||
|
||||
func main() {
|
||||
logging.Init()
|
||||
|
||||
srv := server.New(nil, ":8080")
|
||||
srv := server.New(":6969")
|
||||
srv.Start()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
6
persistence/repositories/doc.go
Normal file
6
persistence/repositories/doc.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// Package repositories provides an interface for accessing dendrite index and configuration
|
||||
package repositories
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Reference in a new issue