add GetError function for ghttp.Request

This commit is contained in:
John
2019-12-04 19:50:49 +08:00
parent a1edd83add
commit e1d4ba9d23
2 changed files with 39 additions and 5 deletions

View File

@ -1,15 +1,43 @@
package main
import (
"fmt"
"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 MiddlewareCORS(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
}
func MiddlewareLog(r *ghttp.Request) {
r.Middleware.Next()
g.Log().Println(r.Response.Status, r.URL.Path, r.GetError().Error())
}
func main() {
s := ghttp.GetServer()
s.BindHandler("/*", func(r *ghttp.Request) {
fmt.Println(r.URL.RawPath)
r.Response.Write(r.GetUrl())
s := g.Server()
s.SetConfigWithMap(g.Map{
"AccessLogEnabled": false,
"ErrorLogEnabled": false,
})
s.BindMiddlewareDefault(MiddlewareLog)
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
group.Middleware(MiddlewareAuth, MiddlewareCORS)
group.ALL("/user/list", func(r *ghttp.Request) {
panic("啊!我出错了!")
})
})
s.SetPort(8199)
s.Run()

View File

@ -161,3 +161,9 @@ func (r *Request) GetSessionId() string {
func (r *Request) GetReferer() string {
return r.Header.Get("Referer")
}
// GetError returns the error occurs in the procedure of the request.
// It returns nil if there's no error.
func (r *Request) GetError() error {
return r.error
}