rest

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: MIT Imports: 24 Imported by: 1

Documentation

Overview

Package rest supports creating RESTful applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Builder added in v0.5.0

func Builder[T Configer](f func(context.Context, T) (*Api, error)) bedrock.AppBuilder[T]

Builder initializes a bedrock.AppBuilder for your Api.

func DefaultConfig added in v0.4.0

func DefaultConfig() config.Source

DefaultConfig is the default config.Source which aligns with the Config type.

func Run added in v0.4.0

func Run[T Configer](r io.Reader, f func(context.Context, T) (*Api, error), opts ...RunOption)

Run begins by reading, parsing and unmarshaling your custom config into the type T. Then it calls the providing function to initialize your Api implementation. Once it has the Api implementation, it begins serving the Api over HTTP. Various middlewares are applied at different stages for your convenience. Some middlewares include, automattic panic recovery, OTel SDK initialization and shutdown, and OS signal based shutdown.

func WithDefaultConfig added in v0.5.0

func WithDefaultConfig(r io.Reader) config.Source

WithDefaultConfig extends the config.Source returned by DefaultConfig to include values from the given io.Reader. The io.Reader can provide custom values, as well as, override default values.

Types

type Api added in v0.5.0

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

Api is a OpenAPI compliant http.Handler.

Api provides a set of standard features: - OpenAPI schema as JSON at "/openapi.json" - Liveness endpoint at "/health/liveness" - Readiness endpoint at "/health/readiness" - Standardized NotFound behaviour - Standardized MethodNotAllowed behaviour

func NewApi added in v0.5.0

func NewApi(title, version string, opts ...RouterOption) *Api

NewApi initializes a Api.

func (*Api) Route added in v0.5.0

func (api *Api) Route(method, pattern string, op Operation) error

Route will configure any request matching method and pattern to be handled by the provided Operation. It will also register the Operation with an underlying OpenAPI 3.0 schema.

func (*Api) ServeHTTP added in v0.5.0

func (api *Api) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface.

type Config

type Config struct {
	humus.Config `config:",squash"`

	OpenApi struct {
		Title   string `config:"title"`
		Version string `config:"version"`
	} `config:"openapi"`

	HTTP struct {
		Port uint `config:"port"`
	} `config:"http"`
}

Config is the default config which can be easily embedded into a more custom app specific config.

func (Config) HttpServer added in v0.5.0

func (c Config) HttpServer(ctx context.Context, h http.Handler) (*http.Server, error)

HttpServer implements the Configer interface.

func (Config) Listener added in v0.5.0

func (c Config) Listener(ctx context.Context) (net.Listener, error)

Listener implements the Configer interface.

type Configer added in v0.5.0

Configer is leveraged to constrain the custom config type into supporting specific initialization behaviour required by Run.

type HttpServerProvider added in v0.5.0

type HttpServerProvider interface {
	HttpServer(context.Context, http.Handler) (*http.Server, error)
}

HttpServerProvider initializes a http.Server.

type ListenerProvider added in v0.5.0

type ListenerProvider interface {
	Listener(context.Context) (net.Listener, error)
}

ListenerProvider initializes a net.Listener.

type Operation added in v0.5.0

type Operation interface {
	http.Handler

	Spec() (openapi3.Operation, error)
}

Operation extends the http.Handler interface by forcing any implementation to also provided a OpenAPI 3.0 representation of its operation.

type RouterOption added in v0.5.0

type RouterOption interface {
	ApplyRouterOption(*RouterOptions)
}

RouterOption sets values on [MuxOptions].

func Liveness

func Liveness(m health.Monitor) RouterOption

Liveness will register the given health.Monitor to be used for reporting when the entire application needs to be restarted.

See [Liveness, Readiness, and Startup Probes](https://kubernetes.io/docs/concepts/configuration/liveness-readiness-startup-probes/) for more details.

func MethodNotAllowed added in v0.5.0

func MethodNotAllowed(h http.Handler) RouterOption

MethodNotAllowed

func NotFound added in v0.5.0

func NotFound(h http.Handler) RouterOption

NotFound

func Readiness

func Readiness(m health.Monitor) RouterOption

Readiness will register the given health.Monitor to be used for reporting when the application is ready for to start accepting traffic.

An example usage of this is to tie the health.Monitor to any backend client circuit breakers. When one of the circuit breakers moves to an OPEN state your application can quickly notify upstream component(s) (e.g. load balancer) that no requests should be sent to it since they'll just fail anyways due to the circuit being OPEN.

See [Liveness, Readiness, and Startup Probes](https://kubernetes.io/docs/concepts/configuration/liveness-readiness-startup-probes/) for more details.

type RouterOptions added in v0.5.0

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

RouterOptions represents configurable values for a [Mux].

type RunOption added in v0.5.0

type RunOption interface {
	ApplyRunOption(*RunOptions)
}

RunOption sets a value on RunOptions.

func LogHandler added in v0.5.0

func LogHandler(h slog.Handler) RunOption

LogHandler overrides the default slog.Handler used for logging any error encountered while building or running the Api.

type RunOptions added in v0.5.0

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

RunOptions are used for configuring the running of a Api.

Directories

Path Synopsis
Package rpc helps users implement [http.Handler]s using a RPC style interface.
Package rpc helps users implement [http.Handler]s using a RPC style interface.

Jump to

Keyboard shortcuts

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