middleware

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: MIT Imports: 9 Imported by: 0

README

Middleware

This package provides pre-configured middleware chains for common web application patterns.

Usage

Public routes (RequestId + AccessLog):

handler := middleware.PublicRoute(myHandler)
router.Handle("GET /api/health", handler)

Private routes (Auth + RequestId + AccessLog):

authParams := middleware.NewAuthParams(jwtSecret, userRepo)
handler := middleware.PrivateRoute(myHandler, authParams)
router.Handle("GET /api/users", handler)

Complete example:

router := http.NewServeMux()
authParams := middleware.NewAuthParams("jwt-secret", userRepository)

// Public endpoints
router.Handle("GET /health", middleware.PublicRoute(healthHandler))
router.Handle("POST /login", middleware.PublicRoute(loginHandler))

// Private endpoints  
router.Handle("GET /users", middleware.PrivateRoute(getUsersHandler, authParams))
router.Handle("POST /users", middleware.PrivateRoute(createUserHandler, authParams))

Authentication

The Auth middleware requires JWT tokens in the Authorization header:

Authorization: Bearer <jwt-token>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLogMiddleware

func AccessLogMiddleware(next http.Handler) http.Handler

func Auth added in v0.12.0

func Auth(next http.Handler, params AuthParams) http.Handler

func PrivateRoute added in v0.13.0

func PrivateRoute(endpoint http.Handler, authParams AuthParams) http.Handler

func PublicRoute added in v0.13.0

func PublicRoute(endpoint http.Handler) http.Handler

func RequestIdMiddleware added in v0.12.0

func RequestIdMiddleware(next http.Handler) http.Handler

Types

type AuthParams added in v0.12.0

type AuthParams struct {
	Secret   string
	UserRepo auth.AuthUserRepository
}

func NewAuthParams added in v0.12.0

func NewAuthParams(secret string, userRepo auth.AuthUserRepository) AuthParams

Jump to

Keyboard shortcuts

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