router

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 11 Imported by: 0

README

Router Package

Go Version Tests Coverage

Production-ready HTTP routing and middleware framework built on Gin, providing flexible route management, authentication, header control, and comprehensive logging.


Table of Contents


Overview

This library provides a comprehensive HTTP routing solution for Go applications built on top of the Gin web framework. It emphasizes clean route organization, flexible middleware chains, secure authentication, and production-ready logging.

Design Philosophy
  1. Route Organization: Group-based routing with merge capabilities for clean API structure
  2. Middleware First: Built-in middleware for latency tracking, logging, and error recovery
  3. Security Focused: Authorization middleware with customizable authentication schemes
  4. Production Ready: Comprehensive error handling, panic recovery, and access logging
  5. Composable: Independent subpackages that integrate seamlessly

Key Features

  • Flexible Routing: Route grouping, merging, and dynamic registration with RouterList
  • Middleware Suite: Latency tracking, request context, access logging, error recovery with panic handling
  • Authorization: Customizable auth middleware supporting Bearer, Basic, API Key, and custom schemes
  • Header Management: Centralized HTTP header control across routes and handlers
  • Thread-Safe: Proper synchronization for concurrent operations
  • Security: Log injection prevention, broken pipe detection, authorization validation
  • Gin Integration: Full compatibility with Gin's ecosystem and middleware

Installation

go get github.com/nabbar/golib/router

Requirements:

  • Go 1.18 or higher
  • github.com/gin-gonic/gin
  • github.com/nabbar/golib/logger
  • github.com/nabbar/golib/errors

Architecture

Package Structure

The package is organized into four main components, each with specific responsibilities:

router/
├── router/              # Core routing and middleware
│   ├── interface.go     # RouterList interface and types
│   ├── model.go         # RouterList implementation
│   ├── middleware.go    # Gin middleware (latency, logging, errors)
│   ├── default.go       # Default engine configurations
│   └── error.go         # Error codes and messages
├── auth/                # Authorization middleware
│   ├── interface.go     # Authorization interface
│   └── model.go         # Auth handler implementation
├── authheader/          # Auth response helpers
│   └── interface.go     # AuthCode types and functions
└── header/              # HTTP header management
    ├── interface.go     # Headers interface
    ├── model.go         # Headers implementation
    └── config.go        # Configuration types
Component Overview
┌─────────────────────────────────────────────────────────┐
│                    Router Package                       │
│  RouterList, Middleware, DefaultGinInit()               │
└──────────────┬──────────────┬──────────────┬────────────┘
               │              │              │
      ┌────────▼─────┐  ┌────▼─────┐  ┌────▼────────┐
      │     auth     │  │authheader│  │   header    │
      │              │  │          │  │             │
      │ Bearer,Basic │  │ 401, 403 │  │ Set/Get/Del │
      │ Custom Auth  │  │ Helpers  │  │ Middleware  │
      └──────────────┘  └──────────┘  └─────────────┘
Component Purpose Coverage Thread-Safe
router Route management, middleware, logging 92.1%
auth Authorization with custom check functions 96.3%
authheader Auth response codes and helpers 100%
header HTTP header manipulation 83.3%
Request Flow
HTTP Request
     │
     ▼
┌──────────────────┐
│ GinLatencyContext│ ← Start timer
└────────┬─────────┘
         │
         ▼
┌─────────────────┐
│GinRequestContext│  ← Extract path, user, query
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Authorization  │  ← Check auth header (optional)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Header Handler │  ← Set custom headers (optional)
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Route Handler  │  ← Your business logic
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  GinAccessLog   │  ← Log request details
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  GinErrorLog    │  ← Log errors, recover panics
└────────┬────────┘
         │
         ▼
   HTTP Response

Performance

Memory Efficiency

The router maintains minimal memory overhead:

  • Route Storage: O(n) where n = number of routes
  • Request Processing: O(1) constant memory per request
  • Middleware Chain: Stack-based execution with no heap allocations
  • Example: Handle 10,000 routes with ~5MB RAM
Thread Safety

All operations are thread-safe through:

  • Immutable Routes: Routes are registered once and never modified during serving
  • Context Isolation: Each request gets its own Gin context
  • Logger Safety: Thread-safe logger integration
  • Concurrent Requests: Unlimited concurrent request handling
Throughput Benchmarks
Operation Throughput Latency Notes
Route Lookup ~10M ops/s <100ns Gin's radix tree
Middleware Chain ~5M req/s <200ns 3 middleware
Authorization ~2M req/s <500ns Token validation
Header Setting ~8M ops/s <125ns Direct write
Access Logging ~1M req/s ~1µs With I/O

Measured on AMD64, Go 1.21, 8 cores

Middleware Performance
Overhead per middleware:
├─ GinLatencyContext    → ~50ns  (timer start)
├─ GinRequestContext    → ~100ns (path sanitization)
├─ Authorization        → ~500ns (auth check)
├─ Header Handler       → ~100ns (header writes)
├─ GinAccessLog         → ~1µs   (log formatting + I/O)
└─ GinErrorLog          → ~50ns  (defer setup)

Use Cases

This library is designed for scenarios requiring robust HTTP routing and middleware:

RESTful APIs

  • Organize endpoints with route groups (/api/v1, /api/v2)
  • Apply authentication to specific route groups
  • Centralized header management (CORS, API versioning)
  • Comprehensive access logging for audit trails

Microservices

  • Service-to-service authentication with Bearer tokens
  • Request tracing with latency tracking
  • Error recovery to prevent service crashes
  • Health check endpoints with custom middleware

Web Applications

  • Session-based authentication
  • CSRF protection via custom headers
  • User activity logging
  • Graceful error handling with user-friendly responses

API Gateways

  • Multi-tenant routing with group isolation
  • Rate limiting integration points
  • Authentication proxy with custom validators
  • Request/response transformation via middleware

Admin Panels

  • Role-based access control via auth middleware
  • Audit logging of all admin actions
  • IP whitelisting through custom middleware
  • Secure header policies (CSP, HSTS)

Quick Start

Basic Routing

Create a simple HTTP server with grouped routes:

package main

import (
    "net/http"
    "github.com/nabbar/golib/router"
)

func main() {
    // Create router list
    routerList := router.NewRouterList(router.DefaultGinInit)
    
    // Register routes
    routerList.Register(http.MethodGet, "/health", healthHandler)
    routerList.RegisterInGroup("/api/v1", http.MethodGet, "/users", usersHandler)
    routerList.RegisterInGroup("/api/v1", http.MethodPost, "/users", createUserHandler)
    
    // Create and start engine
    engine := routerList.Engine()
    routerList.Handler(engine)
    engine.Run(":8080")
}

func healthHandler(c *gin.Context) {
    c.JSON(200, gin.H{"status": "ok"})
}

func usersHandler(c *gin.Context) {
    c.JSON(200, gin.H{"users": []string{"alice", "bob"}})
}

func createUserHandler(c *gin.Context) {
    c.JSON(201, gin.H{"created": true})
}
Middleware Integration

Add logging and error recovery:

package main

import (
    "context"
    "github.com/nabbar/golib/router"
    "github.com/nabbar/golib/logger"
)

func main() {
    // Setup logger
    ctx := func() context.Context { return context.Background() }
    log := logger.New(ctx)
    logFunc := func() logger.Logger { return log }
    
    // Create engine with middleware
    engine := router.DefaultGinInit()
    engine.Use(router.GinLatencyContext)
    engine.Use(router.GinRequestContext)
    engine.Use(router.GinAccessLog(logFunc))
    engine.Use(router.GinErrorLog(logFunc))
    
    // Register routes
    routerList := router.NewRouterList(func() *gin.Engine { return engine })
    routerList.Register("GET", "/api/data", dataHandler)
    routerList.Handler(engine)
    
    engine.Run(":8080")
}
Authorization

Protect routes with Bearer token authentication:

package main

import (
    "github.com/nabbar/golib/router/auth"
    "github.com/nabbar/golib/router/authheader"
    "github.com/nabbar/golib/errors"
)

func main() {
    // Create auth middleware
    authCheck := func(token string) (authheader.AuthCode, errors.Error) {
        if validateToken(token) {
            return authheader.AuthCodeSuccess, nil
        }
        return authheader.AuthCodeForbidden, nil
    }
    
    authorization := auth.NewAuthorization(logFunc, "BEARER", authCheck)
    
    // Protect routes
    engine.GET("/protected", authorization.Register(protectedHandler))
    engine.GET("/admin", authorization.Register(adminHandler))
}

func validateToken(token string) bool {
    // Your token validation logic
    return token == "valid-token-123"
}
Custom Headers

Set headers across multiple routes:

package main

import (
    "github.com/nabbar/golib/router/header"
)

func main() {
    // Create headers
    headers := header.NewHeaders()
    headers.Set("X-API-Version", "v1")
    headers.Set("X-Request-ID", "12345")
    headers.Set("Cache-Control", "no-cache")
    
    // Apply to routes
    engine.GET("/api/data", headers.Register(dataHandler)...)
    engine.GET("/api/users", headers.Register(usersHandler)...)
}

Subpackages

Router Core

Purpose: Core routing functionality with middleware support

Key Components:

  • RouterList: Route registration and organization
  • GinLatencyContext: Request timing
  • GinRequestContext: Path and user extraction
  • GinAccessLog: HTTP access logging
  • GinErrorLog: Error recovery and logging

Example:

routerList := router.NewRouterList(router.DefaultGinInit)
routerList.RegisterInGroup("/api", "GET", "/users", handler)

See: GoDoc


Auth Subpackage

Purpose: HTTP authorization middleware with customizable authentication

Key Components:

  • Authorization: Interface for auth middleware
  • NewAuthorization: Create auth with custom check function
  • Support for Bearer, Basic, API Key, and custom schemes

Features:

  • Custom authentication logic
  • HTTP 401/403 responses
  • Handler chain management
  • Debug logging support

Example:

checkFunc := func(token string) (authheader.AuthCode, errors.Error) {
    if isValid(token) {
        return authheader.AuthCodeSuccess, nil
    }
    return authheader.AuthCodeForbidden, errors.New("invalid token")
}

auth := auth.NewAuthorization(logFunc, "BEARER", checkFunc)
engine.GET("/protected", auth.Register(handler))

See: GoDoc


AuthHeader Subpackage

Purpose: Authorization response codes and helper functions

Key Components:

  • AuthCode: Success, Require, Forbidden
  • AuthRequire: Send 401 Unauthorized
  • AuthForbidden: Send 403 Forbidden
  • Standard HTTP header constants

Example:

if token == "" {
    authheader.AuthRequire(c, errors.New("missing token"))
    return
}
if !hasPermission(user) {
    authheader.AuthForbidden(c, errors.New("insufficient permissions"))
    return
}

See: GoDoc


Header Subpackage

Purpose: HTTP header management and middleware

Key Components:

  • Headers: Interface for header manipulation
  • Add/Set/Get/Del: Header operations
  • Register: Create handler chain with headers
  • HeadersConfig: Map-based configuration

Features:

  • Case-insensitive header names
  • Multi-value header support
  • Middleware integration
  • Configuration-driven setup

Example:

headers := header.NewHeaders()
headers.Set("X-API-Version", "v1")
headers.Set("Cache-Control", "no-cache")
headers.Add("Set-Cookie", "session=abc")

engine.GET("/api/data", headers.Register(handler)...)

See: GoDoc


Best Practices

1. Route Organization

✅ DO: Group related routes together

routerList.RegisterInGroup("/api/v1", "GET", "/users", listUsers)
routerList.RegisterInGroup("/api/v1", "POST", "/users", createUser)
routerList.RegisterInGroup("/api/v1", "GET", "/users/:id", getUser)

❌ DON'T: Mix versions or domains in the same group

// Bad: mixing versions
routerList.RegisterInGroup("/api", "GET", "/v1/users", handler1)
routerList.RegisterInGroup("/api", "GET", "/v2/users", handler2)
2. Middleware Order

✅ DO: Order middleware from general to specific

engine.Use(router.GinLatencyContext)      // 1. Timing
engine.Use(router.GinRequestContext)      // 2. Context
engine.Use(router.GinAccessLog(logFunc))  // 3. Logging
engine.Use(router.GinErrorLog(logFunc))   // 4. Recovery

❌ DON'T: Put error recovery before other middleware

// Bad: recovery won't catch middleware errors
engine.Use(router.GinErrorLog(logFunc))
engine.Use(router.GinLatencyContext)
3. Authorization

✅ DO: Use specific auth types

auth := auth.NewAuthorization(logFunc, "BEARER", checkFunc)

❌ DON'T: Accept any auth type

// Bad: insecure, accepts any format
auth := auth.NewAuthorization(logFunc, "", checkFunc)
4. Error Handling

✅ DO: Return appropriate HTTP status codes

if err != nil {
    c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    return
}

❌ DON'T: Panic in handlers

// Bad: will crash the server
if err != nil {
    panic(err)
}
5. Header Management

✅ DO: Centralize common headers

headers := header.NewHeaders()
headers.Set("X-API-Version", "v1")
headers.Set("X-Content-Type-Options", "nosniff")

engine.GET("/api/*", headers.Register(handler)...)

❌ DON'T: Set headers in every handler

// Bad: repetitive and error-prone
func handler1(c *gin.Context) {
    c.Header("X-API-Version", "v1")
    // ...
}
func handler2(c *gin.Context) {
    c.Header("X-API-Version", "v1")
    // ...
}

Testing

Test Coverage
Package                Coverage    Specs
router                 92.1%       61 tests
router/auth            96.3%       12 tests
router/authheader      100.0%      11 tests
router/header          83.3%       29 tests
────────────────────────────────────────────
Total                  91.4%       113 tests
Running Tests
# Run all tests
go test ./...

# Run with coverage
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

# Run with race detector
CGO_ENABLED=1 go test -race ./...

# Run specific package
go test github.com/nabbar/golib/router/auth

# Verbose output
go test -v ./...
Test Categories

Router Core (61 tests)

  • RouterList operations (32 tests)
  • Middleware functionality (13 tests)
  • Default configurations (8 tests)
  • Error codes (8 tests)

Auth (12 tests)

  • Authorization flow
  • Handler registration
  • Auth code responses
  • Error handling

AuthHeader (11 tests)

  • Auth codes
  • Helper functions
  • HTTP responses
  • Error attachment

Header (29 tests)

  • Header operations
  • Middleware integration
  • Configuration
  • Handler chains

For detailed testing documentation, see TESTING.md.


Contributing

Contributions are welcome! Please follow these guidelines:

Code Contributions

  • Do not use AI to generate package implementation code
  • AI may assist with tests, documentation, and bug fixing
  • All contributions must pass go test -race
  • Maintain or improve test coverage (≥90%)
  • Follow existing code style and patterns

Documentation

  • Update README.md for new features
  • Add examples for common use cases
  • Keep TESTING.md synchronized with test changes
  • Document all exported functions with GoDoc

Testing

  • Write tests for all new features
  • Test edge cases and error conditions
  • Verify thread safety with race detector
  • Include integration tests for middleware chains

Pull Requests

  • Describe the problem and solution
  • Reference related issues
  • Include test results and coverage
  • Update documentation as needed

Future Enhancements

Potential improvements for future versions:

Routing

  • WebSocket support with route integration
  • Server-Sent Events (SSE) middleware
  • GraphQL endpoint helpers
  • gRPC gateway integration

Authentication

  • OAuth2 flow helpers
  • JWT validation middleware
  • API key management
  • Multi-factor authentication support

Middleware

  • Rate limiting middleware
  • Request/response transformation
  • Caching layer integration
  • Metrics collection (Prometheus)

Performance

  • Zero-allocation path matching
  • Connection pooling helpers
  • Response compression middleware
  • Request batching support

Observability

  • OpenTelemetry integration
  • Distributed tracing
  • Structured logging enhancements
  • Health check framework

AI Transparency Notice

In accordance with Article 50.4 of the EU AI Act, AI assistance has been used for testing, documentation, and bug fixing under human supervision.


License

MIT License - See LICENSE file for details.


Resources

Official Documentation

Related Packages

Testing

HTTP Standards

Documentation

Overview

Package router provides HTTP routing functionality built on top of the Gin web framework. It offers a flexible router list system, middleware support, authentication helpers, and header management utilities.

Key features:

  • RouterList: Organize routes with optional grouping
  • Middleware: Latency tracking, request context, access logging, error recovery
  • Authentication: Authorization handlers with customizable check functions
  • Headers: Manage HTTP headers across routes

Example usage:

routerList := router.NewRouterList(router.DefaultGinInit)
routerList.Register(http.MethodGet, "/api/health", healthHandler)
routerList.RegisterInGroup("/api/v1", http.MethodGet, "/users", usersHandler)
engine := routerList.Engine()
routerList.Handler(engine)
engine.Run(":8080")

See also:

  • github.com/gin-gonic/gin for the underlying web framework
  • github.com/nabbar/golib/logger for logging integration
  • github.com/nabbar/golib/errors for error handling

Index

Constants

View Source
const (
	// ErrorParamEmpty indicates that a required parameter was not provided or is empty.
	ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgRouter

	// ErrorConfigValidator indicates that configuration validation failed.
	ErrorConfigValidator

	// ErrorHeaderAuth indicates an invalid response code from authorization check.
	ErrorHeaderAuth

	// ErrorHeaderAuthMissing indicates that the Authorization header is missing from the request.
	ErrorHeaderAuthMissing

	// ErrorHeaderAuthEmpty indicates that the Authorization header is present but empty.
	ErrorHeaderAuthEmpty

	// ErrorHeaderAuthRequire indicates that authorization check failed and authentication is required.
	// This typically results in HTTP 401 Unauthorized.
	ErrorHeaderAuthRequire

	// ErrorHeaderAuthForbidden indicates that authorization check succeeded but the client is not authorized.
	// This typically results in HTTP 403 Forbidden.
	ErrorHeaderAuthForbidden
)

Error codes for the router package. These codes are used with github.com/nabbar/golib/errors for structured error handling.

View Source
const (
	// EmptyHandlerGroup is the identifier used for routes registered without a group.
	// Routes with this group are registered directly on the engine root.
	EmptyHandlerGroup = "<nil>"

	// GinContextStartUnixNanoTime is the context key for storing request start time in nanoseconds.
	// Used by GinLatencyContext middleware to calculate request duration.
	GinContextStartUnixNanoTime = "gin-ctx-start-unix-nano-time"

	// GinContextRequestPath is the context key for storing the sanitized request path.
	// Includes query parameters if present. Set by GinRequestContext middleware.
	GinContextRequestPath = "gin-ctx-request-path"

	// GinContextRequestUser is the context key for storing the authenticated user from URL.
	// Set by GinRequestContext middleware when user info is present in the request URL.
	GinContextRequestUser = "gin-ctx-request-user"
)

Variables

This section is empty.

Functions

func DefaultGinInit added in v1.6.0

func DefaultGinInit() *ginsdk.Engine

DefaultGinInit creates a new Gin engine with default middleware. The engine includes Logger and Recovery middleware from Gin.

This is the default initializer used by NewRouterList when no custom initializer is provided.

Returns a configured Gin engine ready to use.

func DefaultGinWithTrustedPlatform added in v1.7.4

func DefaultGinWithTrustedPlatform(trustedPlatform string) *ginsdk.Engine

DefaultGinWithTrustedPlatform creates a new Gin engine with trusted platform header. The engine includes Logger and Recovery middleware.

The trusted platform header is used to determine the client's real IP address when behind a CDN or load balancer (e.g., "X-CDN-IP", "X-Real-IP").

Parameters:

  • trustedPlatform: Name of the header to trust for client IP

Returns a configured Gin engine with trusted platform set.

func DefaultGinWithTrustyProxy added in v1.6.0

func DefaultGinWithTrustyProxy(trustyProxy []string) *ginsdk.Engine

DefaultGinWithTrustyProxy creates a new Gin engine with trusted proxy configuration. The engine includes Logger and Recovery middleware.

Trusted proxies are IP addresses or CIDR ranges that are allowed to set X-Forwarded-For, X-Real-IP, and other forwarding headers.

Parameters:

  • trustyProxy: List of trusted proxy IP addresses or CIDR ranges

Returns a configured Gin engine with trusted proxies set.

func GinAccessLog added in v1.9.8

func GinAccessLog(log liblog.FuncLog) ginsdk.HandlerFunc

GinAccessLog is a middleware that logs HTTP access information. It should be used after GinLatencyContext and GinRequestContext to have access to timing and request path information.

The middleware logs:

  • Client IP address
  • Authenticated user (if any)
  • Request timestamp
  • Request duration
  • HTTP method
  • Request path with query parameters
  • HTTP protocol version
  • Response status code
  • Response size in bytes

If log is nil or returns nil, no logging is performed.

Usage:

logFunc := func() logger.Logger { return myLogger }
engine.Use(router.GinLatencyContext)
engine.Use(router.GinRequestContext)
engine.Use(router.GinAccessLog(logFunc))

See also: github.com/nabbar/golib/logger

func GinAddGlobalMiddleware added in v1.9.8

func GinAddGlobalMiddleware(eng *ginsdk.Engine, middleware ...ginsdk.HandlerFunc) *ginsdk.Engine

GinAddGlobalMiddleware adds one or more middleware functions to the Gin engine. The middleware will be applied to all routes registered on the engine.

Parameters:

  • eng: Gin engine to add middleware to
  • middleware: One or more middleware handler functions

Returns the same engine for method chaining.

Example:

engine := gin.New()
router.GinAddGlobalMiddleware(engine, router.GinLatencyContext, router.GinRequestContext)

func GinEngine added in v1.9.8

func GinEngine(trustedPlatform string, trustyProxy ...string) (*ginsdk.Engine, error)

GinEngine creates a new Gin engine with optional trusted platform and proxies. Unlike the Default* functions, this does not add any middleware by default.

Parameters:

  • trustedPlatform: Header name to trust for client IP (e.g., "X-Real-IP")
  • trustyProxy: Optional list of trusted proxy IP addresses or CIDR ranges

Returns:

  • *gin.Engine: Configured Gin engine
  • error: Error from SetTrustedProxies if proxy configuration fails

Example:

engine, err := router.GinEngine("X-Forwarded-For", "127.0.0.1", "192.168.0.0/16")
if err != nil {
    log.Fatal(err)
}

func GinErrorLog added in v1.9.8

func GinErrorLog(log liblog.FuncLog) ginsdk.HandlerFunc

GinErrorLog is a middleware that handles panic recovery and error logging. It catches panics, logs errors from the Gin context, and sends appropriate HTTP responses. Should be used with GinRequestContext to have access to the request path.

The middleware:

  • Recovers from panics and converts them to errors
  • Detects broken pipe errors (client disconnected)
  • Logs all errors attached to the Gin context
  • Logs recovered panics
  • Returns 500 Internal Server Error for panics (except broken pipes)

Special handling:

  • Broken pipe errors: Connection is aborted without writing status
  • Other panics: Returns HTTP 500 status

If log is nil or returns nil, errors are recovered but not logged.

Usage:

logFunc := func() logger.Logger { return myLogger }
engine.Use(router.GinRequestContext)
engine.Use(router.GinErrorLog(logFunc))

See also:

  • github.com/nabbar/golib/logger for logging
  • github.com/nabbar/golib/errors for error types

func GinLatencyContext added in v1.9.8

func GinLatencyContext(c *ginsdk.Context)

GinLatencyContext is a middleware that records the request start time. It stores the current time in nanoseconds in the Gin context under the key GinContextStartUnixNanoTime. This allows subsequent middleware or handlers to calculate request latency.

Usage:

engine.Use(router.GinLatencyContext)

To calculate latency in a handler:

startTime := time.Unix(0, c.GetInt64(router.GinContextStartUnixNanoTime))
latency := time.Since(startTime)

func GinRequestContext added in v1.9.8

func GinRequestContext(c *ginsdk.Context)

GinRequestContext is a middleware that extracts and stores request information. It sanitizes and stores the request path (with query parameters) and username (if present in the URL) in the Gin context.

Stored context keys:

  • GinContextRequestPath: Sanitized request path with query string
  • GinContextRequestUser: Username from URL (if present)

The sanitization prevents log injection attacks by removing newlines, tabs, etc.

Usage:

engine.Use(router.GinRequestContext)

func Handler

func Handler(routerList RouterList) http.Handler

Handler creates an http.Handler from a RouterList. It initializes a Gin engine and applies all routes from the RouterList.

If routerList is nil, the global default router list is used instead.

Parameters:

  • routerList: RouterList containing routes to register

Returns an http.Handler that can be used with http.Server.

Example:

routerList := router.NewRouterList(router.DefaultGinInit)
routerList.Register(http.MethodGet, "/health", healthHandler)
handler := router.Handler(routerList)
http.ListenAndServe(":8080", handler)

func RoutersHandler

func RoutersHandler(engine *ginsdk.Engine)

RoutersHandler applies all routes from the global default router list to the engine.

This is a convenience function for applications that use a single global router. For more complex applications, consider creating dedicated RouterList instances.

Parameters:

  • engine: Gin engine to register routes on

func RoutersRegister

func RoutersRegister(method string, relativePath string, router ...ginsdk.HandlerFunc)

RoutersRegister registers a route on the global default router list. The route is registered at root level (without a group).

This is a convenience function for applications that use a single global router. For more complex applications, consider creating dedicated RouterList instances.

Parameters:

  • method: HTTP method (GET, POST, PUT, DELETE, etc.)
  • relativePath: URL path for the route
  • router: One or more handler functions

func RoutersRegisterInGroup

func RoutersRegisterInGroup(group, method string, relativePath string, router ...ginsdk.HandlerFunc)

RoutersRegisterInGroup registers a route in a group on the global default router list.

This is a convenience function for applications that use a single global router. For more complex applications, consider creating dedicated RouterList instances.

Parameters:

  • group: Group path prefix
  • method: HTTP method (GET, POST, PUT, DELETE, etc.)
  • relativePath: URL path for the route (will be prefixed with group)
  • router: One or more handler functions

func SetGinHandler

func SetGinHandler(fct func(c *ginsdk.Context)) ginsdk.HandlerFunc

SetGinHandler is a type conversion helper that converts a function to HandlerFunc. This is useful when you have a function that matches the HandlerFunc signature but needs explicit type conversion.

Parameters:

  • fct: Function with signature func(*gin.Context)

Returns the function as a gin.HandlerFunc type.

Types

type RegisterRouter

type RegisterRouter func(method string, relativePath string, router ...ginsdk.HandlerFunc)

RegisterRouter is a function type for registering routes without a group. It takes an HTTP method, relative path, and one or more handler functions.

type RegisterRouterInGroup

type RegisterRouterInGroup func(group, method string, relativePath string, router ...ginsdk.HandlerFunc)

RegisterRouterInGroup is a function type for registering routes within a group. It takes a group path, HTTP method, relative path, and one or more handler functions.

type RouterList

type RouterList interface {
	// Register adds a route without a group (registered at root level).
	// Multiple handlers can be provided and will be executed in order.
	Register(method string, relativePath string, router ...ginsdk.HandlerFunc)

	// RegisterInGroup adds a route within a specified group.
	// The group path is prefixed to the relative path.
	// Multiple routes can be registered in the same group.
	RegisterInGroup(group, method string, relativePath string, router ...ginsdk.HandlerFunc)

	// RegisterMergeInGroup adds or replaces a route in a group.
	// If a route with the same method and path already exists in the group,
	// its handlers are replaced with the new ones.
	RegisterMergeInGroup(group, method string, relativePath string, router ...ginsdk.HandlerFunc)

	// Handler applies all registered routes to the given Gin engine.
	// Routes are organized by group and registered accordingly.
	Handler(engine *ginsdk.Engine)

	// Engine returns a new Gin engine instance using the configured init function.
	// If no init function was provided, DefaultGinInit is used.
	Engine() *ginsdk.Engine
}

RouterList manages a collection of HTTP routes with optional grouping. It provides methods to register routes, organize them into groups, and apply them to a Gin engine.

All methods are safe for concurrent use.

func NewRouterList

func NewRouterList(initGin func() *ginsdk.Engine) RouterList

NewRouterList creates a new RouterList instance with the specified Gin engine initializer. If initGin is nil, DefaultGinInit will be used when Engine() is called.

Example:

routerList := NewRouterList(func() *gin.Engine {
    engine := gin.New()
    engine.Use(gin.Logger(), gin.Recovery())
    return engine
})

Directories

Path Synopsis
Package auth provides HTTP authorization middleware for Gin-based applications.
Package auth provides HTTP authorization middleware for Gin-based applications.
Package authheader provides HTTP authorization header constants and helper functions.
Package authheader provides HTTP authorization header constants and helper functions.
Package header provides HTTP header management for Gin-based applications.
Package header provides HTTP header management for Gin-based applications.

Jump to

Keyboard shortcuts

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