mux

package module
Version: v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2020 License: MIT Imports: 1 Imported by: 2

README

Package mux

Package mux does HTTP servers in a compositional, middleware-based style. It's inspired by KoaJS.

Head on over to the docs to learn more.

Examples coming soon.

Documentation

Overview

Package mux does HTTP servers in a compositional, middleware-based style. It's inspired by https://koajs.com.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware func(r *Request)

Middleware is the basic unit used to compose path handlers and segment request handling logic.

func (Middleware) HandlerFunc added in v0.4.0

func (m Middleware) HandlerFunc(next http.HandlerFunc) http.HandlerFunc

HandlerFunc returns an http.HandlerFunc equivalent of the middleware. When converting, the given http.HandlerFunc will be called when the middleware calls Next on the request, if at all. The next HandlerFunc can be nil, in which case r.Next() in the middleware will do nothing.

type Mux

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

Mux implements http.Handler and provides the Handle() method, which can be used to compose multiple middleware functions into a HandlerFunc() which is registered on the provided path on the internal http.ServeMux.

func New

func New(common ...Middleware) Mux

New constructs a new Mux. The provided common middleware will be run before other middleware for every handled path. They won't be run for unhandled paths.

func (Mux) ServeHTTP

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

ServeHTTP forwards to the internal http.ServeMux.

func (Mux) Use added in v0.3.0

func (m Mux) Use(path string, middleware ...Middleware)

Use composes the provided middleware with the common middleware to create a single HandlerFunc, and registers it for the given path on the internal ServeMux. Path matching works exactly as it does in the standard http package.

type Next

type Next func()

Next is just a regular function the middleware can call to continue processing the request in the rest of the middleware chain. When it returns, the rest of the middleware chain has finished processing, and the calling middleware is next to finish. I'm giving it its own type in case I decide to change the function signature later on.

type Request

type Request struct {
	*http.Request
	Response http.ResponseWriter
	Next     Next
}

Request couples requests and responses because for servers it's safe to assume that every received *http.Request should have some kind of associated http.ResponseWriter.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL