flow

package
v2.16.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package flow is a delightfully simple, readable, and tiny HTTP router for Go web applications. Its features include:

* Use named parameters, wildcards and (optionally) regexp patterns in your routes. * Create route groups which use different middleware (a bit like chi). * Customizable handlers for 404 Not Found and 405 Method Not Allowed responses. * Automatic handling of OPTIONS and HEAD requests. * Works with http.Handler, http.HandlerFunc, and standard Go middleware.

Example code:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/alexedwards/flow"
)

func main() {
	mux := flow.New()

	// The Use() method can be used to register middleware. Middleware declared at
	// the top level will used on all routes (including error handlers and OPTIONS
	// responses).
	mux.Use(exampleMiddleware1)

	// Routes can use multiple HTTP methods.
	mux.HandleFunc("/profile/:name", exampleHandlerFunc1, "GET", "POST")

	// Optionally, regular expressions can be used to enforce a specific pattern
	// for a named parameter.
	mux.HandleFunc("/profile/:name/:age|^[0-9]{1,3}$", exampleHandlerFunc2, "GET")

	// The wildcard ... can be used to match the remainder of a request path.
	// Notice that HTTP methods are also optional (if not provided, all HTTP
	// methods will match the route).
	mux.Handle("/static/...", exampleHandler)

	// You can create route 'groups'.
	mux.Group(func(mux *flow.Mux) {
		// Middleware declared within in the group will only be used on the routes
		// in the group.
		mux.Use(exampleMiddleware2)

		mux.HandleFunc("/admin", exampleHandlerFunc3, "GET")

		// Groups can be nested.
		mux.Group(func(mux *flow.Mux) {
			mux.Use(exampleMiddleware3)

			mux.HandleFunc("/admin/passwords", exampleHandlerFunc4, "GET")
		})
	})

	err := http.ListenAndServe(":2323", mux)
	log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

AllMethods is a slice containing all HTTP request methods.

Functions

func Param

func Param(ctx context.Context, param string) string

Param is used to retrieve the value of a named parameter or wildcard from the request context. It returns the empty string if no matching parameter is found.

Types

type Mux

type Mux struct {
	NotFound         http.Handler
	MethodNotAllowed http.Handler
	Options          http.Handler
	// contains filtered or unexported fields
}

Mux is a http.Handler which dispatches requests to different handlers.

func New

func New() *Mux

New returns a new initialized Mux instance.

func (*Mux) Group

func (m *Mux) Group(fn func(*Mux))

Group is used to create 'groups' of routes in a Mux. Middleware registered inside the group will only be used on the routes in that group. See the example code at the start of the package documentation for how to use this feature.

func (*Mux) Handle

func (m *Mux) Handle(pattern string, handler http.Handler, methods ...string)

Handle registers a new handler for the given request path pattern and HTTP methods.

func (*Mux) HandleFunc

func (m *Mux) HandleFunc(pattern string, fn http.HandlerFunc, methods ...string)

HandleFunc is an adapter which allows using a http.HandlerFunc as a handler.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes the router implement the http.Handler interface.

func (*Mux) Use

func (m *Mux) Use(mw ...func(http.Handler) http.Handler)

Use registers middleware with the Mux instance. Middleware must have the signature `func(http.Handler) http.Handler`.

Jump to

Keyboard shortcuts

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