Skip to content
protoc-gen-sphere

protoc-gen-sphere

Generates HTTP server code from .proto service definitions, using google.api.http annotations and Sphere’s httpx / httpz runtime.

Install

  • go install github.com/go-sphere/protoc-gen-sphere@latest

Key Flags

Type flags use the import/path;Identifier format.

FlagDescriptionDefault
versionPrint version and exitfalse
omitemptySkip files whose methods have no google.api.http optiontrue
omitempty_prefixWhen set, omitempty only applies to files with this prefix""
fail_on_warnTreat generation warnings as errors (skipped streaming methods, GET/DELETE declaring a body, missing body)false
template_fileCustom Go text/template; empty uses the embedded default""
swagger_auth_headerComment injected as the authorization header in generated Swagger docs// @Param Authorization header string false "Bearer token"
router_typeRouter typegithub.com/go-sphere/httpx;Router
context_typeRequest context typegithub.com/go-sphere/httpx;Context
handler_typeType returned by each generated handlergithub.com/go-sphere/httpx;Handler
context_load_funcExpression appended to the context value to obtain a context.Context.Context()
data_resp_typeSuccess envelope; must support genericsgithub.com/go-sphere/sphere/server/httpz;DataResponse
error_resp_typeError envelopegithub.com/go-sphere/sphere/server/httpz;ErrorResponse
server_handler_funcWrapper that adapts the generated handler to the response model; must support genericsgithub.com/go-sphere/sphere/server/httpz;WithJson

Request binding no longer uses standalone parse_*_func flags. Binding is performed through methods on the configured context_type (BindJSON / BindQuery / BindURI / BindHeader / BindForm). Customize binding by changing context_type.

Buf Example

version: v2
managed:
  enabled: true
  disable:
    - file_option: go_package_prefix
      module: buf.build/googleapis/googleapis
    - file_option: go_package_prefix
      module: buf.build/bufbuild/protovalidate
  override:
    - file_option: go_package_prefix
      value: github.com/go-sphere/sphere-layout/api
plugins:
  - local: protoc-gen-sphere
    out: api
    opt:
      - paths=source_relative
      - swagger_auth_header=// @Security ApiKeyAuth

Notes

  • Generated handlers take and return httpx types. The default wrapper is httpz.WithJson.
  • GET, HEAD, DELETE, and OPTIONS never emit BindJSON. If a proto still declares body on those methods, the plugin warns (or fails with fail_on_warn) and generates the handler without a body bind.
  • Pair with protoc-gen-sphere-binding to inject binding tags into generated structs.
  • Official templates wrap Gin, Fiber, Echo, or Hertz behind httpx adapters. Changing router_type / context_type is how you retarget the generated code.
Last updated on