Parcourir la source

add context user supported

fancl il y a 1 an
Parent
commit
183f940f65
3 fichiers modifiés avec 98 ajouts et 5 suppressions
  1. 65 5
      entry/http/context.go
  2. 21 0
      entry/http/user.go
  3. 12 0
      entry/http/user_test.go

+ 65 - 5
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

+ 21 - 0
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]
+}

+ 12 - 0
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")
+	}
+}