|
@@ -5,7 +5,6 @@ import (
|
|
"github.com/gogf/gf/frame/g"
|
|
"github.com/gogf/gf/frame/g"
|
|
"github.com/gogf/gf/net/ghttp"
|
|
"github.com/gogf/gf/net/ghttp"
|
|
"github.com/gogf/gf/os/glog"
|
|
"github.com/gogf/gf/os/glog"
|
|
- "github.com/gogf/gf/text/gstr"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
var TestServerName string
|
|
var TestServerName string
|
|
@@ -13,54 +12,22 @@ var TestServerName string
|
|
//var TestServerName string = "gtoken"
|
|
//var TestServerName string = "gtoken"
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
- g.Server(TestServerName).Run()
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 管理初始化顺序.
|
|
|
|
-func init() {
|
|
|
|
- initConfig()
|
|
|
|
- initRouter()
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 用于配置初始化.
|
|
|
|
-func initConfig() {
|
|
|
|
glog.Info("########service start...")
|
|
glog.Info("########service start...")
|
|
|
|
|
|
- v := g.View()
|
|
|
|
- c := g.Config()
|
|
|
|
|
|
+ g.Cfg().SetPath("example/sample1")
|
|
s := g.Server(TestServerName)
|
|
s := g.Server(TestServerName)
|
|
-
|
|
|
|
- path := ""
|
|
|
|
- // 配置对象及视图对象配置
|
|
|
|
- c.AddPath(path + "config")
|
|
|
|
-
|
|
|
|
- v.SetDelimiters("${", "}")
|
|
|
|
- v.AddPath(path + "template")
|
|
|
|
-
|
|
|
|
- // glog配置
|
|
|
|
- logPath := c.GetString("log-path")
|
|
|
|
- glog.SetPath(logPath)
|
|
|
|
- glog.SetStdoutPrint(true)
|
|
|
|
-
|
|
|
|
- s.SetServerRoot("./public")
|
|
|
|
- s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
|
|
|
|
- s.SetLogPath(logPath)
|
|
|
|
- s.SetErrorLogEnabled(true)
|
|
|
|
- s.SetAccessLogEnabled(true)
|
|
|
|
- s.SetPort(c.GetInt("http-port"))
|
|
|
|
|
|
+ initRouter(s)
|
|
|
|
|
|
glog.Info("########service finish.")
|
|
glog.Info("########service finish.")
|
|
|
|
+ s.Run()
|
|
}
|
|
}
|
|
|
|
|
|
var gfToken *gtoken.GfToken
|
|
var gfToken *gtoken.GfToken
|
|
|
|
|
|
/*
|
|
/*
|
|
-绑定业务路由
|
|
|
|
|
|
+统一路由注册
|
|
*/
|
|
*/
|
|
-func bindRouter() {
|
|
|
|
-
|
|
|
|
- s := g.Server(TestServerName)
|
|
|
|
-
|
|
|
|
|
|
+func initRouter(s *ghttp.Server) {
|
|
s.Group("/", func(g *ghttp.RouterGroup) {
|
|
s.Group("/", func(g *ghttp.RouterGroup) {
|
|
g.Middleware(CORS)
|
|
g.Middleware(CORS)
|
|
|
|
|
|
@@ -94,36 +61,6 @@ func bindRouter() {
|
|
MultiLogin: g.Config().GetBool("gtoken.multi-login"),
|
|
MultiLogin: g.Config().GetBool("gtoken.multi-login"),
|
|
}
|
|
}
|
|
gfToken.Start()
|
|
gfToken.Start()
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/*
|
|
|
|
-统一路由注册
|
|
|
|
-*/
|
|
|
|
-func initRouter() {
|
|
|
|
-
|
|
|
|
- s := g.Server(TestServerName)
|
|
|
|
-
|
|
|
|
- // 绑定路由
|
|
|
|
- bindRouter()
|
|
|
|
-
|
|
|
|
- // 首页
|
|
|
|
- s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
- content, err := g.View().Parse("index.html", map[string]interface{}{
|
|
|
|
- "id": 1,
|
|
|
|
- "name": "GTOKEN",
|
|
|
|
- "title": g.Config().GetString("setting.title"),
|
|
|
|
- })
|
|
|
|
- if err != nil {
|
|
|
|
- glog.Error(err)
|
|
|
|
- }
|
|
|
|
- r.Response.Write(content)
|
|
|
|
-
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- // 某些浏览器直接请求favicon.ico文件,特别是产生404时
|
|
|
|
- s.SetRewrite("/favicon.ico", "/resource/image/favicon.ico")
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
func Login(r *ghttp.Request) (string, interface{}) {
|
|
func Login(r *ghttp.Request) (string, interface{}) {
|
|
@@ -138,23 +75,8 @@ func Login(r *ghttp.Request) (string, interface{}) {
|
|
return username, "1"
|
|
return username, "1"
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// 跨域
|
|
func CORS(r *ghttp.Request) {
|
|
func CORS(r *ghttp.Request) {
|
|
- opt := ghttp.CORSOptions{
|
|
|
|
- AllowOrigin: "*",
|
|
|
|
- AllowMethods: ghttp.HTTP_METHODS,
|
|
|
|
- AllowCredentials: "true",
|
|
|
|
- AllowHeaders: "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With,version,time,uuid,sign",
|
|
|
|
- MaxAge: 3628800,
|
|
|
|
- }
|
|
|
|
- if origin := r.Request.Header.Get("Origin"); origin != "" {
|
|
|
|
- opt.AllowOrigin = origin
|
|
|
|
- } else if referer := r.Request.Referer(); referer != "" {
|
|
|
|
- if p := gstr.PosR(referer, "/", 6); p != -1 {
|
|
|
|
- opt.AllowOrigin = referer[:p]
|
|
|
|
- } else {
|
|
|
|
- opt.AllowOrigin = referer
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- r.Response.CORS(opt)
|
|
|
|
|
|
+ r.Response.CORSDefault()
|
|
r.Middleware.Next()
|
|
r.Middleware.Next()
|
|
}
|
|
}
|