feat(http): setup http server
This commit is contained in:
parent
be6a6fc4aa
commit
9669d2bd5c
7 changed files with 114 additions and 24 deletions
|
|
@ -1,7 +1,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/http"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
logging.Init()
|
||||||
|
|
||||||
|
srv := server.New(nil, ":8080")
|
||||||
|
srv.Start()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
core/http/server.go
Normal file
28
core/http/server.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
25
core/logging/logger.go
Normal file
25
core/logging/logger.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -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"`
|
|
||||||
}
|
|
||||||
55
core/rpc/rpc_handler.go
Normal file
55
core/rpc/rpc_handler.go
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
dendrite
BIN
dendrite
Binary file not shown.
Binary file not shown.
Loading…
Add table
Reference in a new issue