gohttp/admin/admin.go

58 lines
1.3 KiB
Go

package admin
import (
"net/http"
"strings"
)
type Route struct {
Method string
Path string
Handle http.HandlerFunc
}
func about(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("About Page"))
}
var AdminRoutes = []Route{
// Admin Routes
{"GET", "/about", about},
}
type PServeMux struct {
// routes map[string]map[string]http.HandlerFunc
*http.ServeMux
}
func (p *PServeMux) Use(route Route) {
method := strings.ToLower(route.Method)
p.ServeMux.HandleFunc("/"+method+route.Path, route.Handle)
// _, exist := p.routes[route.Path]
// if !exist {
// mroutes := make(map[string]http.HandlerFunc)
// p.routes[route.Path] = mroutes
// p.ServeMux.HandleFunc(route.Path, func(w http.ResponseWriter, r *http.Request) {
// p.routes[route.Path][r.Method](w, r)
// })
// }
// p.routes[route.Path][route.Method] = route.Handle
}
func (p *PServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/" + strings.ToLower(r.Method) + r.URL.Path
r.RequestURI = "/" + strings.ToLower(r.Method) + r.RequestURI
p.ServeMux.ServeHTTP(w, r)
}
var AdminServerMux *PServeMux
func init() {
AdminServerMux = &PServeMux{ServeMux: http.NewServeMux()}
// AdminServerMux.routes = make(map[string]map[string]http.HandlerFunc)
for _, route := range AdminRoutes {
AdminServerMux.Use(route)
}
}