refactor api server around rpc services

This commit is contained in:
lpf
2026-03-15 01:00:41 +08:00
parent 341e578c9f
commit 231529e907
32 changed files with 5956 additions and 3614 deletions

37
pkg/rpc/envelope.go Normal file
View File

@@ -0,0 +1,37 @@
package rpc
import (
"encoding/json"
"strings"
)
type Request struct {
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
type Response struct {
OK bool `json:"ok"`
Result interface{} `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
Details interface{} `json:"details,omitempty"`
Retryable bool `json:"retryable,omitempty"`
}
func (r Request) NormalizedMethod() string {
return strings.ToLower(strings.TrimSpace(r.Method))
}
func DecodeParams(raw json.RawMessage, target interface{}) error {
if len(raw) == 0 || string(raw) == "null" {
return nil
}
return json.Unmarshal(raw, target)
}