Proto Packages & Runtime
Sphere extends Protobuf with specialized packages and provides a runtime layer that makes building HTTP APIs clean and efficient.
Proto Packages Overview
Sphere includes focused Protobuf packages that keep HTTP binding, errors, and custom options declarative. These annotations power Sphere’s generators and help maintain consistent, type-safe APIs.
sphere/binding
Purpose:
- Declare where each field binds from (URI, query, body, header, form)
- Set message/oneof defaults and auto-tags for generated structs
- Work seamlessly with Sphere’s request binding helpers
Use when:
- You want explicit, generator-driven request parsing rules
- You prefer consistent struct tags without hand editing
Example:
message GetUserRequest {
int64 user_id = 1 [(sphere.binding.location) = BINDING_LOCATION_URI];
repeated string fields = 2 [(sphere.binding.location) = BINDING_LOCATION_QUERY];
string auth_token = 3 [(sphere.binding.location) = BINDING_LOCATION_HEADER];
}See API Definitions Guide for detailed examples.
sphere/errors
Purpose:
- Define typed error enums with HTTP status, reason, and message
- Generate helpers to wrap causes and produce uniform JSON errors
- Map error codes to HTTP status codes automatically
Use when:
- You need consistent error shapes across services and clients
- You want programmatic access to status/code/reason/message
Example:
enum UserError {
option (sphere.errors.default_status) = 500;
USER_ERROR_NOT_FOUND = 1001 [(sphere.errors.options) = {
status: 404
reason: "USER_NOT_FOUND"
message: "User not found"
}];
}See Error Handling Guide for implementation details.
sphere/options
Purpose:
- Attach simple key/value metadata to RPC methods
- Let generators consume options for advanced routing or transports
Use when:
- You build adapters beyond HTTP
- You need custom routing hints or generator options
Runtime Layer
Sphere’s HTTP runtime is split in two:
httpx— router/context/handler interfaces, plus adapters for Gin, Fiber, Echo, and Hertzserver/httpz— JSON envelopes,WithJson, andAbortWithJsonErroron top ofhttpx
Official templates still use Gin as the default engine, but generated code talks to httpx, not *gin.Context.
Core Components
Response Wrappers:
httpz.WithJson[T]: wraps a handler returning(T, error)and serializes success toDataResponse[T]httpz.AbortWithJsonError: normalizes errors toErrorResponsewith HTTP status, applicationcode, and a user-facingmessage
Request Binding:
httpx.Contextmethods:BindJSON,BindQuery,BindURI,BindHeader,BindForm- Struct tags come from
sphere/bindingviaprotoc-gen-sphere-binding
Server Features:
- Docs Server: auxiliary HTTP server for Swagger UI
- File / proxy helpers:
server/service/file,server/service/reverseproxy - Middleware: auth, CORS, online tracking, rate limiting, selector
Typical Request Flow
- Protobuf +
protoc-gen-spheregenerate handler plumbing - Request arrives at an
httpxadapter (Gin by default) - Handler binds request data to generated structs (using sphere/binding tags)
- Service method executes business logic, returns data or a typed error
httpz.WithJsonwritesDataResponseor routes the error throughAbortWithJsonError
Example Handler
// Generated by protoc-gen-sphere
func _UserService_GetUser0_HTTP_Handler(srv UserServiceHTTPServer) httpx.Handler {
return httpz.WithJson(func(ctx httpx.Context) (*User, error) {
var in GetUserRequest
if err := ctx.BindHeader(&in); err != nil {
return nil, err
}
if err := ctx.BindQuery(&in); err != nil {
return nil, err
}
if err := ctx.BindURI(&in); err != nil {
return nil, err
}
return srv.GetUser(ctx.Context(), &in)
})
}Extensibility
- Custom Router Types: swap Gin for Fiber, Echo, or Hertz via
httpxadapters andprotoc-gen-sphererouter_type/context_typeflags - Response Envelope: override
data_resp_type/error_resp_type/server_handler_func - Error Parser:
httpz.SetDefaultErrorParserto merge validation or domain-specific errors - Debug leaks:
httpz.SetDebugMode(true)includeserr.Error()inErrorResponse.Error; production leaves it empty
See HTTP Runtime for envelopes, debug mode, and adapter wiring.