mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 19:52:25 +00:00
add context TODO
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package gateway
|
package gateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
@@ -13,6 +14,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context() // TODO
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
sessionUUID := r.Header.Get("X-Session-UUID")
|
sessionUUID := r.Header.Get("X-Session-UUID")
|
||||||
if sessionUUID == "" {
|
if sessionUUID == "" {
|
||||||
@@ -64,7 +67,11 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
gs.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrParseErrorS))
|
gs.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrParseErrorS))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp := gs.Route(sessionUUID, r, &single)
|
resp := gs.Route(ctx, sessionUUID, r, &single)
|
||||||
|
if resp == nil {
|
||||||
|
w.Write([]byte(""))
|
||||||
|
return
|
||||||
|
}
|
||||||
rpc.WriteResponse(w, resp)
|
rpc.WriteResponse(w, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -76,7 +83,7 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(req rpc.RPCRequest) {
|
go func(req rpc.RPCRequest) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
res := gs.Route(sessionUUID, r, &req)
|
res := gs.Route(ctx, sessionUUID, r, &req)
|
||||||
if res != nil {
|
if res != nil {
|
||||||
responses <- *res
|
responses <- *res
|
||||||
}
|
}
|
||||||
@@ -91,10 +98,12 @@ func (gs *GatewayServer) Handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
if len(result) > 0 {
|
if len(result) > 0 {
|
||||||
json.NewEncoder(w).Encode(result)
|
json.NewEncoder(w).Encode(result)
|
||||||
|
} else {
|
||||||
|
w.Write([]byte("[]"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *GatewayServer) Route(sid string, r *http.Request, req *rpc.RPCRequest) (resp *rpc.RPCResponse) {
|
func (gs *GatewayServer) Route(ctx context.Context, sid string, r *http.Request, req *rpc.RPCRequest) (resp *rpc.RPCResponse) {
|
||||||
defer utils.CatchPanicWithFallback(func(rec any) {
|
defer utils.CatchPanicWithFallback(func(rec any) {
|
||||||
gs.x.SLog.Error("panic caught in handler", slog.Any("error", rec))
|
gs.x.SLog.Error("panic caught in handler", slog.Any("error", rec))
|
||||||
resp = rpc.NewError(rpc.ErrInternalError, "Internal server error (panic)", req.ID)
|
resp = rpc.NewError(rpc.ErrInternalError, "Internal server error (panic)", req.ID)
|
||||||
@@ -110,10 +119,10 @@ func (gs *GatewayServer) Route(sid string, r *http.Request, req *rpc.RPCRequest)
|
|||||||
return rpc.NewError(rpc.ErrContextVersion, rpc.ErrContextVersionS, req.ID)
|
return rpc.NewError(rpc.ErrContextVersion, rpc.ErrContextVersionS, req.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = server.Handle(sid, r, req)
|
|
||||||
// checks if request is notification
|
// checks if request is notification
|
||||||
if req.ID == nil {
|
if req.ID == nil {
|
||||||
|
go server.Handle(ctx, sid, r, req)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return resp
|
return server.Handle(ctx, sid, r, req)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
package sv1
|
package sv1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/akyaiy/GoSally-mvp/internal/server/rpc"
|
"github.com/akyaiy/GoSally-mvp/internal/server/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *HandlerV1) Handle(sid string, r *http.Request, req *rpc.RPCRequest) *rpc.RPCResponse {
|
func (h *HandlerV1) Handle(_ context.Context, sid string, r *http.Request, req *rpc.RPCRequest) *rpc.RPCResponse {
|
||||||
if req.Method == "" {
|
if req.Method == "" {
|
||||||
h.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrMethodNotFoundS), slog.String("requested-method", req.Method))
|
h.x.SLog.Info("invalid request received", slog.String("issue", rpc.ErrMethodNotFoundS), slog.String("requested-method", req.Method))
|
||||||
return rpc.NewError(rpc.ErrMethodIsMissing, rpc.ErrMethodIsMissingS, req.ID)
|
return rpc.NewError(rpc.ErrMethodIsMissing, rpc.ErrMethodIsMissingS, req.ID)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package sv1
|
package sv1
|
||||||
|
|
||||||
|
// TODO: make a lua state pool using sync.Pool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|||||||
Reference in New Issue
Block a user