|
@@ -28,14 +28,24 @@ type Context struct {
|
|
|
req *http.Request
|
|
|
res http.ResponseWriter
|
|
|
params map[string]string
|
|
|
+ user *Userinfo
|
|
|
statusCode int
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) reset(req *http.Request, res http.ResponseWriter, ps map[string]string) {
|
|
|
ctx.statusCode = http.StatusOK
|
|
|
+ ctx.user = nil
|
|
|
ctx.req, ctx.res, ctx.params = req, res, ps
|
|
|
}
|
|
|
|
|
|
+func (ctx *Context) User() *Userinfo {
|
|
|
+ return ctx.user
|
|
|
+}
|
|
|
+
|
|
|
+func (ctx *Context) SetUser(ui *Userinfo) {
|
|
|
+ ctx.user = ui
|
|
|
+}
|
|
|
+
|
|
|
func (ctx *Context) RealIp() string {
|
|
|
var (
|
|
|
s string
|
|
@@ -52,7 +62,7 @@ __end:
|
|
|
for {
|
|
|
if pos = strings.IndexByte(ipaddr, ','); pos > -1 {
|
|
|
s = strings.TrimSpace(ipaddr[:pos])
|
|
|
- if netip := net.ParseIP(s); netip != nil && !netip.IsPrivate() {
|
|
|
+ if netAddr := net.ParseIP(s); netAddr != nil && !netAddr.IsPrivate() {
|
|
|
return s
|
|
|
}
|
|
|
ipaddr = ipaddr[pos+1:]
|
|
@@ -83,18 +93,30 @@ func (ctx *Context) Bind(v any) (err error) {
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) Query(k string) string {
|
|
|
+ qs := ctx.Request().URL.Query()
|
|
|
+ if qs == nil {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ return qs.Get(k)
|
|
|
+}
|
|
|
+
|
|
|
+func (ctx *Context) Form(k string) string {
|
|
|
return ctx.Request().FormValue(k)
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) Param(k string) string {
|
|
|
var (
|
|
|
ok bool
|
|
|
- v string
|
|
|
+ s string
|
|
|
)
|
|
|
- if v, ok = ctx.params[k]; ok {
|
|
|
- return v
|
|
|
+ if s, ok = ctx.params[k]; ok {
|
|
|
+ return s
|
|
|
}
|
|
|
- return ctx.Request().FormValue(k)
|
|
|
+ s = ctx.Query(k)
|
|
|
+ if s == "" {
|
|
|
+ s = ctx.Form(k)
|
|
|
+ }
|
|
|
+ return s
|
|
|
}
|
|
|
|
|
|
func (ctx *Context) json(res responsePayload) (err error) {
|
|
@@ -129,6 +151,44 @@ func (ctx *Context) SetCookie(cookie *http.Cookie) {
|
|
|
http.SetCookie(ctx.Response(), cookie)
|
|
|
}
|
|
|
|
|
|
+func (ctx *Context) GetCookie(name string) (*http.Cookie, error) {
|
|
|
+ return ctx.Request().Cookie(name)
|
|
|
+}
|
|
|
+
|
|
|
+func (ctx *Context) DeleteCookie(name string) {
|
|
|
+ cookie, err := ctx.GetCookie(name)
|
|
|
+ if err == nil {
|
|
|
+ cookie.MaxAge = -1
|
|
|
+ ctx.SetCookie(cookie)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (ctx *Context) SetCookieValue(name, value, domain string) {
|
|
|
+ if domain == "" {
|
|
|
+ domain = ctx.Request().URL.Hostname()
|
|
|
+ }
|
|
|
+ if name == "" || value == "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ctx.SetCookie(&http.Cookie{
|
|
|
+ Name: name,
|
|
|
+ Value: value,
|
|
|
+ Path: "/",
|
|
|
+ Domain: domain,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func (ctx *Context) GetCookieValue(name string) string {
|
|
|
+ if name == "" {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ cookie, err := ctx.GetCookie(name)
|
|
|
+ if err == nil {
|
|
|
+ return cookie.Value
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+}
|
|
|
+
|
|
|
func (ctx *Context) SendFile(filename string) (err error) {
|
|
|
var (
|
|
|
fi os.FileInfo
|