extras

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 7 Imported by: 0

README

Router Adapters

This package provides router adapters for different routing backends.

Available Adapters

Default adapter - High performance, modern API

import (
    "github.com/xraph/forge"
    "github.com/xraph/forge/extras"
)

router := forge.NewRouter(
    forge.WithAdapter(extras.NewBunRouterAdapter()),
    forge.WithContainer(container),
)

Features:

  • Fast route matching
  • Modern API design
  • Path parameters: /users/:id
  • Wildcard routes: /files/*
2. Chi Router

Clean, composable router from go-chi

router := forge.NewRouter(
    forge.WithAdapter(extras.NewChiAdapter()),
    forge.WithContainer(container),
)

Features:

  • Middleware-first design
  • Sub-router mounting
  • Path parameters: /users/{id}
3. HTTPRouter

Extremely fast router from julienschmidt

router := forge.NewRouter(
    forge.WithAdapter(extras.NewHTTPRouterAdapter()),
    forge.WithContainer(container),
)

Features:

  • Fastest route matching
  • Minimal allocations
  • Path parameters: /users/:id

Default Behavior

If no adapter is specified, the router uses a simple in-memory adapter for basic functionality. For production use, explicitly specify one of the above adapters.

Choosing an Adapter

Adapter Performance Features Use Case
BunRouter ⚡⚡⚡ High Modern, full-featured Recommended for most apps
Chi ⚡⚡ Good Middleware-focused Apps with complex middleware
HTTPRouter ⚡⚡⚡ Highest Minimal, fast High-throughput APIs

Example

package main

import (
    "net/http"
    "github.com/xraph/forge"
    "github.com/xraph/forge/extras"
)

func main() {
    // Create container
    container := forge.NewContainer()
    
    // Create router with BunRouter adapter
    router := forge.NewRouter(
        forge.WithAdapter(extras.NewBunRouterAdapter()),
        forge.WithContainer(container),
        forge.WithRecovery(),
    )
    
    // Register routes
    router.GET("/", func(ctx forge.Context) error {
        return ctx.String(200, "Hello from BunRouter!")
    })
    
    router.GET("/users/:id", func(ctx forge.Context) error {
        id := ctx.Param("id")
        return ctx.JSON(200, map[string]string{
            "id": id,
            "name": "User " + id,
        })
    })
    
    // Start server
    http.ListenAndServe(":8080", router)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHTTPRouterParams

func GetHTTPRouterParams(r *http.Request) httprouter.Params

GetHTTPRouterParams retrieves httprouter params from context

func NewBunRouterAdapter

func NewBunRouterAdapter() forge.RouterAdapter

NewBunRouterAdapter creates a BunRouter adapter (default)

func NewChiAdapter

func NewChiAdapter() forge.RouterAdapter

NewChiAdapter creates a Chi router adapter

func NewHTTPRouterAdapter

func NewHTTPRouterAdapter() forge.RouterAdapter

NewHTTPRouterAdapter creates an HTTPRouter adapter

Types

type BunRouterAdapter

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

BunRouterAdapter wraps uptrace/bunrouter

func (*BunRouterAdapter) Close

func (a *BunRouterAdapter) Close() error

Close cleans up resources

func (*BunRouterAdapter) Handle

func (a *BunRouterAdapter) Handle(method, path string, handler http.Handler)

Handle registers a route

func (*BunRouterAdapter) Mount

func (a *BunRouterAdapter) Mount(path string, handler http.Handler)

Mount registers a sub-handler

func (*BunRouterAdapter) ServeHTTP

func (a *BunRouterAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches requests

type ChiAdapter

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

ChiAdapter wraps go-chi/chi router

func (*ChiAdapter) Close

func (a *ChiAdapter) Close() error

Close cleans up resources

func (*ChiAdapter) Handle

func (a *ChiAdapter) Handle(method, path string, handler http.Handler)

Handle registers a route

func (*ChiAdapter) Mount

func (a *ChiAdapter) Mount(path string, handler http.Handler)

Mount registers a sub-handler

func (*ChiAdapter) ServeHTTP

func (a *ChiAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches requests

type HTTPRouterAdapter

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

HTTPRouterAdapter wraps julienschmidt/httprouter

func (*HTTPRouterAdapter) Close

func (a *HTTPRouterAdapter) Close() error

Close cleans up resources

func (*HTTPRouterAdapter) Handle

func (a *HTTPRouterAdapter) Handle(method, path string, handler http.Handler)

Handle registers a route

func (*HTTPRouterAdapter) Mount

func (a *HTTPRouterAdapter) Mount(path string, handler http.Handler)

Mount registers a sub-handler

func (*HTTPRouterAdapter) ServeHTTP

func (a *HTTPRouterAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches requests

Jump to

Keyboard shortcuts

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