API 參考取自原始碼中的 Go 文件註解,以英文撰寫。
package backend
import "github.com/nsgo-dev/game/pkg/online/backend"概覽
Package backend is experimental: its API and documented formats may change in a minor release. See the API stability policy. composes online service handlers and compiled game RPCs in one embeddable HTTP handler.
索引
- type AfterHook
- type Backend
- func New(auth func(*http.Request) (Caller, error)) *Backend
- func (b *Backend) AddAfterHook(fn AfterHook) error
- func (b *Backend) AddBeforeHook(fn BeforeHook) error
- func (b *Backend) Mount(prefix string, h http.Handler) error
- func (b *Backend) RPC(name string, fn RPCFunc) error
- func (b *Backend) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type BeforeHook
- type Caller
- type RPCFunc
- type StatusError
型別
type AfterHook func(context.Context, Caller, string, json.RawMessage, any)AfterHook runs after a game RPC returns a result and before that result is encoded. It cannot change the response or reject the call.
type Backend struct {
// contains filtered or unexported fields
}Backend is an embeddable service mux. Hooks and service-specific auth remain owned by the mounted handlers, while RPC authorization is supplied by the auth function given to New. Register mounts and RPCs before serving; registration is not safe to run concurrently with itself or with ServeHTTP.
func New(auth func(*http.Request) (Caller, error)) *BackendNew returns an empty backend. Auth is required for RPC calls and may inspect the request's bearer token using the application's verifier.
func (b *Backend) AddAfterHook(fn AfterHook) errorAddAfterHook registers a hook for every successful RPC. Hooks run in registration order after the RPC returns and before its result is encoded. Register hooks before serving; registration is not safe to run concurrently with ServeHTTP.
func (b *Backend) AddBeforeHook(fn BeforeHook) errorAddBeforeHook registers a hook for every RPC. Hooks run in registration order. Register hooks before serving; registration is not safe to run concurrently with ServeHTTP.
func (b *Backend) Mount(prefix string, h http.Handler) errorMount mounts an existing service handler below prefix, such as /accounts/. The handler sees request paths with the prefix removed. A prefix without a trailing slash gets one; a prefix of "/", one that is not a clean path, one under /v1/rpc/, or one with characters other than letters, digits, '-', '_', '.' and '/' is refused, as is a prefix mounted twice.
func (b *Backend) RPC(name string, fn RPCFunc) errorRPC registers POST /v1/rpc/name. Name is made of letters, digits, '-', '_' and '.', is not "." or "..", and each name is registered once.
The handler's result is encoded as JSON. A StatusError with a 4xx or 5xx code is answered with that code and its message; any other error is answered with 500 and a generic message, so internal error text never reaches the client.
func (b *Backend) ServeHTTP(w http.ResponseWriter, r *http.Request)ServeHTTP serves mounted services and RPCs.
type BeforeHook func(context.Context, Caller, string, json.RawMessage) (json.RawMessage, error)BeforeHook runs after RPC authentication and JSON decoding, before the game RPC. It returns a replacement JSON value, nil to keep the body unchanged, or an error. A StatusError exposes a safe 4xx or 5xx response to the caller; a replacement that is not valid JSON is answered with 500, as any other error is.
type Caller struct{ AccountID string }Caller identifies the authenticated account invoking an RPC.
type RPCFunc func(context.Context, Caller, json.RawMessage) (any, error)RPCFunc handles one game-defined JSON RPC.
type StatusError struct {
Code int
Err error
}StatusError lets a game RPC choose its HTTP error status. Its message is sent to the client, so it must be safe to show.
func (e StatusError) Error() stringError returns the wrapped error's message, or the status text when Err is nil.