Mux is a simple and efficient route distributor that supports the net/http interface of the standard library.
Mux is a simple and efficient route distributor that supports the net/http interface of the standard library. Routing data is stored in the prefix tree preTree, supported by https://github.com/obity/pretree.
The current version only supports variable routes and simple routes such as /user/:id or /user/login. According to the http request method GET, HEAD, POST, PUT, PATCH, DELETE, CONNECT, OPTIONS, TRACE grouping is handled separately, and the routes between different groups are isolated from each other. Routing variable writing format supports both :id and {id},but the actual storage is :id.
See this document at GoDoc
go get -u github.com/obity/mux@latest
package main
import (
"net/http"
"github.com/obity/mux"
)
func main() {
m := mux.NewMux()
m.GET("/pet/findByStatus", Findbystatus)
m.GET("/pet/{id}", PetHandler)
m.POST("/user/createWithList", Createwithlist)
m.DELETE("/user/:username", Userinfo)
m.Start(":8001")
}
func PetHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ID := vars["id"]
w.WriteHeader(http.StatusOK)
w.Write([]byte("ID:" + ID))
return
}
func Findbystatus(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("FindByStatus matched:" + r.RequestURI))
return
}
func Createwithlist(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("CreateWithList matched:" + r.RequestURI))
return
}
func Userinfo(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("UserInfo matched:" + r.RequestURI))
return
}
func UserHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("UserInfo matched:" + r.RequestURI))
return
}