prefixhandler

Path Prefix Handler for http.ServeMux
Example
package main
import (
"io"
"net/http"
"github.com/acoshift/prefixhandler"
)
func main() {
mux := http.NewServeMux()
itemMux := http.NewServeMux()
itemMux.Handle("/", http.HandlerFunc(itemDetail))
itemMux.Handle("/edit", http.HandlerFunc(itemEdit))
mux.Handle("/item", http.NotFoundHandler())
mux.Handle("/item/", prefixhandler.New("/item", "item_id", itemMux))
http.ListenAndServe(":8080", mux)
}
func itemDetail(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
itemID := prefixhandler.Get(r.Context(), "item_id")
// or
// itemID = r.Context().Value("item_id").(string)
if itemID == "" {
http.NotFound(w, r)
return
}
io.WriteString(w, "Item: "+itemID)
}
func itemEdit(w http.ResponseWriter, r *http.Request) {
itemID := prefixhandler.Get(r.Context(), "item_id")
io.WriteString(w, "Editing Item: "+itemID)
}