From 05c7ba5f298e23dbcb739b4df91cbabf596ca9ce Mon Sep 17 00:00:00 2001 From: John Date: Mon, 24 Jun 2019 19:05:07 +0800 Subject: [PATCH] improve ghttp.CORSDefault --- .gitignore | 1 - g/net/ghttp/ghttp_response_cors.go | 3 +-- geg/net/ghttp/server/log/log.go | 25 +++++++++++++++++++++++++ geg/net/ghttp/server/log/log_error.go | 17 +++++++++++++++++ geg/other/test.go | 21 ++++++++++++++++++++- 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 geg/net/ghttp/server/log/log.go create mode 100644 geg/net/ghttp/server/log/log_error.go diff --git a/.gitignore b/.gitignore index 93456099d..8470fac8f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,6 @@ .settings/ .vscode/ vender/ -log/ composer.lock gitpush.sh pkg/ diff --git a/g/net/ghttp/ghttp_response_cors.go b/g/net/ghttp/ghttp_response_cors.go index 708cfed8d..d5dcc9d36 100644 --- a/g/net/ghttp/ghttp_response_cors.go +++ b/g/net/ghttp/ghttp_response_cors.go @@ -8,7 +8,6 @@ package ghttp import ( - "github.com/gogf/gf/g/text/gstr" "github.com/gogf/gf/g/util/gconv" ) @@ -26,7 +25,7 @@ type CORSOptions struct { // 默认的CORS配置 func (r *Response) DefaultCORSOptions() CORSOptions { return CORSOptions{ - AllowOrigin: gstr.TrimRight(r.request.Referer(), "/"), + AllowOrigin: "*", AllowMethods: HTTP_METHODS, AllowCredentials: "true", MaxAge: 3628800, diff --git a/geg/net/ghttp/server/log/log.go b/geg/net/ghttp/server/log/log.go new file mode 100644 index 000000000..55ec3fb85 --- /dev/null +++ b/geg/net/ghttp/server/log/log.go @@ -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() +} diff --git a/geg/net/ghttp/server/log/log_error.go b/geg/net/ghttp/server/log/log_error.go new file mode 100644 index 000000000..02cc6cd11 --- /dev/null +++ b/geg/net/ghttp/server/log/log_error.go @@ -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() +} diff --git a/geg/other/test.go b/geg/other/test.go index 790580777..1b134b6c5 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -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() }