From 183f940f65cb4e958cc4254564aa61d04d55d913 Mon Sep 17 00:00:00 2001 From: fancl Date: Wed, 20 Sep 2023 10:16:05 +0800 Subject: [PATCH] add context user supported --- entry/http/context.go | 70 ++++++++++++++++++++++++++++++++++++++--- entry/http/user.go | 21 +++++++++++++ entry/http/user_test.go | 12 +++++++ 3 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 entry/http/user.go create mode 100644 entry/http/user_test.go diff --git a/entry/http/context.go b/entry/http/context.go index f6b3177..1c7cbaa 100644 --- a/entry/http/context.go +++ b/entry/http/context.go @@ -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 diff --git a/entry/http/user.go b/entry/http/user.go new file mode 100644 index 0000000..62e7984 --- /dev/null +++ b/entry/http/user.go @@ -0,0 +1,21 @@ +package http + +type Userinfo struct { + ID string + Name string + variables map[string]string +} + +func (ui *Userinfo) Set(k, v string) { + if ui.variables == nil { + ui.variables = make(map[string]string) + } + ui.variables[k] = v +} + +func (ui *Userinfo) Get(k string) string { + if ui.variables == nil { + return "" + } + return ui.variables[k] +} diff --git a/entry/http/user_test.go b/entry/http/user_test.go new file mode 100644 index 0000000..7730ac5 --- /dev/null +++ b/entry/http/user_test.go @@ -0,0 +1,12 @@ +package http + +import "testing" + +func TestUserinfo_Set(t *testing.T) { + ui := &Userinfo{} + ui.Set("name", "xxx") + ui.Set("lost", "xxx") + if ui.Get("lost") != "xxx" { + t.Error("error") + } +}