sux

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 3 Imported by: 1

README

sux

An allocation-conscious, middleware-capable HTTP router for Go with support for static routes, parameters (:id), wildcards (*path), route groups, and configurable 404/405 handling.

Installation

go get code.icod.de/dalu/sux

Quick start

package main

import (
	"net/http"

	"code.icod.de/dalu/sux"
)

func main() {
	r := sux.New()

	r.GET("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello"))
	})

	r.GET("/users/:id", func(w http.ResponseWriter, r *http.Request) {
		params := sux.ParamsFromContext(r)
		w.Write([]byte("user " + params.Get("id")))
	})

	http.ListenAndServe(":8080", r)
}

Middleware

Middleware wraps handlers and can be applied globally or per route group.

logger := func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// log request here
		next.ServeHTTP(w, r)
	})
}

r := sux.New()
r.Use(logger) // global

r.GET("/ping", func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("pong"))
})

Route groups

Groups share a prefix and middleware.

api := r.Group("/api", func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("X-API-Version", "v1")
		next.ServeHTTP(w, r)
	})
})

api.GET("/users", func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("users"))
})

Parameters and wildcards

r.GET("/posts/:postId", func(w http.ResponseWriter, r *http.Request) {
	p := sux.ParamsFromContext(r)
	w.Write([]byte(p.Get("postId")))
})

r.GET("/files/*path", func(w http.ResponseWriter, r *http.Request) {
	p := sux.ParamsFromContext(r)
	w.Write([]byte("file: " + p.Get("path")))
})

404/405 handlers

r.NotFound(func(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "custom 404", http.StatusNotFound)
})

r.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "custom 405", http.StatusMethodNotAllowed)
})

// Disable cross-method probing if you prefer 404s instead of 405 checks
r.EnableMethodNotAllowedCheck(false)

Performance notes

  • Internally pools parsed segments and parameter storage to reduce per-request allocations.
  • Parameters are copied into the request context so they remain valid even after handlers return (important for async users of the context).
  • For raw numbers, run go test -bench . -benchmem.

Testing

go test ./...

Benchmarks:

go test -bench . -benchmem

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MiddlewareFunc added in v1.1.0

type MiddlewareFunc func(http.Handler) http.Handler

MiddlewareFunc represents a middleware function

type Params added in v1.1.0

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

Params holds route parameters extracted from the URL Optimized to reduce allocations compared to map[string]string

func ParamsFromContext added in v1.1.0

func ParamsFromContext(r *http.Request) Params

ParamsFromContext extracts route parameters from the request context

func (Params) Get added in v1.1.0

func (p Params) Get(key string) string

Get returns the value for the given key

func (Params) Len added in v1.1.0

func (p Params) Len() int

Len returns the number of parameters

func (*Params) Reset added in v1.1.0

func (p *Params) Reset()

Reset clears all parameters for reuse

func (*Params) Set added in v1.1.0

func (p *Params) Set(key, value string)

Set sets the value for the given key

type Router

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

func New

func New() *Router

New creates a new Router instance

func (*Router) DELETE

func (r *Router) DELETE(path string, handler http.HandlerFunc) *Router

DELETE adds a DELETE route

func (*Router) EnableMethodNotAllowedCheck added in v1.2.0

func (r *Router) EnableMethodNotAllowedCheck(enabled bool)

EnableMethodNotAllowedCheck toggles costly cross-method lookup on 404

func (*Router) GET

func (r *Router) GET(path string, handler http.HandlerFunc) *Router

GET adds a GET route

func (*Router) Group added in v1.1.0

func (r *Router) Group(prefix string, middleware ...MiddlewareFunc) *Router

Group creates a new route group with a prefix and optional middleware

func (*Router) HEAD

func (r *Router) HEAD(path string, handler http.HandlerFunc) *Router

HEAD adds a HEAD route

func (*Router) MethodNotAllowed added in v1.1.0

func (r *Router) MethodNotAllowed(handler http.HandlerFunc)

MethodNotAllowed sets a custom 405 handler

func (*Router) NotFound added in v1.1.0

func (r *Router) NotFound(handler http.HandlerFunc)

NotFound sets a custom 404 handler

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handler http.HandlerFunc) *Router

OPTIONS adds an OPTIONS route

func (*Router) PATCH

func (r *Router) PATCH(path string, handler http.HandlerFunc) *Router

PATCH adds a PATCH route

func (*Router) POST

func (r *Router) POST(path string, handler http.HandlerFunc) *Router

POST adds a POST route

func (*Router) PUT

func (r *Router) PUT(path string, handler http.HandlerFunc) *Router

PUT adds a PUT route

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface

func (*Router) Use added in v1.1.0

func (r *Router) Use(middleware ...MiddlewareFunc)

Use adds global middleware to the router

Jump to

Keyboard shortcuts

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