mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 21:12:25 +00:00
Compare commits
2 Commits
ec94df5f4a
...
0151c3f68a
| Author | SHA1 | Date | |
|---|---|---|---|
| 0151c3f68a | |||
| 1f36f2d7bc |
@@ -1,10 +1,5 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrParseError = -32700
|
||||
ErrParseErrorS = "Parse error"
|
||||
@@ -24,16 +19,3 @@ const (
|
||||
ErrContextVersion = -32010
|
||||
ErrContextVersionS = "Invalid context version"
|
||||
)
|
||||
|
||||
func WriteRouterError(w http.ResponseWriter, status int, e *RPCError) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
|
||||
data, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(data)
|
||||
return err
|
||||
}
|
||||
|
||||
33
internal/server/rpc/writers.go
Normal file
33
internal/server/rpc/writers.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func write(w http.ResponseWriter, status int, msg any) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
|
||||
switch m := msg.(type) {
|
||||
case RPCError, *RPCError,
|
||||
RPCResponse, *RPCResponse:
|
||||
data, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(data)
|
||||
return err
|
||||
default:
|
||||
return fmt.Errorf("invalid RPC structure: %T", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func WriteRouterError(w http.ResponseWriter, status int, errm *RPCError) error {
|
||||
return write(w, status, errm)
|
||||
}
|
||||
|
||||
func WriteResponse(w http.ResponseWriter, response *RPCResponse) error {
|
||||
return write(w, http.StatusOK, response)
|
||||
}
|
||||
@@ -7,7 +7,11 @@ import (
|
||||
)
|
||||
|
||||
func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request, req rpc.RPCRequest) {
|
||||
w.Write([]byte("Sigmas"))
|
||||
rpc.WriteResponse(w, &rpc.RPCResponse{
|
||||
JSONRPC: rpc.JSONRPCVersion,
|
||||
ID: req.ID,
|
||||
Result: "Hi",
|
||||
}) // test answer to make sure everything works
|
||||
}
|
||||
|
||||
// func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user