triemux

package
v0.0.0-...-b92a033 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT, MIT Imports: 8 Imported by: 1

README

triemux

triemux is an implementation of Go's http.Handler that multiplexes a set of other handlers onto a single path hierarchy, using an efficient prefix trie, or "trie", to map request paths to handlers. It's designed to be used as a first-line router, allowing different paths on the same domain to be served by different applications.

API documentation for triemux can be found at godoc.org.

Install

go install github.com/alphagov/router/triemux

Usage

mux := triemux.NewMux()

com := httputil.NewSingleHostReverseProxy(url.Parse("https://example.com"))
org := httputil.NewSingleHostReverseProxy(url.Parse("https://example.org"))

// Register a prefix route pointing to the "com" backend (all requests to
// "/com<anything>" will go to this backend).
mux.Handle("/com", true, com)

// Register an exact (non-prefix) route pointing to the "org" backend.
mux.Handle("/org", false, org)

...

srv := &http.Server{
        Addr:         ":8080",
        Handler:      mux,
        ReadTimeout:  60 * time.Second(),
        WriteTimeout: 60 * time.Second(),
}
srv.ListenAndServe()

Licence

triemux is released under the MIT licence, a copy of which can be found in LICENCE.

Documentation

Overview

Package triemux implements an HTTP multiplexer, or URL router, which can be used to serve responses from multiple distinct handlers within a single URL hierarchy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterMetrics

func RegisterMetrics(r prometheus.Registerer)

Types

type Mux

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

func NewMux

func NewMux() *Mux

NewMux makes a new empty Mux.

func (*Mux) Handle

func (mux *Mux) Handle(path string, prefix bool, handler http.Handler)

Handle adds a route (either an exact path or a path prefix) to the Mux and and associates it with a handler, so that the Mux will pass matching requests to that handler.

func (*Mux) RouteCount

func (mux *Mux) RouteCount() int

func (*Mux) ServeHTTP

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

ServeHTTP forwards the request to a backend with a registered route matching the request path. Serves 404 when there is no backend. Serves 301 redirect to lowercase path when the URL path is entirely uppercase. Serves 503 when no routes are loaded.

Jump to

Keyboard shortcuts

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