api

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package api is a platform module: a gRPC server and a REST gateway in front of it, with the interceptors every service needs and the OpenAPI description of the API.

The project writes proto files and handlers and registers its services from wireDomain; the module owns the servers, the interceptor chain, the gateway options, health, metrics, CORS and the documentation page.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddStreamInterceptor

func AddStreamInterceptor(app *platform.App, i grpc.StreamServerInterceptor)

AddStreamInterceptor adds a project stream interceptor.

func AddUnaryInterceptor

func AddUnaryInterceptor(app *platform.App, i grpc.UnaryServerInterceptor)

AddUnaryInterceptor adds a project interceptor, such as authentication. Project interceptors run after recovery, metrics and logging and before request validation.

func HandleHTTP

func HandleHTTP(app *platform.App, pattern string, h http.Handler)

HandleHTTP serves a plain HTTP route next to the gateway: webhooks, file downloads, anything that is not a gRPC call.

func Register

func Register(app *platform.App, s Service)

Register adds a gRPC service and its gateway.

func UseHTTP

func UseHTTP(app *platform.App, mw func(http.Handler) http.Handler)

UseHTTP wraps the whole REST side in a middleware.

Types

type Config

type Config struct {
	GRPCAddr    string   // gRPC listen address
	HTTPAddr    string   // REST gateway listen address
	MaxRecvSize int      // largest request message, bytes
	MaxSendSize int      // largest response message, bytes
	CORSOrigins []string // origins allowed to call the REST API from a browser; "*" allows any
	Docs        bool     // serve /openapi.yaml and the /docs page
	Reflection  bool     // gRPC server reflection, for grpcurl and similar tools
}

Config holds module settings. Load fills it from the environment; the platform generator writes the Load call into the project's config.gen.go.

func Load

func Load(l *confx.Loader) Config

Load reads the module settings from environment variables.

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module implements platform.Module.

func New

func New(cfg Config, opts ...Option) *Module

New creates the module from ready settings.

func (*Module) GRPCAddr

func (m *Module) GRPCAddr() string

GRPCAddr is the address the gRPC server listens on.

func (*Module) HTTPAddr

func (m *Module) HTTPAddr() string

HTTPAddr is the address the REST gateway listens on.

func (*Module) Health

func (m *Module) Health(context.Context) error

Health reports whether both servers are serving.

func (*Module) Init

func (m *Module) Init(_ context.Context, app *platform.App) error

Init puts the registry into the container, so wireDomain can register services.

func (*Module) Name

func (m *Module) Name() string

func (*Module) Start

func (m *Module) Start(ctx context.Context) error

Start builds the servers from what the project registered and starts serving.

func (*Module) Stop

func (m *Module) Stop(ctx context.Context) error

Stop stops taking requests and lets the running ones finish within the shutdown timeout; what is still running after it is cut off.

type Option

type Option func(*Module)

Option configures the module.

func WithOpenAPI

func WithOpenAPI(fsys fs.FS) Option

WithOpenAPI gives the module the generated OpenAPI description: a directory holding openapi.yaml. The generated wiring passes the embedded api/openapi directory.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds what the project adds to the API. It lives in the container and is filled from wireDomain, before the module starts.

type Service

type Service struct {
	GRPC    func(*grpc.Server)
	Gateway func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error
}

Service is a gRPC service of the project and, optionally, its REST gateway. Both functions are generated: RegisterXServer from protoc-gen-go-grpc and RegisterXHandler from protoc-gen-grpc-gateway.

api.Register(app, api.Service{
	GRPC:    func(s *grpc.Server) { ordersv1.RegisterOrdersServiceServer(s, handler) },
	Gateway: ordersv1.RegisterOrdersServiceHandler,
})

Directories

Path Synopsis
internal
testapi/platformtest/v1
Package platformtestv1 is a reverse proxy.
Package platformtestv1 is a reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL