You are here: Home » NewsFeeds » Mux – A lightweight, fast HTTP request router for Go

Mux – A lightweight, fast HTTP request router for Go

README.md

mux is a lightweight fast HTTP request router (also called multiplexer or just mux for short) for Go 1.7.

The difference between the default mux of Go’s net/http package and this mux is,
it’s supports variables and regex in the routing pattern and matches against the request method. It also scales better.

Features:

REGEX URL Matcher
Vars URL Matcher
GetVars in handler
GetQueries in handler
URL Matcher
Header Matcher
Scheme Matcher
Custom Matcher
Route Validators
Http method declaration
Support for standard lib http.Handler and http.HandlerFunc
Custom NotFound handler
Respect the Go standard http.Handler interface
Routes are sorted
Context support
Feature request are welcome

Example (Method GET):

package main

import (
“net/http”
“fmt”
“os”

“github.com/donutloop/mux”
)

func main() {
r := mux.Classic()

//URL: https://localhost:8080/home
r.HandleFunc(http.MethodGet, “/home”, homeHandler)

//URL: https://localhost:8080/home-1
r.Handle(http.MethodGet, “/home-1”, http.HandlerFunc(homeHandler))

//URL: https://localhost:8080/home-2
r.Get(“/home-2”, homeHandler)

//URL: https://localhost:8080/home-3
r.RegisterRoute(http.MethodGet, r.NewRoute().Path(“/home-3”).HandlerFunc(homeHandler))

//URL: https://localhost:8080/home-4
r.RegisterRoute(http.MethodGet, r.NewRoute().Path(“/home-4”).Handler(http.HandlerFunc(homeHandler)))

errorHandler := func(errs []error) {
for _ , err := range errs {
fmt.Print(err)
}
if != len(errs) {
os.Exit(2)
}


 

Original article