i have viewed source code of mux,but i just want something simple ,not using all features.
i wanna get value of "id" in the url like /url/{id},setting the value in the req.Form and clean the path like mux.
code like
r:=http.NewServeMux()
r.HandlerFunc("/",func(w http.ResponseWriter,r *http.Request){
// Clean path to canonical form and redirect.
if p := cleanPath(req.URL.Path); p != req.URL.Path {
url := *req.URL
url.Path = p
p = url.String()
//根据HTTP的协议重定向
w.Header().Set("Location", p)
w.WriteHeader(http.StatusMovedPermanently)
return
}
// some code check the url parse id and set to req.form
})
//then add some specific url handlers.
in go doc ,it says long Pattern will take higher priority than short Pattern. and i want to run something(parse id , clean path etc) before all handlers.
i don't want to give up the features of defaultmux. should i redefine a brand new route,or use http.NewServeMux()? if i use http.NewServerMux(),how should i do to add something while keeping the features?
We have used http://www.gorillatoolkit.org/pkg/mux for over a year in our production stack and have been very happy with it.
For some really simple sites I host I use the built in routing something like this:
package main
import (
"flag"
"fmt"
"net/http"
"os"
)
const (
version = "0.1.0"
)
var (
port uint
)
func init() {
flag.UintVar(&port, "port", 8000, "the port to listen on")
flag.UintVar(&port, "p", 8000, "the port to listen on")
}
func main() {
flag.Parse()
// Retrieve the current working directory.
path, err := os.Getwd()
if err != nil {
panic(err)
}
http.HandleFunc("/gallery/view.aspx", handleGallery)
http.HandleFunc("/gallery/viewLarge.aspx", handleViewLarge)
http.HandleFunc("/ir.ashx", handleImageResize)
http.Handle("/", http.FileServer(http.Dir(path)))
panic(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
Let me know what you want your routes to be and I could give you a more specific example to what you are looking for.