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() {
|
func main() {
|
||||||
logging.Init()
|
logging.Init()
|
||||||
|
srv := server.New(":6969")
|
||||||
srv := server.New(nil, ":8080")
|
|
||||||
srv.Start()
|
srv.Start()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
||||||
)
|
)
|
||||||
|
|
||||||
type initializeParams struct {
|
type initializeCommand struct {
|
||||||
VaultPath string `json:"vaultPath"`
|
VaultPath string `json:"vaultPath"`
|
||||||
TemplateDir string `json:"templateDir"`
|
TemplateDir string `json:"templateDir"`
|
||||||
|
|
||||||
|
|
@ -23,7 +23,7 @@ type initializeParams struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Initialize(raw json.RawMessage) (*config.Config, error) {
|
func Initialize(raw json.RawMessage) (*config.Config, error) {
|
||||||
var params initializeParams
|
var params initializeCommand
|
||||||
|
|
||||||
if err := json.Unmarshal(raw, ¶ms); err != nil {
|
if err := json.Unmarshal(raw, ¶ms); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,21 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/config"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
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{
|
return &Server{
|
||||||
addr: addr,
|
addr: addr,
|
||||||
}
|
}
|
||||||
|
|
@ -22,7 +26,47 @@ func (s *Server) Start() error {
|
||||||
slog.Info("server started")
|
slog.Info("server started")
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
mux.HandleFunc("/rpc", rpc.RPCHandler)
|
mux.HandleFunc("/rpc", s.RPCHandler)
|
||||||
|
|
||||||
return http.ListenAndServe(s.addr, mux)
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
|
|
@ -28,28 +25,3 @@ type Notification struct {
|
||||||
Params json.RawMessage `json:"params,omitempty"`
|
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 (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
@ -20,6 +21,7 @@ func InitializeIndex(vaulPath string) error {
|
||||||
migrationsDir := filepath.Join("persistence", "migrations")
|
migrationsDir := filepath.Join("persistence", "migrations")
|
||||||
|
|
||||||
if err := applyMigrations(db, migrationsDir); err != nil {
|
if err := applyMigrations(db, migrationsDir); err != nil {
|
||||||
|
slog.Error("failed to apply migrations", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
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