http

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain(handler http.Handler, middlewares ...Middleware) http.Handler

Chain applies middlewares to a handler. The first middleware in the list will be the outermost (executed first).

func ChainFunc

func ChainFunc(handler http.HandlerFunc, middlewares ...Middleware) http.Handler

ChainFunc is like Chain but accepts http.HandlerFunc.

func PathParam

func PathParam(r *http.Request, key string) string

PathParam extracts a URL path parameter from the request. This abstracts the underlying router implementation. Handlers should use this instead of directly calling chi.URLParam or r.PathValue.

func PrintRoutes

func PrintRoutes(w io.Writer, stats RouteStats, format string, filters RouteFilters)

PrintRoutes prints routes to the given writer in the specified format.

func QueryParam

func QueryParam(r *http.Request, key string) string

QueryParam extracts a URL query parameter from the request.

func QueryParamDefault

func QueryParamDefault(r *http.Request, key, defaultValue string) string

QueryParamDefault extracts a URL query parameter with a default value.

Types

type ChiOption

type ChiOption func(*chiRouter)

ChiOption is a function that configures the Chi router.

func WithChiMiddleware

func WithChiMiddleware() ChiOption

WithChiMiddleware adds Chi's built-in middleware.

func WithRequestID

func WithRequestID() ChiOption

WithRequestID adds Chi's request ID middleware.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware is a function that wraps an http.Handler. This follows the standard net/http middleware pattern.

type RouteFilters

type RouteFilters struct {
	Method string
	Path   string
	SortBy string
}

RouteFilters contains filter options for route listing.

type RouteInfo

type RouteInfo struct {
	Method      string
	Path        string
	Handler     string
	Middlewares []string
}

RouteInfo holds information about a registered route.

type RouteStats

type RouteStats struct {
	Total   int
	Methods map[string]int
	Routes  []RouteInfo
}

RouteStats holds route statistics.

func CollectRoutes

func CollectRoutes(router Router) RouteStats

CollectRoutes walks the router and collects all registered routes.

type Router

type Router interface {
	// HTTP method handlers with optional route-specific middleware.
	// Middleware is applied in order: first middleware wraps outermost.
	//
	// Example:
	//   r.GET("/", handler)                                    // No middleware
	//   r.GET("/", handler, authMiddleware)                    // With auth
	//   r.GET("/", handler, authMiddleware, loggingMiddleware) // Multiple
	GET(path string, handler http.HandlerFunc, middlewares ...Middleware)
	POST(path string, handler http.HandlerFunc, middlewares ...Middleware)
	PUT(path string, handler http.HandlerFunc, middlewares ...Middleware)
	PATCH(path string, handler http.HandlerFunc, middlewares ...Middleware)
	DELETE(path string, handler http.HandlerFunc, middlewares ...Middleware)

	// Group creates a new route group with prefix and optional middleware.
	// Group middleware applies to all routes within the group.
	Group(prefix string, fn func(Router), middlewares ...Middleware)

	// Use adds middleware to the router (applies to all subsequent routes)
	Use(middlewares ...Middleware)

	// With returns a new Router with the given middleware applied.
	// Prefer using inline middleware in route methods instead.
	With(middlewares ...Middleware) Router

	// Handler returns the http.Handler for use with http.Server
	Handler() http.Handler

	// Walk iterates over all registered routes.
	// The callback receives method, path, and handler for each route.
	Walk(fn func(method, path string, handler http.Handler) error) error
}

Router defines the interface for HTTP routing. This abstraction allows swapping the underlying router implementation (Chi, stdlib, Gin, etc.) without changing application code.

func NewChiRouter

func NewChiRouter() Router

NewChiRouter creates a new Router using Chi as the underlying implementation.

func NewChiRouterWithOptions

func NewChiRouterWithOptions(opts ...ChiOption) Router

NewChiRouterWithOptions creates a router with custom options.

type Server

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

Server represents the HTTP server.

func NewServer

func NewServer(cfg *config.Config, log *logger.Logger, opts ...ServerOption) *Server

NewServer creates a new HTTP server. By default, it uses Chi router. Use WithRouter option to change.

func (*Server) Config

func (s *Server) Config() *config.Config

Config returns the server configuration.

func (*Server) Logger

func (s *Server) Logger() *logger.Logger

Logger returns the server logger.

func (*Server) Router

func (s *Server) Router() Router

Router returns the router for registering handlers.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the HTTP server.

func (*Server) Start

func (s *Server) Start() error

Start starts the HTTP server.

type ServerOption

type ServerOption func(*Server)

ServerOption is a function that configures the server.

func WithRouter

func WithRouter(r Router) ServerOption

WithRouter sets a custom router implementation.

Directories

Path Synopsis
Package handler provides HTTP handlers for the API server.
Package handler provides HTTP handlers for the API server.
Package middleware provides HTTP middleware for the API server.
Package middleware provides HTTP middleware for the API server.
Package routes registers all HTTP routes for the API.
Package routes registers all HTTP routes for the API.

Jump to

Keyboard shortcuts

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