package srv import ( "net/http" "strings" ) type Req struct { r *http.Request AppURL string Path string ParentPath string Pattern string Sess Sess Data map[string]string } func (req *Req) Construct(r *http.Request) { req.r = r req.r.ParseForm() req.AppURL = "/" + r.Host req.Path = r.RequestURI req.ParentPath = req.getParentPath() req.Pattern = req.getPattern() req.Data = make(map[string]string) } func (req *Req) Body(key string) string { return req.r.FormValue(key) } func (req *Req) Query(key string) string { return req.r.URL.Query().Get(key) } func (req *Req) Param(key string) string { return req.r.PathValue(key) } func (req *Req) Cookie(name string) (string, error) { cookie, err := req.r.Cookie(name) if err != nil { return "", err } return cookie.Value, nil } func (req *Req) getParentPath() string { parentPath := "" arr := strings.Split(req.Path, "/") for i := 0; i < len(arr)-1; i++ { if arr[i] == "" { continue } parentPath += "/" + arr[i] } return parentPath } func (req *Req) getPattern() string { _, pattern := rtr.Handler(req.r) return pattern } func (req *Req) Set(key string, val string) { req.Data[key] = val } func (req *Req) Get(key string) string { return req.Data[key] }