improve ghttp.CORSDefault

This commit is contained in:
John
2019-06-24 19:05:07 +08:00
parent ebed433dde
commit 05c7ba5f29
5 changed files with 63 additions and 4 deletions

View File

@ -0,0 +1,25 @@
package main
import (
"github.com/gogf/gf/g/net/ghttp"
"net/http"
)
func main() {
s := ghttp.GetServer()
s.BindHandler("/log/handler", func(r *ghttp.Request) {
r.Response.WriteStatus(http.StatusNotFound, "文件找不到了")
})
s.SetAccessLogEnabled(true)
s.SetErrorLogEnabled(true)
//s.SetLogHandler(func(r *ghttp.Request, error ...interface{}) {
// if len(error) > 0 {
// // 如果是错误日志
// fmt.Println("错误产生了:", error[0])
// }
// // 这里是请求日志
// fmt.Println("请求处理完成,请求地址:", r.URL.String(), "请求结果:", r.Response.Status)
//})
s.SetPort(8199)
s.Run()
}

View File

@ -0,0 +1,17 @@
package main
import (
"github.com/gogf/gf/g/net/ghttp"
)
func main() {
s := ghttp.GetServer()
s.BindHandler("/log/error", func(r *ghttp.Request) {
if j := r.GetJson(); j != nil {
r.Response.Write(j.Get("test"))
}
})
s.SetErrorLogEnabled(true)
s.SetPort(8199)
s.Run()
}

View File

@ -1,5 +1,24 @@
package main
func main() {
import (
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/net/ghttp"
)
type Order struct{}
func (order *Order) Get(r *ghttp.Request) {
r.Response.Write("GET")
}
func main() {
s := g.Server()
s.BindHookHandlerByMap("/api.v1/*any", map[string]ghttp.HandlerFunc{
"BeforeServe": func(r *ghttp.Request) {
r.Response.CORSDefault()
},
})
s.BindObjectRest("/api.v1/{.struct}", new(Order))
s.SetPort(8199)
s.Run()
}