Documentation
¶
Overview ¶
Package rest supports creating RESTful applications.
Index ¶
- func Builder[T Configer](f func(context.Context, T) (*Api, error)) bedrock.AppBuilder[T]
- func DefaultConfig() config.Source
- func Run[T Configer](r io.Reader, f func(context.Context, T) (*Api, error), opts ...RunOption)
- func WithDefaultConfig(r io.Reader) config.Source
- type Api
- type Config
- type Configer
- type HttpServerProvider
- type ListenerProvider
- type Operation
- type RouterOption
- type RouterOptions
- type RunOption
- type RunOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Builder ¶ added in v0.5.0
Builder initializes a bedrock.AppBuilder for your Api.
func DefaultConfig ¶ added in v0.4.0
DefaultConfig is the default config.Source which aligns with the Config type.
func Run ¶ added in v0.4.0
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
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
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
HttpServer implements the Configer interface.
type Configer ¶ added in v0.5.0
type Configer interface { appbuilder.OTelInitializer ListenerProvider HttpServerProvider }
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
ListenerProvider initializes a net.Listener.
type Operation ¶ added in v0.5.0
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 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
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.