feat(login): 添加全局请求钩子并优化 CORS 配置
- 在 login 模块中添加了全局请求钩子 beforeServeHook - 实现了跨域请求的统一处理,增加了 localhost 的支持 - 优化了 i18n 信息的处理方式
This commit is contained in:
@@ -9,8 +9,10 @@ import (
|
||||
i18n "blazing/modules/base/middleware"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -26,6 +28,7 @@ var (
|
||||
|
||||
s := g.Server()
|
||||
s.SetServerAgent(cool.Config.Name)
|
||||
s.BindHookHandler("/*", ghttp.HookBeforeServe, beforeServeHook)
|
||||
runtime.SetMutexProfileFraction(1) // (非必需)开启对锁调用的跟踪
|
||||
runtime.SetBlockProfileRate(1) // (非必需)开启对阻塞操作的跟踪
|
||||
s.EnablePProf()
|
||||
@@ -35,8 +38,14 @@ var (
|
||||
}
|
||||
// i18n 信息
|
||||
s.BindHandler("/i18n", i18n.I18nInfo)
|
||||
// g.Server().BindMiddleware("/*", MiddlewareCORS)
|
||||
s.Run()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func beforeServeHook(r *ghttp.Request) {
|
||||
glog.Debugf(r.GetCtx(), "beforeServeHook [is file:%v] URI:%s", r.IsFileRequest(), r.RequestURI)
|
||||
r.Response.CORSDefault()
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
corsOptions.AllowDomain = []string{"*"}
|
||||
corsOptions.AllowDomain = []string{"*", "localhost"}
|
||||
if !r.Response.CORSAllowedOrigin(corsOptions) {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user