main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "github.com/goflyfox/gtoken/gtoken"
  4. "github.com/gogf/gf/frame/g"
  5. "github.com/gogf/gf/net/ghttp"
  6. "github.com/gogf/gf/os/glog"
  7. "github.com/gogf/gf/text/gstr"
  8. )
  9. var TestServerName string
  10. //var TestServerName string = "gtoken"
  11. func main() {
  12. g.Server(TestServerName).Run()
  13. }
  14. // 管理初始化顺序.
  15. func init() {
  16. initConfig()
  17. initRouter()
  18. }
  19. // 用于配置初始化.
  20. func initConfig() {
  21. glog.Info("########service start...")
  22. v := g.View()
  23. c := g.Config()
  24. s := g.Server(TestServerName)
  25. path := ""
  26. // 配置对象及视图对象配置
  27. c.AddPath(path + "config")
  28. v.SetDelimiters("${", "}")
  29. v.AddPath(path + "template")
  30. // glog配置
  31. logPath := c.GetString("log-path")
  32. glog.SetPath(logPath)
  33. glog.SetStdoutPrint(true)
  34. s.SetServerRoot("./public")
  35. s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
  36. s.SetLogPath(logPath)
  37. s.SetErrorLogEnabled(true)
  38. s.SetAccessLogEnabled(true)
  39. s.SetPort(c.GetInt("http-port"))
  40. glog.Info("########service finish.")
  41. }
  42. var gfToken *gtoken.GfToken
  43. /*
  44. 绑定业务路由
  45. */
  46. func bindRouter() {
  47. s := g.Server(TestServerName)
  48. s.Group("/", func(g *ghttp.RouterGroup) {
  49. g.Middleware(CORS)
  50. // 调试路由
  51. g.ALL("/hello", func(r *ghttp.Request) {
  52. r.Response.WriteJson(gtoken.Succ("hello"))
  53. })
  54. g.ALL("/system/user", func(r *ghttp.Request) {
  55. r.Response.WriteJson(gtoken.Succ("system user"))
  56. })
  57. g.ALL("/user/info", func(r *ghttp.Request) {
  58. r.Response.WriteJson(gtoken.Succ("user info"))
  59. })
  60. g.ALL("/system/user/info", func(r *ghttp.Request) {
  61. r.Response.WriteJson(gtoken.Succ("system user info"))
  62. })
  63. })
  64. loginFunc := Login
  65. // 启动gtoken
  66. gfToken := &gtoken.GfToken{
  67. ServerName: TestServerName,
  68. //Timeout: 10 * 1000,
  69. CacheMode: g.Config().GetInt8("gtoken.cache-mode"),
  70. LoginPath: "/login",
  71. LoginBeforeFunc: loginFunc,
  72. LogoutPath: "/user/logout",
  73. AuthPaths: g.SliceStr{"/user", "/system"}, // 这里是按照前缀拦截,拦截/user /user/list /user/add ...
  74. AuthExcludePaths: g.SliceStr{"/user/info", "/system/user/info"}, // 不拦截路径 /user/info,/system/user/info,/system/user,
  75. GlobalMiddleware: true, // 开启全局拦截
  76. MultiLogin: g.Config().GetBool("gtoken.multi-login"),
  77. }
  78. gfToken.Start()
  79. }
  80. /*
  81. 统一路由注册
  82. */
  83. func initRouter() {
  84. s := g.Server(TestServerName)
  85. // 绑定路由
  86. bindRouter()
  87. // 首页
  88. s.BindHandler("/", func(r *ghttp.Request) {
  89. content, err := g.View().Parse("index.html", map[string]interface{}{
  90. "id": 1,
  91. "name": "GTOKEN",
  92. "title": g.Config().GetString("setting.title"),
  93. })
  94. if err != nil {
  95. glog.Error(err)
  96. }
  97. r.Response.Write(content)
  98. })
  99. // 某些浏览器直接请求favicon.ico文件,特别是产生404时
  100. s.SetRewrite("/favicon.ico", "/resource/image/favicon.ico")
  101. }
  102. func Login(r *ghttp.Request) (string, interface{}) {
  103. username := r.GetString("username")
  104. passwd := r.GetString("passwd")
  105. if username == "" || passwd == "" {
  106. r.Response.WriteJson(gtoken.Fail("账号或密码错误."))
  107. r.ExitAll()
  108. }
  109. return username, "1"
  110. }
  111. func CORS(r *ghttp.Request) {
  112. opt := ghttp.CORSOptions{
  113. AllowOrigin: "*",
  114. AllowMethods: ghttp.HTTP_METHODS,
  115. AllowCredentials: "true",
  116. AllowHeaders: "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With,version,time,uuid,sign",
  117. MaxAge: 3628800,
  118. }
  119. if origin := r.Request.Header.Get("Origin"); origin != "" {
  120. opt.AllowOrigin = origin
  121. } else if referer := r.Request.Referer(); referer != "" {
  122. if p := gstr.PosR(referer, "/", 6); p != -1 {
  123. opt.AllowOrigin = referer[:p]
  124. } else {
  125. opt.AllowOrigin = referer
  126. }
  127. }
  128. r.Response.CORS(opt)
  129. r.Middleware.Next()
  130. }