mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
improve middleware feature of ghttp.Server; add MapStrAny function for gmap/gtree
This commit is contained in:
44
.example/net/ghttp/server/middleware/auth_exception.go
Normal file
44
.example/net/ghttp/server/middleware/auth_exception.go
Normal 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()
|
||||
}
|
||||
@ -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"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user