mroute is a HTTP request multiplexer, similar to net/http.ServeMux
.
It compares incoming requests to a list of registered Patterns, and
dispatches to the mchain.Handler that corresponds to the first matching
Pattern. mroute also supports Middleware (composable shared
functionality applied to every request) and uses the standard
context
package to store request-scoped values.
This is a fork of goji adapted to the mchain philosophy of returning errors.
package main
import (
"fmt"
"net/http"
"github.com/prasannavl/mroute"
"github.com/prasannavl/mroute/pat"
)
func hello(w http.ResponseWriter, r *http.Request) error {
name := pat.Param(r, "name")
fmt.Fprintf(w, "Hello, %s!", name)
return nil
}
func main() {
mux := mroute.NewMux()
mux.HandleFunc(pat.Get("/hello/:name"), hello)
http.ListenAndServe("localhost:8000", mux)
}
Please refer to mroute's GoDoc Documentation for a full API reference.
mchain
: https://github.com/prasannavl/mchain
goji/goji
: https://goji.io
Thanks to goji
router - mroute is an adaptation of this fantastic work.