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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ type Request struct {
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
Jsonrpc string `json:"jsonrpc"`
|
||||||
ID *int `json:"id"`
|
ID *int `json:"id"`
|
||||||
Result any `json:"result,omitempty"`
|
Result json.RawMessage `json:"result,omitempty"`
|
||||||
Error *Error `json:"error,omitempty"`
|
Error *Error `json:"error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,17 @@ package server
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
|
"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 {
|
||||||
|
Db *sql.DB
|
||||||
handlers map[string]handlers.Handler
|
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) {
|
func (s *Server) Register(method string, handler handlers.Handler) {
|
||||||
s.handlers[method] = 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]
|
handler, ok := s.handlers[req.Method]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
s.respond(w, req.ID, nil, &rpc.Error{
|
s.write(w, rpc.Response{
|
||||||
Code: -32601,
|
Jsonrpc: "2.0",
|
||||||
Message: "method not found",
|
ID: req.ID,
|
||||||
|
Error: &rpc.Error{Code: -32601, Message: "method not found"},
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := handler.Handle(ctx, req.Params)
|
result := handler.Handle(ctx, req.Params)
|
||||||
|
|
||||||
if req.ID == nil {
|
if req.ID == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.respond(w, req.ID, result, err)
|
s.write(w, *result)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) write(w io.Writer, resp rpc.Response) {
|
func (s *Server) write(w io.Writer, resp rpc.Response) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package integration_test
|
package integration_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
|
@ -11,14 +10,7 @@ import (
|
||||||
|
|
||||||
func TestCreateNoteHandlerOnSucess(t *testing.T) {
|
func TestCreateNoteHandlerOnSucess(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
// TODO: move to test vars and main_test.go
|
uow := repositories.NewUnitOfWork(Fixture.Db)
|
||||||
db, err := sql.Open("sqlite", DbPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to open database: %v", err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
uow := repositories.NewUnitOfWork(db)
|
|
||||||
|
|
||||||
handler := handlers.NewCreateNoteHandler(uow)
|
handler := handlers.NewCreateNoteHandler(uow)
|
||||||
|
|
||||||
|
|
@ -38,5 +30,18 @@ func TestCreateNoteHandlerOnSucess(t *testing.T) {
|
||||||
request := CreateTestRequest("createNote", 1, requestParamsBytes)
|
request := CreateTestRequest("createNote", 1, requestParamsBytes)
|
||||||
|
|
||||||
// Act
|
// 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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -10,19 +11,38 @@ import (
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DbPath string = os.TempDir() + "/dendrite_test_vault"
|
type TestFixture struct {
|
||||||
var TestContext = context.Background()
|
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) {
|
func TestMain(m *testing.M) {
|
||||||
|
|
||||||
err := persistence.InitializeIndex(DbPath)
|
err := persistence.InitializeIndex(Fixture.DbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
code := m.Run()
|
code := m.Run()
|
||||||
|
|
||||||
os.RemoveAll(DbPath)
|
os.RemoveAll(Fixture.DbPath)
|
||||||
|
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
@ -35,3 +55,5 @@ func CreateTestRequest(method string, ID int, params json.RawMessage) *rpc.Reque
|
||||||
Params: params,
|
Params: params,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue