diff --git a/cmd/main.go b/cmd/main.go index f3ae387..d541f0a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,7 +1,13 @@ package main import ( + "github.com/KristianJBorgwarth/dendrite.daemon/core/http" + "github.com/KristianJBorgwarth/dendrite.daemon/core/logging" ) func main() { + logging.Init() + + srv := server.New(nil, ":8080") + srv.Start() } diff --git a/core/http/server.go b/core/http/server.go new file mode 100644 index 0000000..7c55fd1 --- /dev/null +++ b/core/http/server.go @@ -0,0 +1,28 @@ +// Package server provides the implementation of the server component of the application. +package server + +import ( + "log/slog" + "net/http" + + "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" +) + +type Server struct { + addr string +} + +func New(log *slog.Logger, addr string) *Server { + return &Server{ + addr: addr, + } +} + +func (s *Server) Start() error { + slog.Info("server started") + mux := http.NewServeMux() + + mux.HandleFunc("/rpc", rpc.RPCHandler) + + return http.ListenAndServe(s.addr, mux) +} diff --git a/core/logging/logger.go b/core/logging/logger.go new file mode 100644 index 0000000..c8ce991 --- /dev/null +++ b/core/logging/logger.go @@ -0,0 +1,25 @@ +// Package logging +package logging + +import ( + "io" + "log/slog" + "os" +) + +func Init() { + logFile, err := os.OpenFile("/tmp/dendrite.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) + if err != nil { + panic(err) + } + + writer := io.MultiWriter(os.Stdout, logFile) + + handler := slog.NewTextHandler(writer, &slog.HandlerOptions{ + Level: slog.LevelDebug, + }) + + logger := slog.New(handler) + + slog.SetDefault(logger) +} diff --git a/core/models/types.go b/core/models/types.go deleted file mode 100644 index 149a10e..0000000 --- a/core/models/types.go +++ /dev/null @@ -1,24 +0,0 @@ -// Package types types contains relevant types for ipc -package types - -import "encoding/json" - -type Request struct { - Jsonrpc string `json:"jsonrpc"` - ID int `json:"id"` - Method string `json:"method"` - Params json.RawMessage `json:"params"` -} - -type Response struct { - Jsonrpc string `json:"jsonrpc"` - ID int `json:"id"` - Result interface{} `json:"result,omitempty"` - Error interface{} `json:"error,omitempty"` -} - -type Notification struct { - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params json.RawMessage `json:"params,omitempty"` -} diff --git a/core/rpc/rpc_handler.go b/core/rpc/rpc_handler.go new file mode 100644 index 0000000..49db38f --- /dev/null +++ b/core/rpc/rpc_handler.go @@ -0,0 +1,55 @@ +// Package rpc provides a simple implementation of JSON-RPC 2.0 protocol for handling RPC requests and notifications. +package rpc + +import ( + "encoding/json" + "net/http" + + "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers" +) + +type Request struct { + Jsonrpc string `json:"jsonrpc"` + ID int `json:"id"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` +} + +type Response struct { + Jsonrpc string `json:"jsonrpc"` + ID int `json:"id"` + Result any `json:"result,omitempty"` + Error any `json:"error,omitempty"` +} + +type Notification struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + 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/dendrite b/dendrite index a371e55..83ad510 100755 Binary files a/dendrite and b/dendrite differ diff --git a/persistence/index.db b/persistence/index.db deleted file mode 100644 index d7c1874..0000000 Binary files a/persistence/index.db and /dev/null differ