Documentation
¶
Index ¶
- func PathParam(ctx context.Context, name string) (string, bool)
- func PathParams(ctx context.Context) map[string]string
- func RegisterHandler[Req, Res any](eg *EndpointGroup, in Endpoint[Req, Res], opts ...RegisterOption[Req, Res])
- func RegisterHandlerM[Req, Meta, Res any](eg *EndpointGroup, in EndpointWithMeta[Req, Meta, Res], ...)
- type Codec
- type DefaultCodec
- func (c DefaultCodec[Req, Res]) Consumes() []string
- func (c DefaultCodec[Req, Res]) DecodeBody(r *http.Request) (Req, error)
- func (c DefaultCodec[Req, Res]) DecodeQuery(r *http.Request) (Req, error)
- func (c DefaultCodec[Req, Res]) Encode(w http.ResponseWriter, res Res) error
- func (c DefaultCodec[Req, Res]) EncodeError(w http.ResponseWriter, err error) error
- func (c DefaultCodec[Req, Res]) Produces() []string
- type Endpoint
- func DELETE[Req, Res any](handler Handler[Req, Res], path string) Endpoint[Req, Res]
- func GET[Req, Res any](handler Handler[Req, Res], path string) Endpoint[Req, Res]
- func HEAD[Req, Res any](handler Handler[Req, Res], path string) Endpoint[Req, Res]
- func OPTIONS[Req, Res any](handler Handler[Req, Res], path string) Endpoint[Req, Res]
- func PATCH[Req, Res any](handler Handler[Req, Res], path string) Endpoint[Req, Res]
- func POST[Req, Res any](handler Handler[Req, Res], path string) Endpoint[Req, Res]
- func PUT[Req, Res any](handler Handler[Req, Res], path string) Endpoint[Req, Res]
- type EndpointDescription
- type EndpointGroup
- type EndpointMeta
- type EndpointWithMeta
- func DELETEM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
- func GETM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
- func HEADM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
- func OPTIONSM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
- func PATCHM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
- func POSTM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
- func PUTM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
- type Handler
- type HandlerMiddleware
- type HandlerWithMeta
- type HandlerWithMetaMiddleware
- type Middleware
- type MiddlewareOption
- type MiddlewareWithPriority
- type RegisterOption
- type RegisterOptionWithMeta
- func WithCodecWithMeta[Req, Meta, Res any](codec Codec[Req, Res]) RegisterOptionWithMeta[Req, Meta, Res]
- func WithMetaMiddleware[Req, Meta, Res any](middleware HandlerWithMetaMiddleware[Req, Meta, Res]) RegisterOptionWithMeta[Req, Meta, Res]
- func WithMetaMiddlewares[Req, Meta, Res any](middlewares ...HandlerWithMetaMiddleware[Req, Meta, Res]) RegisterOptionWithMeta[Req, Meta, Res]
- type Router
- func (r *Router) Describe() []EndpointDescription
- func (r *Router) GenTS(w io.Writer, opts TSGenOptions) error
- func (r *Router) GenTSDir(dir string, opts TSGenOptions) error
- func (r *Router) GenerateTSClient() error
- func (r *Router) Handler() (http.Handler, error)
- func (r *Router) HandlerMust() http.Handler
- func (r *Router) RunServer(addr string, opts ...RunServerOption) error
- func (r *Router) Server(addr string, opts ...ServerOption) *http.Server
- func (r *Router) SetFallback(h http.Handler)
- func (r *Router) SetTSClientGenConfig(cfg *TSClientGenConfig)
- func (r *Router) Use(middleware Middleware, middlewareOpts ...MiddlewareOption)
- type RunServerOption
- type ServerOption
- type StatusError
- type TSClientGenConfig
- type TSGenOptions
- type TypeRef
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PathParams ¶
PathParams returns a copy of all path params.
func RegisterHandler ¶
func RegisterHandler[Req, Res any](eg *EndpointGroup, in Endpoint[Req, Res], opts ...RegisterOption[Req, Res])
RegisterHandler registers an endpoint with the endpoint group.
func RegisterHandlerM ¶
func RegisterHandlerM[Req, Meta, Res any](eg *EndpointGroup, in EndpointWithMeta[Req, Meta, Res], opts ...RegisterOptionWithMeta[Req, Meta, Res])
RegisterHandlerM registers an endpoint with typed metadata.
Types ¶
type Codec ¶
type Codec[Req any, Res any] interface { DecodeBody(*http.Request) (Req, error) DecodeQuery(*http.Request) (Req, error) Encode(http.ResponseWriter, Res) error EncodeError(http.ResponseWriter, error) error }
Codec defines the interface for encoding and decoding HTTP requests and responses.
type DefaultCodec ¶
DefaultCodec implements Codec using JSON for bodies and structured query decoding.
func (DefaultCodec[Req, Res]) Consumes ¶
func (c DefaultCodec[Req, Res]) Consumes() []string
Consumes returns the content types this codec can decode.
func (DefaultCodec[Req, Res]) DecodeBody ¶
func (c DefaultCodec[Req, Res]) DecodeBody(r *http.Request) (Req, error)
DecodeBody decodes the request body into the request type using JSON.
func (DefaultCodec[Req, Res]) DecodeQuery ¶
func (c DefaultCodec[Req, Res]) DecodeQuery(r *http.Request) (Req, error)
DecodeQuery decodes query parameters into the request type.
func (DefaultCodec[Req, Res]) Encode ¶
func (c DefaultCodec[Req, Res]) Encode(w http.ResponseWriter, res Res) error
Encode encodes the response into the HTTP response writer.
func (DefaultCodec[Req, Res]) EncodeError ¶
func (c DefaultCodec[Req, Res]) EncodeError(w http.ResponseWriter, err error) error
EncodeError encodes an error into the HTTP response writer.
func (DefaultCodec[Req, Res]) Produces ¶
func (c DefaultCodec[Req, Res]) Produces() []string
Produces returns the content types this codec can encode.
type Endpoint ¶
Endpoint represents an HTTP endpoint with a handler, path, and method.
type EndpointDescription ¶
type EndpointDescription struct {
Method string
Path string
Req TypeRef
Meta TypeRef
Res TypeRef
Consumes []string
Produces []string
}
EndpointDescription describes an endpoint for TypeScript generation.
type EndpointGroup ¶
type EndpointGroup struct {
Prefix string
Handlers []*endpoint
Middlewares []*MiddlewareWithPriority
Metas []*EndpointMeta
// contains filtered or unexported fields
}
EndpointGroup groups endpoints with a common prefix and middlewares.
func (*EndpointGroup) Group ¶
func (eg *EndpointGroup) Group(prefix string) *EndpointGroup
Group creates a subgroup with the given prefix.
func (*EndpointGroup) Use ¶
func (eg *EndpointGroup) Use(middleware Middleware, middlewareOpts ...MiddlewareOption)
Use adds a middleware to this group (and its sub-groups).
type EndpointMeta ¶
type EndpointMeta struct {
Method string
Path string
Req reflect.Type
Meta reflect.Type
Res reflect.Type
Consumes []string
Produces []string
}
EndpointMeta contains metadata about an endpoint.
type EndpointWithMeta ¶
type EndpointWithMeta[Req, Meta, Res any] struct { Handler HandlerWithMeta[Req, Meta, Res] Path string Method string }
EndpointWithMeta represents an HTTP endpoint with typed request metadata.
func DELETEM ¶
func DELETEM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
DELETEM creates an EndpointWithMeta for HTTP DELETE requests.
func GETM ¶
func GETM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
GETM creates an EndpointWithMeta for HTTP GET requests.
func HEADM ¶
func HEADM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
HEADM creates an EndpointWithMeta for HTTP HEAD requests.
func OPTIONSM ¶
func OPTIONSM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
OPTIONSM creates an EndpointWithMeta for HTTP OPTIONS requests.
func PATCHM ¶
func PATCHM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
PATCHM creates an EndpointWithMeta for HTTP PATCH requests.
func POSTM ¶
func POSTM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
POSTM creates an EndpointWithMeta for HTTP POST requests.
func PUTM ¶
func PUTM[Req, Meta, Res any](handler HandlerWithMeta[Req, Meta, Res], path string) EndpointWithMeta[Req, Meta, Res]
PUTM creates an EndpointWithMeta for HTTP PUT requests.
type HandlerMiddleware ¶
HandlerMiddleware is a middleware function for typed handlers.
type HandlerWithMeta ¶
type HandlerWithMeta[Req, Meta, Res any] func(ctx context.Context, request Req, meta Meta) (Res, error)
HandlerWithMeta is a handler that receives request metadata decoded from path/header tags.
type HandlerWithMetaMiddleware ¶
type HandlerWithMetaMiddleware[Req, Meta, Res any] func(next HandlerWithMeta[Req, Meta, Res]) HandlerWithMeta[Req, Meta, Res]
HandlerWithMetaMiddleware is a middleware function for typed handlers with metadata.
type Middleware ¶
Middleware is a function that wraps an http.Handler.
type MiddlewareOption ¶
type MiddlewareOption interface {
// contains filtered or unexported methods
}
MiddlewareOption configures middleware options.
func Priority ¶
func Priority(priority int) MiddlewareOption
Priority sets middleware ordering. Higher priority runs earlier (wraps outer). For equal priority, later-registered middleware wraps outer.
type MiddlewareWithPriority ¶
type MiddlewareWithPriority struct {
Middleware Middleware
Priority int
}
MiddlewareWithPriority associates a middleware with a priority level.
type RegisterOption ¶
type RegisterOption[Req, Res any] interface { // contains filtered or unexported methods }
RegisterOption configures options for registering a handler.
func WithCodec ¶
func WithCodec[Req, Res any](codec Codec[Req, Res]) RegisterOption[Req, Res]
WithCodec sets the codec for the handler.
func WithMiddleware ¶
func WithMiddleware[Req, Res any](middleware HandlerMiddleware[Req, Res]) RegisterOption[Req, Res]
WithMiddleware adds a middleware to the handler.
func WithMiddlewares ¶
func WithMiddlewares[Req, Res any](middlewares ...HandlerMiddleware[Req, Res]) RegisterOption[Req, Res]
WithMiddlewares adds multiple middlewares to the handler.
type RegisterOptionWithMeta ¶
type RegisterOptionWithMeta[Req, Meta, Res any] interface { // contains filtered or unexported methods }
RegisterOptionWithMeta configures options for registering a handler with metadata.
func WithCodecWithMeta ¶
func WithCodecWithMeta[Req, Meta, Res any](codec Codec[Req, Res]) RegisterOptionWithMeta[Req, Meta, Res]
WithCodecWithMeta sets the codec for the handler with metadata.
func WithMetaMiddleware ¶
func WithMetaMiddleware[Req, Meta, Res any](middleware HandlerWithMetaMiddleware[Req, Meta, Res]) RegisterOptionWithMeta[Req, Meta, Res]
WithMetaMiddleware adds a middleware to the handler with metadata.
func WithMetaMiddlewares ¶
func WithMetaMiddlewares[Req, Meta, Res any](middlewares ...HandlerWithMetaMiddleware[Req, Meta, Res]) RegisterOptionWithMeta[Req, Meta, Res]
WithMetaMiddlewares adds multiple middlewares to the handler with metadata.
type Router ¶
type Router struct {
*EndpointGroup
// contains filtered or unexported fields
}
Router is the main router for handling HTTP requests.
func (*Router) Describe ¶
func (r *Router) Describe() []EndpointDescription
Describe returns endpoint metadata suitable for generators.
func (*Router) GenTS ¶
func (r *Router) GenTS(w io.Writer, opts TSGenOptions) error
GenTS writes a TypeScript client based on registered endpoint metadata.
Intended usage with go:generate:
//go:generate go run ./cmd/gen
where ./cmd/gen constructs your router and calls router.GenTS(...).
func (*Router) GenTSDir ¶
func (r *Router) GenTSDir(dir string, opts TSGenOptions) error
GenTSDir writes a multi-file TypeScript client into dir, split by path segment. It overwrites the generated files it creates.
func (*Router) GenerateTSClient ¶
GenerateTSClient generates the TypeScript client using the configured TSClientGenConfig. No-op if no config is set. Errors are returned to the caller, and also passed to cfg.OnError if provided.
func (*Router) Handler ¶
Handler builds and returns an http.Handler for the registered endpoints. It returns an error if routes are invalid (e.g., duplicate method+path).
func (*Router) HandlerMust ¶
HandlerMust returns the handler or panics if building the handler fails.
func (*Router) RunServer ¶
func (r *Router) RunServer(addr string, opts ...RunServerOption) error
RunServer runs the HTTP server. By default, it enables graceful shutdown with a 30-second timeout. It blocks until the server is shut down.
Options:
- WithGracefulShutdown(timeout): Enable graceful shutdown (default: enabled with 30s timeout)
- WithLogger(logger): Custom logger for server events (default: slog.Default())
Example:
// Simple usage with defaults (graceful shutdown enabled)
r.RunServer(":8080")
// Custom timeout
r.RunServer(":8080", httprpc.WithGracefulShutdown(60*time.Second))
// Custom logger
r.RunServer(":8080", httprpc.WithLogger(myLogger))
func (*Router) Server ¶
func (r *Router) Server(addr string, opts ...ServerOption) *http.Server
Server returns a configured http.Server using Router.Handler().
func (*Router) SetFallback ¶
SetFallback sets a handler that is invoked when no registered endpoint matches the request path. Middlewares registered on the root group still apply to the fallback.
func (*Router) SetTSClientGenConfig ¶
func (r *Router) SetTSClientGenConfig(cfg *TSClientGenConfig)
SetTSClientGenConfig sets the configuration for TypeScript client generation.
func (*Router) Use ¶
func (r *Router) Use(middleware Middleware, middlewareOpts ...MiddlewareOption)
Use adds a middleware to the router. Priority -> Higher means earlier execution.
type RunServerOption ¶
type RunServerOption interface {
// contains filtered or unexported methods
}
RunServerOption configures RunServer behavior.
func WithGracefulShutdown ¶
func WithGracefulShutdown(timeout time.Duration) RunServerOption
WithGracefulShutdown enables graceful shutdown on SIGINT/SIGTERM signals. This is the default behavior. Use this option to explicitly enable it or to customize the shutdown timeout.
func WithLogger ¶
func WithLogger(logger *slog.Logger) RunServerOption
WithLogger sets a custom logger for server lifecycle events. If not provided, slog.Default() is used.
type ServerOption ¶
type ServerOption interface {
// contains filtered or unexported methods
}
ServerOption configures server options.
func IdleTimeout ¶
func IdleTimeout(d time.Duration) ServerOption
IdleTimeout sets the IdleTimeout for the server.
func MaxHeaderBytes ¶
func MaxHeaderBytes(n int) ServerOption
MaxHeaderBytes sets the MaxHeaderBytes for the server.
func ReadHeaderTimeout ¶
func ReadHeaderTimeout(d time.Duration) ServerOption
ReadHeaderTimeout sets the ReadHeaderTimeout for the server.
func ReadTimeout ¶
func ReadTimeout(d time.Duration) ServerOption
ReadTimeout sets the ReadTimeout for the server.
func WriteTimeout ¶
func WriteTimeout(d time.Duration) ServerOption
WriteTimeout sets the WriteTimeout for the server.
type StatusError ¶
StatusError marks an error with an HTTP status code for encoding. It can be returned by handlers or used internally (e.g. decode failures).
func (StatusError) Error ¶
func (e StatusError) Error() string
func (StatusError) Unwrap ¶
func (e StatusError) Unwrap() error
type TSClientGenConfig ¶
type TSClientGenConfig struct {
// Dir is the output directory for generated TypeScript.
// If empty, generation is disabled.
Dir string
// Options are passed through to GenTSDir.
Options TSGenOptions
// OnError is called if GenerateTSClient encounters an error.
// If nil, errors are only returned to the caller.
OnError func(error)
}
TSClientGenConfig configures TypeScript client generation.
type TSGenOptions ¶
type TSGenOptions struct {
PackageName string
ClientName string
// SkipPathSegments skips leading path segments when choosing a module file.
// Example: for "/v1/users/list" and SkipPathSegments=1, the module is "users".
SkipPathSegments int
}
TSGenOptions configures TypeScript generation.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package middleware provides HTTP middleware utilities for the httprpc framework.
|
Package middleware provides HTTP middleware utilities for the httprpc framework. |