Documentation
¶
Index ¶
- Constants
- Variables
- func Bind[T any](r *http.Request, opts ...BindOption) (T, error)
- func Chain(h http.Handler, mws ...Middleware) http.Handler
- func ClientIP(r *http.Request, trusted []netip.Prefix) string
- func CorrelationID(ctx context.Context) string
- func Created(w http.ResponseWriter, v any)
- func Handle[Req, Res any](fn func(ctx context.Context, req Req) (Res, error), opts ...BindOption) http.Handler
- func IDs(ctx context.Context) (string, string)
- func JSON(w http.ResponseWriter, status int, v any)
- func LoggerFrom(ctx context.Context) *slog.Logger
- func NewClient(next http.RoundTripper) *http.Client
- func NoContent(w http.ResponseWriter)
- func OK(w http.ResponseWriter, v any)
- func PropagatingTransport(next http.RoundTripper) http.RoundTripper
- func Register(code Code, d Definition)
- func RequestID(ctx context.Context) string
- func SkipHealth(r *http.Request) bool
- func User(ctx context.Context) (types.User, error)
- func Validate(v any) error
- func Validator() *validator.Validate
- func WithIDs(ctx context.Context, requestID, correlationID string) context.Context
- func WithLogger(ctx context.Context, l *slog.Logger) context.Context
- func WriteError(w http.ResponseWriter, r *http.Request, err error)
- type App
- func (a *App) Draining() <-chan struct{}
- func (a *App) Group(prefix string, mws ...Middleware) *Group
- func (a *App) Handle(pattern string, h http.Handler)
- func (a *App) HandleFunc(pattern string, h HandlerFunc)
- func (a *App) Handler() http.Handler
- func (a *App) Logger() *slog.Logger
- func (a *App) Metrics() *monitoring.Metrics
- func (a *App) Mux() *http.ServeMux
- func (a *App) OnShutdown(fn func(context.Context) error)
- func (a *App) Prober() *monitoring.Prober
- func (a *App) Run(ctx context.Context, addr string) error
- func (a *App) Serve(ctx context.Context, ln net.Listener) error
- func (a *App) Server(addr string) *http.Server
- type BindOption
- type CORSOptions
- type Code
- type Definition
- type Envelope
- type Error
- func (e *Error) Error() string
- func (e *Error) Is(target error) bool
- func (e *Error) Unwrap() error
- func (e *Error) WithCustomerActions(a ...string) *Error
- func (e *Error) WithFields(f ...FieldError) *Error
- func (e *Error) WithMessage(msg string) *Error
- func (e *Error) WithSupportActions(a ...string) *Error
- func (e *Error) Wrap(cause error) *Error
- type ErrorReporter
- type FieldError
- type Group
- func (g *Group) Delete(path string, h http.Handler, mws ...Middleware)
- func (g *Group) Get(path string, h http.Handler, mws ...Middleware)
- func (g *Group) Group(prefix string, mws ...Middleware) *Group
- func (g *Group) Handle(method, path string, h http.Handler, mws ...Middleware)
- func (g *Group) Patch(path string, h http.Handler, mws ...Middleware)
- func (g *Group) Post(path string, h http.Handler, mws ...Middleware)
- func (g *Group) Put(path string, h http.Handler, mws ...Middleware)
- func (g *Group) Use(mws ...Middleware)
- type HandlerFunc
- type Middleware
- func AccessLog(skip func(*http.Request) bool) Middleware
- func BodyLimit(n int64) Middleware
- func CORS(o CORSOptions) Middleware
- func ClientInfo(trusted []netip.Prefix) Middleware
- func RateLimit(o RateLimitOptions) Middleware
- func Recover() Middleware
- func RequestIDs(log *slog.Logger) Middleware
- func RequireFeature(feature string) Middleware
- func RequireRole(roles ...string) Middleware
- func RequireUser() Middleware
- func SecurityHeaders(o SecurityHeadersOptions) Middleware
- func Timeout(d time.Duration) Middleware
- func WithErrorReporter(rep ErrorReporter) Middleware
- type Options
- type RateLimitOptions
- type SecurityHeadersOptions
Constants ¶
View Source
const ( HeaderCorrelationID = "X-Correlation-ID" HeaderRequestID = "X-Request-ID" )
Variables ¶
View Source
var ErrServerClosed = errors.New("server closed")
View Source
var LoopbackOnly = []netip.Prefix{netip.MustParsePrefix("127.0.0.0/8"), netip.MustParsePrefix("::1/128")}
Functions ¶
func CorrelationID ¶
func Created ¶
func Created(w http.ResponseWriter, v any)
func NoContent ¶
func NoContent(w http.ResponseWriter)
func OK ¶
func OK(w http.ResponseWriter, v any)
func PropagatingTransport ¶
func PropagatingTransport(next http.RoundTripper) http.RoundTripper
func Register ¶
func Register(code Code, d Definition)
func SkipHealth ¶
func WriteError ¶
func WriteError(w http.ResponseWriter, r *http.Request, err error)
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
func (*App) HandleFunc ¶
func (a *App) HandleFunc(pattern string, h HandlerFunc)
type BindOption ¶
type BindOption func(*bindOptions)
func AllowUnknownFields ¶
func AllowUnknownFields() BindOption
func MaxBodyBytes ¶
func MaxBodyBytes(n int64) BindOption
func NoBody ¶
func NoBody() BindOption
func SkipValidation ¶
func SkipValidation() BindOption
type CORSOptions ¶
type Code ¶
type Code string
const ( CodeBadRequest Code = "E400-1" CodeValidation Code = "E400-2" CodeInvalidJSON Code = "E400-3" CodeUnauthenticated Code = "E401-1" CodeSessionExpired Code = "E401-2" CodeForbidden Code = "E403-1" CodeNotFound Code = "E404-1" CodeRouteNotFound Code = "E404-2" CodeMethod Code = "E405" CodeTimeout Code = "E408" CodeConflict Code = "E409" CodeGone Code = "E410" CodeTooLarge Code = "E413" CodeMediaType Code = "E415" CodeRateLimited Code = "E429" CodeInternal Code = "E500-1" CodeBadGateway Code = "E502" CodeGatewayTimeout Code = "E504" )
type Definition ¶
type Definition struct {
Status int
Message string
CustomerActions []string
SupportActions []string
}
func Lookup ¶
func Lookup(code Code) (Definition, bool)
type Error ¶
type Error struct {
Status int `json:"-"`
Code Code `json:"code"`
Message string `json:"message"`
CustomerActions []string `json:"customerActions,omitempty"`
SupportActions []string `json:"supportActions,omitempty"`
Fields []FieldError `json:"fields,omitempty"`
// contains filtered or unexported fields
}
func (*Error) WithCustomerActions ¶
func (*Error) WithFields ¶
func (e *Error) WithFields(f ...FieldError) *Error
func (*Error) WithMessage ¶
func (*Error) WithSupportActions ¶
type FieldError ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
func (*Group) Handle ¶
func (g *Group) Handle(method, path string, h http.Handler, mws ...Middleware)
func (*Group) Use ¶
func (g *Group) Use(mws ...Middleware)
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
func (HandlerFunc) ServeHTTP ¶
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Middleware ¶
func BodyLimit ¶
func BodyLimit(n int64) Middleware
func CORS ¶
func CORS(o CORSOptions) Middleware
func ClientInfo ¶ added in v0.4.0
func ClientInfo(trusted []netip.Prefix) Middleware
func RateLimit ¶
func RateLimit(o RateLimitOptions) Middleware
func Recover ¶
func Recover() Middleware
func RequestIDs ¶
func RequestIDs(log *slog.Logger) Middleware
func RequireFeature ¶ added in v0.2.0
func RequireFeature(feature string) Middleware
func RequireRole ¶ added in v0.2.0
func RequireRole(roles ...string) Middleware
func RequireUser ¶ added in v0.2.0
func RequireUser() Middleware
func SecurityHeaders ¶
func SecurityHeaders(o SecurityHeadersOptions) Middleware
func Timeout ¶
func Timeout(d time.Duration) Middleware
func WithErrorReporter ¶
func WithErrorReporter(rep ErrorReporter) Middleware
type Options ¶
type Options struct {
Name string
Environment string
Logger *slog.Logger
Prober *monitoring.Prober
Metrics *monitoring.Metrics
RequestTimeout time.Duration
ShutdownTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxBodyBytes int64
CORS *CORSOptions
RateLimit *RateLimitOptions
Security SecurityHeadersOptions
TrustedProxies []netip.Prefix
ErrorReporter ErrorReporter
Middleware []Middleware
StaticDir string
StaticPrefix string
}
type RateLimitOptions ¶
type SecurityHeadersOptions ¶
Click to show internal directories.
Click to hide internal directories.