Documentation
¶
Overview ¶
Package transwarp provides a unified, framework-agnostic interface for HTTP routing in Go.
It acts as a wrapper layer that allows developers to write routing logic once and switch between different underlying web engines (such as Fiber, Gin, Echo, or Chi) at compile time using Go build tags.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶ added in v1.0.1
func Chain(h http.HandlerFunc, mws ...Middleware) http.HandlerFunc
Chain is a helper that wraps a specific handler with a list of middlewares.
It allows applying middlewares to a single endpoint without creating a Group. The middlewares are applied in the order they are passed (first matches first).
Usage:
server.GET("/private", transwarp.Chain(myHandler, AuthMW, LoggingMW))
func Register ¶
func Register(d Driver, c routerConstructor)
Register adds a driver constructor to the central registry.
This function is intended to be called within the `init()` functions of the driver-specific files (e.g., `driver_fiber.go`). This allows for a decoupled, plugin-like architecture where the core logic doesn't strictly depend on specific frameworks until they are explicitly linked by the compiler.
Parameters:
- d: The Driver enum key (e.g., DriverFiber).
- c: The constructor function that initializes that specific driver.
Types ¶
type Config ¶
type Config struct {
// Driver specifies which underlying engine should be initialized.
//
// Important: The chosen Driver must match the build tags used during
// compilation. For example, if DriverFiber is set, the application
// must be built with `go build -tags fiber`.
//
// If the Driver does not match the compiled build tags, the Factory
// method (New) will panic to prevent runtime inconsistencies.
Driver Driver
}
Config holds the initialization settings for the Transwarp engine.
type Driver ¶
type Driver string
Driver represents the specific web framework engine that Transwarp will use to handle HTTP requests.
The value of the Driver must match the build tag provided during compilation.
const ( // DriverGin selects the Gin Gonic framework (v1). // To use this driver, you must compile with: -tags gin DriverGin Driver = "gin" // DriverEcho selects the Echo framework (v5). // To use this driver, you must compile with: -tags echo DriverEcho Driver = "echo" // DriverFiber selects the GoFiber framework (v3 Beta). // This driver offers high performance but requires specific handling for // zero-allocation contexts. // To use this driver, you must compile with: -tags fiber DriverFiber Driver = "fiber" // DriverChi selects the go-chi framework (v5). // This driver is fully compatible with standard net/http and is lightweight. // To use this driver, you must compile with: -tags chi DriverChi Driver = "chi" // DriverMock selects the internal Mock router. // This driver is intended for unit testing logic without spinning up // a real TCP network listener. It requires no specific build tags. DriverMock Driver = "mock" )
type Middleware ¶
Middleware defines the standard function signature for HTTP interceptors.
Transwarp enforces the standard "net/http" middleware pattern:
func(next http.Handler) http.Handler
This ensures that middlewares are portable and compatible across all supported drivers (Gin, Fiber, etc.), as Transwarp handles the necessary internal adaptations for non-standard frameworks.
type Transwarp ¶
Transwarp is the main interface that abstracts the routing logic.
It embeds the internal Router interface, exposing methods to:
- Register routes (GET, POST, PUT, DELETE, etc.)
- Create route groups with shared middleware.
- Register middleware (Use).
- Retrieve URL parameters in a unified way.
- Start the HTTP server (Serve).
Implementation details are hidden behind this interface, allowing the underlying engine to be swapped without changing the consuming code.
func New ¶
New is the main factory method for instantiating the Transwarp engine.
It looks up the requested driver in the internal registry and returns an initialized Transwarp interface.
Panic: This function will panic if the requested Driver is not found in the registry. This usually happens when there is a mismatch between the code configuration and the compilation command.
Example of a Panic Scenario:
- Code: transwarp.New(transwarp.Config{Driver: transwarp.DriverFiber})
- Command: go run -tags gin main.go
In this case, the Fiber driver was never compiled, so it never registered itself, causing the lookup to fail. The command should has been:
- Code: transwarp.New(transwarp.Config{Driver: transwarp.DriverFiber})
- Command: go run .tags fiber main.go
