Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
github.com/cdelorme/go-routing
This code is DEPRECATED, I won't be working on it anymore. The HttpRouter library works great and has complete code coverage, I highly recommend it.
Basic routing abstraction for the core http package in golang.
It aims at delivering a simple way to setup a server, and route traffic.
There are a few library-style alternatives:
While Gorilla
and gocraft
offer excellent features, my own package is nearly a mirror of HttpRouter
. The HttpRouter
package provides exceptional performance, but doesn't give you all the features and controls the others do.
If you'd prefer to be locked into a framework, there are a few out there that do more than routing:
There is also a gin
made by the makers of martini
which reloads projects when modified, not to be confused with the gin
web framework, which is based off HttpRouter
and has very good performance.
My router aims for simplicity while remaining completely modular. It features the following:
http.Handler
interface (ServeHTTP) and can be applied directly to a server/favicon.ico
)My router acts as an abstraction for registering many routes under a single http.Handler
that can be assigned to an http.Server
; without any crossover object or data type references that are specific to my library. This keeps things fully decoupled and as basic as possible.
You can import my router like this:
import "github.com/cdelorme/go-routing"
You can create a router like this:
router := routing.Router{}
You may optionally apply a prefix:
router.Prefix = "/api/v1"
This prefix will be used by the RegisterController
callback to CreateAndRegisterRoute
, but will be ignored by CreateRoute
and RegisterRoute
(since those are direct access methods). This should provide some flexibility.
Route registration accepts a path and one or more methods (1):
router.CreateAndRegisterRoute("/path", callbackOne, "GET")
router.CreateAndRegisterRoute("/path2", callbackTwo, "GET", "POST", "PUT")
You can also register a custom 404 (NotFound) handler, like this:
router.HandleNotFound = My404Handler
You can register a compatible controller like this (2):
router.RegisterController(controller)
You can create a new server and apply the router to it like this:
Or in more detail, like this:
server := http.Server{
Addr: Address,
MaxHeaderBytes: 1 << 20,
Handler: &router,
}
Callbacks can be stand-alone functions or part of a struct. Routes are wildcard matched using strings.Index
, adding built-in support for optional trailing slashes, and custom enhancements such as URL-based argument parsing supporting Clean URL structures.
Your controller is responsible for appending any prefixes to a route during registration (CreateAndRegisterRoute
).
Because of wildcard routing, registering parent and child routes is ill-advised with this system.
I had originally toyed with the idea of passing optional prefixes through the registration process, but concluded that it created an unnecessary level of complication.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.