Documentation ¶
Index ¶
- Constants
- Variables
- func DecodeBody(res http.ResponseWriter, req *http.Request, out any) error
- func HasFlush(w http.ResponseWriter) bool
- func HasHijack(w http.ResponseWriter) bool
- func HasReadFrom(w http.ResponseWriter) bool
- func ListenAndServe(s HTTPServer, address string) error
- func StartJSONResponse(w http.ResponseWriter, statusCode int)
- func Wrap(f HandlerFn) http.Handler
- func WrapFn(f HandlerFn) http.HandlerFunc
- func WrapHandler(h Handler) http.Handler
- func WriteError(w http.ResponseWriter, err *Error)
- func WriteJSON(w http.ResponseWriter, statusCode int, body any) error
- func WriteOK(w http.ResponseWriter) error
- func WriteOKMessage(w http.ResponseWriter, msg string) error
- type AppMux
- type AppMuxConfig
- type Blueprint
- type BlueprintImpl
- type Error
- type ErrorOption
- type HTTPHandler
- type HTTPServer
- type Handler
- type HandlerApp
- type HandlerAppConfig
- type HandlerFn
- type HandlerProvider
- type Mounter
- type MounterHandler
- type ServerConfig
- func (c ServerConfig) WithBlueprint(af Blueprint) ServerConfig
- func (c ServerConfig) WithIdleTimeout(v time.Duration) ServerConfig
- func (c ServerConfig) WithLogger(logger logr.Logger) ServerConfig
- func (c ServerConfig) WithReadHeaderTimeout(v time.Duration) ServerConfig
- func (c ServerConfig) WithReadTimeout(v time.Duration) ServerConfig
- func (c ServerConfig) WithTLSConfig(cfg *tls.Config) ServerConfig
- func (c ServerConfig) WithWriteTimeout(v time.Duration) ServerConfig
- type UnsupportedError
Constants ¶
const (
DefaultAddress = ":http"
)
Variables ¶
var ( ErrMalformedContent = errors.New("content has syntax error") ErrTooLarge = errors.New("request is too large") )
var ( ErrBadRequest = NewErrorOpts(http.StatusBadRequest) ErrPaymentRequired = NewErrorOpts(http.StatusPaymentRequired) ErrForbidden = NewErrorOpts(http.StatusForbidden) ErrNotFound = NewErrorOpts(http.StatusNotFound) ErrMethodNotAllowed = NewErrorOpts(http.StatusMethodNotAllowed) ErrNotAcceptable = NewErrorOpts(http.StatusNotAcceptable) ErrProxyAuthRequired = NewErrorOpts(http.StatusProxyAuthRequired) ErrRequestTimeout = NewErrorOpts(http.StatusRequestTimeout) ErrConflict = NewErrorOpts(http.StatusConflict) ErrGone = NewErrorOpts(http.StatusGone) ErrLengthRequired = NewErrorOpts(http.StatusLengthRequired) ErrPreconditionFailed = NewErrorOpts(http.StatusPreconditionFailed) ErrRequestEntityTooLarge = NewErrorOpts(http.StatusRequestEntityTooLarge) ErrRequestURITooLong = NewErrorOpts(http.StatusRequestURITooLong) ErrUnsupportedMediaType = NewErrorOpts(http.StatusUnsupportedMediaType) ErrRequestedRangeNotSatisfiable = NewErrorOpts(http.StatusRequestedRangeNotSatisfiable) ErrExpectationFailed = NewErrorOpts(http.StatusExpectationFailed) ErrTeapot = NewErrorOpts(http.StatusTeapot) ErrMisdirectedRequest = NewErrorOpts(http.StatusMisdirectedRequest) ErrUnprocessableEntity = NewErrorOpts(http.StatusUnprocessableEntity) ErrLocked = NewErrorOpts(http.StatusLocked) ErrFailedDependency = NewErrorOpts(http.StatusFailedDependency) ErrTooEarly = NewErrorOpts(http.StatusTooEarly) ErrUpgradeRequired = NewErrorOpts(http.StatusUpgradeRequired) ErrPreconditionRequired = NewErrorOpts(http.StatusPreconditionRequired) ErrTooManyRequests = NewErrorOpts(http.StatusTooManyRequests) ErrRequestHeaderFieldsTooLarge = NewErrorOpts(http.StatusRequestHeaderFieldsTooLarge) ErrInternalServerError = NewErrorOpts(http.StatusInternalServerError) )
var (
ErrUnsupported = errors.New("unsupported")
)
Functions ¶
func DecodeBody ¶
func HasFlush ¶
func HasFlush(w http.ResponseWriter) bool
func HasHijack ¶
func HasHijack(w http.ResponseWriter) bool
func HasReadFrom ¶
func HasReadFrom(w http.ResponseWriter) bool
func ListenAndServe ¶
func ListenAndServe(s HTTPServer, address string) error
func StartJSONResponse ¶
func StartJSONResponse(w http.ResponseWriter, statusCode int)
func WrapFn ¶
func WrapFn(f HandlerFn) http.HandlerFunc
WrapFn wraps a function as an http.HandlerFunc.
func WrapHandler ¶
WrapHandler wraps a raizu.Handler as http.Handler.
func WriteError ¶
func WriteError(w http.ResponseWriter, err *Error)
func WriteOK ¶
func WriteOK(w http.ResponseWriter) error
func WriteOKMessage ¶
func WriteOKMessage(w http.ResponseWriter, msg string) error
Types ¶
type AppMux ¶
type AppMux[MT MounterHandler] struct { // contains filtered or unexported fields }
func NewAppMux ¶
func NewAppMux[MT MounterHandler](cfg AppMuxConfig[MT]) (*AppMux[MT], error)
type AppMuxConfig ¶
type AppMuxConfig[MT MounterHandler] struct { Prefix string NewMuxFn func() MT Blueprints []Blueprint }
type Blueprint ¶
type Blueprint interface {
NewHandlerProvider() (HandlerProvider, error)
}
func AppMuxBlueprint ¶
func AppMuxBlueprint[MT MounterHandler](cfg AppMuxConfig[MT]) Blueprint
func HandlerAppBlueprint ¶ added in v0.2.0
func HandlerAppBlueprint(cfg HandlerAppConfig) Blueprint
func NewBlueprint ¶
func NewBlueprint[HPT HandlerProvider, CT any](makeFn func(cf CT) (HPT, error), cf CT) Blueprint
type BlueprintImpl ¶
type BlueprintImpl[HPT HandlerProvider, CT any] struct { // contains filtered or unexported fields }
func (BlueprintImpl[HPT, CT]) NewHandlerProvider ¶
func (bp BlueprintImpl[HPT, CT]) NewHandlerProvider() (HandlerProvider, error)
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func NewErrorOpts ¶
func NewErrorOpts(status int, opts ...ErrorOption) (e *Error)
type ErrorOption ¶
type ErrorOption func(e *Error)
func WithInner ¶
func WithInner(err error) ErrorOption
func WithMessage ¶
func WithMessage(msg string) ErrorOption
type HTTPHandler ¶
type HTTPHandler struct{ Handler }
HTTPHandler wraps a raizu.Handler as http.Handler.
Use the WrapHandler constructor function as a shorthand for wrapping a raizu.Handler as an http.Handler.
func (HTTPHandler) ServeHTTP ¶
func (h HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type HTTPServer ¶
type HTTPServer interface { // Close immediately closes all active [net.Listener] instances and // any connections in state [http.StateNew], [http.StateActive], or [http.StateIdle]. For a // graceful shutdown, use Shutdown. // // See [http.Server.Close] for more details. Close() error // Shutdown gracefully shuts down the server without interrupting any // active connections. // // See [http.Server.Shutdown] for more details. Shutdown(ctx context.Context) error // Serve starts accepting incoming connections on the [net.Listener] in l, creating a // new service goroutine for each. The service goroutines read requests and // then is expected to call srv.Handler of the corresponding NewBlueprint to reply to them. // // See [http.Server.Serve] for more details. Serve(l net.Listener) error }
A HTTPServer represents an HTTP server.
func NewServer ¶
func NewServer(config ServerConfig) (HTTPServer, error)
NewServer creates a new raizu.HTTPServer based on the raizu.ServerConfig provided.
All Blueprints provided in raizu.ServerConfig.Blueprints will be instantiated and mounted on the server. If any raizu.Blueprint instance fails, the whole function fails and returns an error instead.
type Handler ¶
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request) error
}
A Handler responds to an HTTP request.
ServeHTTP should write reply headers and data to the ResponseWriter and then return nil to indicate that the request was successfully processed or return an error if handling the request failed for whatever reason.
All other notes from http.Handler apply here as well.
type HandlerApp ¶ added in v0.2.0
type HandlerApp struct {
// contains filtered or unexported fields
}
func NewHandlerApp ¶ added in v0.2.0
func NewHandlerApp(cfg HandlerAppConfig) (*HandlerApp, error)
func (*HandlerApp) Handler ¶ added in v0.2.0
func (a *HandlerApp) Handler() http.Handler
func (*HandlerApp) Prefix ¶ added in v0.2.0
func (a *HandlerApp) Prefix() string
type HandlerAppConfig ¶ added in v0.2.0
type HandlerFn ¶
type HandlerFn func(http.ResponseWriter, *http.Request) error
HandlerFn is an adapter to allow the use of ordinary functions as raizu.Handler handlers. If f is a function with the appropriate signature, HandlerFn(f) is a raizu.Handler that calls f.
type HandlerProvider ¶
type Mounter ¶
Mounter is anything a http.Handler can be mounted on.
type MounterHandler ¶
MounterHandler represents a http.Handler that can mount other http.Handler instances.
type ServerConfig ¶
type ServerConfig struct { // AccessLog will receive http access requests logs as Info // level logging and any error encountered during the handling // of http access requests. Logger logr.Logger // TLSConfig TLSConfig *tls.Config // ReadTimeout see [http.Server.ReadTimeout] ReadTimeout time.Duration // ReadTimeout see [http.Server.ReadHeaderTimeout] ReadHeaderTimeout time.Duration // ReadTimeout see [http.Server.WriteTimeout] WriteTimeout time.Duration // IdleTimeout see [http.Server.IdleTimeout] IdleTimeout time.Duration // Blueprint is a factory that produces an app which will // be served via the http server. Blueprint Blueprint }
ServerConfig describes the Server runtime configuration.
func NewServerConfigDefault ¶
func NewServerConfigDefault() ServerConfig
func (ServerConfig) WithBlueprint ¶
func (c ServerConfig) WithBlueprint(af Blueprint) ServerConfig
func (ServerConfig) WithIdleTimeout ¶
func (c ServerConfig) WithIdleTimeout(v time.Duration) ServerConfig
func (ServerConfig) WithLogger ¶
func (c ServerConfig) WithLogger(logger logr.Logger) ServerConfig
func (ServerConfig) WithReadHeaderTimeout ¶
func (c ServerConfig) WithReadHeaderTimeout(v time.Duration) ServerConfig
func (ServerConfig) WithReadTimeout ¶
func (c ServerConfig) WithReadTimeout(v time.Duration) ServerConfig
func (ServerConfig) WithTLSConfig ¶
func (c ServerConfig) WithTLSConfig(cfg *tls.Config) ServerConfig
func (ServerConfig) WithWriteTimeout ¶
func (c ServerConfig) WithWriteTimeout(v time.Duration) ServerConfig
type UnsupportedError ¶
type UnsupportedError struct {
// contains filtered or unexported fields
}
func (UnsupportedError) Error ¶
func (e UnsupportedError) Error() string
func (UnsupportedError) Unwrap ¶
func (e UnsupportedError) Unwrap() error