improve middleware feature of ghttp.Server; add MapStrAny function for gmap/gtree

This commit is contained in:
John
2019-09-19 19:44:46 +08:00
parent 62f66b4fec
commit b9fbfb91bd
10 changed files with 124 additions and 11 deletions

View File

@ -0,0 +1,44 @@
package main
import (
"net/http"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func MiddlewareAuth(r *ghttp.Request) {
token := r.Get("token")
if token == "123456" {
r.Middleware.Next()
} else {
r.Response.WriteStatus(http.StatusForbidden)
}
}
func main() {
s := g.Server()
s.Group("/admin", func(g *ghttp.RouterGroup) {
g.MiddlewarePattern("/*action", func(r *ghttp.Request) {
if action := r.GetRouterString("action"); action != "" {
switch action {
case "login":
r.Middleware.Next()
return
}
}
MiddlewareAuth(r)
})
g.ALL("/login", func(r *ghttp.Request) {
r.Response.Write("login")
})
g.ALL("/dashboard", func(r *ghttp.Request) {
r.Response.Write("dashboard")
})
g.ALL("/user", func(r *ghttp.Request) {
r.Response.Write("user")
})
})
s.SetPort(8199)
s.Run()
}

View File

@ -1,16 +1,12 @@
package main
import (
"fmt"
"reflect"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/os/gcmd"
"github.com/gogf/gf/os/glog"
)
func main() {
m, _ := gstr.Parse("map[a]=1&map[b]=2")
g.Dump(m)
fmt.Println(reflect.TypeOf(m["map"].(map[string]interface{})["b"]))
glog.SetFlags(glog.F_TIME_DATE | glog.F_TIME_TIME | glog.F_FILE_SHORT)
glog.Debug("dd")
glog.Println("timeout", gcmd.GetOpt("timeout"))
}