GitHubGet started
More

backend

github.com/nsgo-dev/game/pkg/online/backend composes the online services' HTTP handlers and compiled game RPC routes into one embeddable http.Handler, so one server process can host accounts, cloudsave, leaderboard/selfhost and the game's own server-side calls.

b := backend.New(func(r *http.Request) (backend.Caller, error) {
    claims, err := verifyBearer(r)
    if err != nil {
        return backend.Caller{}, err
    }
    return backend.Caller{AccountID: claims.Subject}, nil
})
if err := b.Mount("/accounts/", accountsService.Handler()); err != nil {
    log.Fatal(err)
}
if err := b.Mount("/saves/", cloudsaveService.Handler()); err != nil {
    log.Fatal(err)
}
if err := b.RPC("claim-daily-reward", func(ctx context.Context, caller backend.Caller, raw json.RawMessage) (any, error) {
    return rewards.Claim(ctx, caller.AccountID)
}); err != nil {
    log.Fatal(err)
}
log.Fatal(http.ListenAndServe(":8080", b))

Mounts

Mount serves a handler below a prefix and removes the prefix before the handler sees the request, so the accounts service mounted at /accounts/ answers GET /v1/me at GET /accounts/v1/me. Each service keeps its own authentication, CORS, validation and error responses. A prefix without a trailing slash gets one. Mount returns an error for /, a prefix that is not a clean path, a prefix under /v1/rpc/, a prefix with characters other than letters, digits, -, _, . and /, a nil handler, and a prefix mounted twice.

RPCs

RPC serves POST /v1/rpc/{name}. A name is letters, digits, -, _ and ., and each name is registered once. Every call runs the authentication function given to New, which returns the Caller passed to the handler together with the raw JSON body; without an authentication function every RPC answers 401. AddBeforeHook callbacks run in registration order after authentication and may rewrite the JSON body (returning nil keeps it; a replacement that is not valid JSON answers 500) or return a client-visible StatusError. AddAfterHook callbacks run in registration order after a successful RPC, observe the authenticated caller, final request body and result, and cannot change the response.

Case Status
The handler returns a result 200, the result encoded as JSON
Authentication fails 401
The body is missing, not one JSON value, or larger than 1 MiB 400
The handler returns a backend.StatusError with a 4xx or 5xx code That code, with the error's message
The handler returns any other error, or a result that cannot be encoded 500, with a generic message

Return backend.StatusError{Code: http.StatusConflict, Err: err} for errors the client should see, such as "already claimed"; its message is sent to the client. Any other error stays on the server, so a database error never leaks its text to players.

Limits

Register mounts, hooks and RPCs before serving: registration is not safe to run concurrently with ServeHTTP. Hooks are process-local and apply to game RPCs, while mounted services retain their own request lifecycle. The mux has no goroutines or service stores of its own.

Edit this page