mwmux

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 4 Imported by: 0

README

mwmux

Small wrapper around Go's http.ServeMux to register middleware in the same way as in Express.js / ASP.NET (and probably several others).

Getting mwmux

Run the following Go command to install the mwmux package:

$ go get github.com/nielshoek/mwmux
Running mwmux

First you need to import the mwmux package, then an example using a simple middleware and a middleware with a wildcard:

import (
	"fmt"
	"net/http"

	"github.com/nielshoek/mwmux"
)

func main() {
// 1. Create a CMux.
	mux := mwmux.NewMWMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Home"))
	})

	mux.Handle("/todos", &MyHandler{})
	mux.Handle("/todos/", &MyHandler{})

// 2a. Register a middleware.
	mux.Use("/", func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		fmt.Println("Middleware '/'")
		next(w, r)
	})

// 2b. Register a middleware which expects a part to be an identifier.
//     Works like a wildcard, meaning that part of the URL path can be anything.
//     Only the '{' and the '}' character are necessary. So e.g.
//     '/{}/' or '/{todoId}/' would work as well. Can be used multiple times.
	mux.Use("/todos/{id}", func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		fmt.Println("Middleware '/todos/{id}'")
		next(w, r)
	})

	if err := mux.ListenAndServe("localhost:4321"); err != nil {
		fmt.Println("Server Error: ", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerWrapper

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

func (*HandlerWrapper) ServeHTTP

func (handlerWrapper *HandlerWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MWMux added in v0.5.0

type MWMux struct {
	Middlewares map[string]map[int]MiddlewareFunc
	// contains filtered or unexported fields
}
var MMux *MWMux

func NewMWMux added in v0.5.0

func NewMWMux() *MWMux

func (*MWMux) Handle added in v0.5.0

func (mmux *MWMux) Handle(pattern string, handler http.Handler)

func (*MWMux) HandleFunc added in v0.5.0

func (mmux *MWMux) HandleFunc(p string, handler func(http.ResponseWriter, *http.Request))

func (*MWMux) ListenAndServe added in v0.5.0

func (mmux *MWMux) ListenAndServe(addr string) error

func (*MWMux) ServeHTTP added in v0.5.0

func (mmux *MWMux) ServeHTTP(writer http.ResponseWriter, request *http.Request)

func (*MWMux) Use added in v0.5.0

func (mmux *MWMux) Use(
	path string,
	handler func(http.ResponseWriter, *http.Request, http.HandlerFunc),
)

Register a middleware on the MWMux

type MiddlewareFunc

type MiddlewareFunc func(http.ResponseWriter, *http.Request, http.HandlerFunc)

Jump to

Keyboard shortcuts

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