ref(server): simplified server
This commit is contained in:
parent
ed2ebff9d3
commit
18c2af7928
5 changed files with 57 additions and 32 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ type Request struct {
|
|||
type Response struct {
|
||||
Jsonrpc string `json:"jsonrpc"`
|
||||
ID *int `json:"id"`
|
||||
Result any `json:"result,omitempty"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *Error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,20 @@
|
|||
// Package server contains the server running the JSON-RPC 2.0 Protocol.
|
||||
// Package server contains the server running the JSON-RPC 2.0 Protocol.
|
||||
package server
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Db *sql.DB
|
||||
handlers map[string]handlers.Handler
|
||||
}
|
||||
|
||||
|
|
@ -21,6 +24,10 @@ func NewServer() *Server {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Server) InitDatabase(db *sql.DB) {
|
||||
s.Db = db
|
||||
}
|
||||
|
||||
func (s *Server) Register(method string, handler handlers.Handler) {
|
||||
s.handlers[method] = handler
|
||||
}
|
||||
|
|
@ -51,31 +58,21 @@ func (s *Server) handle(w io.Writer, req rpc.Request) {
|
|||
handler, ok := s.handlers[req.Method]
|
||||
|
||||
if !ok {
|
||||
s.respond(w, req.ID, nil, &rpc.Error{
|
||||
Code: -32601,
|
||||
Message: "method not found",
|
||||
s.write(w, rpc.Response{
|
||||
Jsonrpc: "2.0",
|
||||
ID: req.ID,
|
||||
Error: &rpc.Error{Code: -32601, Message: "method not found"},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := handler.Handle(ctx, req.Params)
|
||||
result := handler.Handle(ctx, req.Params)
|
||||
|
||||
if req.ID == nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.respond(w, req.ID, result, err)
|
||||
}
|
||||
|
||||
func (s *Server) respond(w io.Writer, id *int, result any, err *rpc.Error) {
|
||||
resp := rpc.Response{
|
||||
Jsonrpc: "2.0",
|
||||
ID: id,
|
||||
Result: result,
|
||||
Error: err,
|
||||
}
|
||||
|
||||
s.write(w, resp)
|
||||
s.write(w, *result)
|
||||
}
|
||||
|
||||
func (s *Server) write(w io.Writer, resp rpc.Response) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package integration_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
|
|
@ -11,14 +10,7 @@ import (
|
|||
|
||||
func TestCreateNoteHandlerOnSucess(t *testing.T) {
|
||||
// Arrange
|
||||
// TODO: move to test vars and main_test.go
|
||||
db, err := sql.Open("sqlite", DbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
uow := repositories.NewUnitOfWork(db)
|
||||
uow := repositories.NewUnitOfWork(Fixture.Db)
|
||||
|
||||
handler := handlers.NewCreateNoteHandler(uow)
|
||||
|
||||
|
|
@ -38,5 +30,18 @@ func TestCreateNoteHandlerOnSucess(t *testing.T) {
|
|||
request := CreateTestRequest("createNote", 1, requestParamsBytes)
|
||||
|
||||
// Act
|
||||
response, rpcError = handler.Handle(TestContext, request.Params)
|
||||
response := handler.Handle(Fixture.TestContext, request.Params)
|
||||
|
||||
// Assert
|
||||
if response.Error != nil {
|
||||
t.Fatalf("expected no error, got: %v", response.Error)
|
||||
}
|
||||
|
||||
if response.Result == nil {
|
||||
t.Fatal("expected result, got nil")
|
||||
}
|
||||
|
||||
if *response.ID != 1 {
|
||||
t.Fatal("expected non-empty ID in response result")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package integration_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
|
@ -10,19 +11,38 @@ import (
|
|||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
||||
)
|
||||
|
||||
var DbPath string = os.TempDir() + "/dendrite_test_vault"
|
||||
var TestContext = context.Background()
|
||||
type TestFixture struct {
|
||||
Db *sql.DB
|
||||
DbPath string
|
||||
TestContext context.Context
|
||||
}
|
||||
|
||||
func NewTestFixture() (*TestFixture, error) {
|
||||
dbPath := os.TempDir() + "/dendrite_test_vault"
|
||||
db, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TestFixture{
|
||||
Db: db,
|
||||
DbPath: dbPath,
|
||||
TestContext: context.Background(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
var Fixture, err = NewTestFixture()
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
err := persistence.InitializeIndex(DbPath)
|
||||
err := persistence.InitializeIndex(Fixture.DbPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
code := m.Run()
|
||||
|
||||
os.RemoveAll(DbPath)
|
||||
os.RemoveAll(Fixture.DbPath)
|
||||
|
||||
os.Exit(code)
|
||||
}
|
||||
|
|
@ -35,3 +55,5 @@ func CreateTestRequest(method string, ID int, params json.RawMessage) *rpc.Reque
|
|||
Params: params,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue