add context user supported

This commit is contained in:
fancl 2023-09-20 10:16:05 +08:00
parent 2db8293160
commit 183f940f65
3 changed files with 98 additions and 5 deletions

View File

@ -28,14 +28,24 @@ type Context struct {
req *http.Request req *http.Request
res http.ResponseWriter res http.ResponseWriter
params map[string]string params map[string]string
user *Userinfo
statusCode int statusCode int
} }
func (ctx *Context) reset(req *http.Request, res http.ResponseWriter, ps map[string]string) { func (ctx *Context) reset(req *http.Request, res http.ResponseWriter, ps map[string]string) {
ctx.statusCode = http.StatusOK ctx.statusCode = http.StatusOK
ctx.user = nil
ctx.req, ctx.res, ctx.params = req, res, ps 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 { func (ctx *Context) RealIp() string {
var ( var (
s string s string
@ -52,7 +62,7 @@ __end:
for { for {
if pos = strings.IndexByte(ipaddr, ','); pos > -1 { if pos = strings.IndexByte(ipaddr, ','); pos > -1 {
s = strings.TrimSpace(ipaddr[:pos]) 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 return s
} }
ipaddr = ipaddr[pos+1:] ipaddr = ipaddr[pos+1:]
@ -83,18 +93,30 @@ func (ctx *Context) Bind(v any) (err error) {
} }
func (ctx *Context) Query(k string) string { 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) return ctx.Request().FormValue(k)
} }
func (ctx *Context) Param(k string) string { func (ctx *Context) Param(k string) string {
var ( var (
ok bool ok bool
v string s string
) )
if v, ok = ctx.params[k]; ok { if s, ok = ctx.params[k]; ok {
return v 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) { func (ctx *Context) json(res responsePayload) (err error) {
@ -129,6 +151,44 @@ func (ctx *Context) SetCookie(cookie *http.Cookie) {
http.SetCookie(ctx.Response(), 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) { func (ctx *Context) SendFile(filename string) (err error) {
var ( var (
fi os.FileInfo fi os.FileInfo

21
entry/http/user.go 100644
View File

@ -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]
}

View File

@ -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")
}
}