router.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package router
  2. import (
  3. "gmanager/app/api/common"
  4. "gmanager/app/api/config"
  5. "gmanager/app/api/department"
  6. "gmanager/app/api/game"
  7. "gmanager/app/api/gm"
  8. "gmanager/app/api/log"
  9. "gmanager/app/api/menu"
  10. "gmanager/app/api/role"
  11. "gmanager/app/api/user"
  12. "gmanager/app/component/middle"
  13. "gmanager/app/component/started"
  14. "gmanager/app/constants"
  15. "gmanager/library/base"
  16. "strings"
  17. "github.com/goflyfox/gtoken/gtoken"
  18. "github.com/gogf/gf/frame/g"
  19. "github.com/gogf/gf/net/ghttp"
  20. "github.com/gogf/gf/os/glog"
  21. )
  22. /*
  23. 绑定业务路由
  24. */
  25. func bindRouter() {
  26. urlPath := g.Config().GetString("url-path")
  27. s := g.Server()
  28. // 中间件
  29. // 允许跨域
  30. s.BindMiddleware("/*", func(r *ghttp.Request) {
  31. r.Response.CORSDefault()
  32. r.Middleware.Next()
  33. })
  34. // 日志拦截
  35. s.BindMiddleware(urlPath+"/*", middle.MiddlewareLog)
  36. // 通用属性
  37. s.BindMiddleware(urlPath+"/*", middle.MiddlewareCommon)
  38. // 首页
  39. s.BindHandler(urlPath+"/", common.Login)
  40. s.BindHandler(urlPath+"/main.html", common.Index)
  41. s.BindHandler(urlPath+"/login", common.Login)
  42. s.BindHandler(urlPath+"/welcome", common.Welcome)
  43. s.BindHandler(urlPath+"/admin/welcome.html", common.Welcome)
  44. s.BindHandler("/SendGM", common.GM)
  45. s.BindHandler("/SendMail", common.SendMail)
  46. gmAction := new(gm.Action)
  47. //group.ALL("gm", gmAction)
  48. s.BindHandler("/gm/downloadcdk/{ticke}", gmAction.DownloadCdk)
  49. s.Group(urlPath+"/system", func(group *ghttp.RouterGroup) {
  50. userAction := new(user.Action)
  51. group.ALL("user", userAction)
  52. group.GET("/user/get/{id}", userAction.Get)
  53. group.ALL("user/delete/{id}", userAction.Delete)
  54. departAction := new(department.Action)
  55. group.ALL("department", departAction)
  56. group.GET("/department/get/{id}", departAction.Get)
  57. group.ALL("/department/delete/{id}", departAction.Delete)
  58. logAction := new(log.Action)
  59. group.ALL("log", logAction)
  60. group.GET("/log/get/{id}", logAction.Get)
  61. group.ALL("/log/delete/{id}", logAction.Delete)
  62. menuAction := new(menu.Action)
  63. group.ALL("menu", menuAction)
  64. group.GET("/menu/get/{id}", menuAction.Get)
  65. group.ALL("/menu/delete/{id}", menuAction.Delete)
  66. roleAction := new(role.Action)
  67. group.ALL("role", roleAction)
  68. group.GET("/role/get/{id}", roleAction.Get)
  69. group.ALL("/role/delete/{id}", roleAction.Delete)
  70. configAction := new(config.Action)
  71. group.ALL("config", configAction)
  72. group.GET("/config/get/{id}", configAction.Get)
  73. group.ALL("/config/delete/{id}", configAction.Delete)
  74. gameAction := new(game.Action)
  75. group.ALL("game", gameAction)
  76. group.GET("/game/getserverdetail/{id}", gameAction.GetServerDetail)
  77. group.ALL("/game/delserver/{id}", gameAction.DelServer)
  78. group.GET("/game/getwhitelistdetail/{id}", gameAction.GetWhiteListDetail)
  79. group.ALL("/game/delwhitelist/{id}", gameAction.DelWhiteList)
  80. group.GET("/game/getblacklistdetail/{id}", gameAction.GetBlackListDetail)
  81. group.ALL("/game/delblacklist/{id}", gameAction.DelBlackList)
  82. group.GET("/game/getnoticeinfodetail/{id}", gameAction.GetNoticeInfoDetail)
  83. group.ALL("/game/delnoticeinfo/{id}", gameAction.DelNoticeInfo)
  84. group.GET("/game/setsilence/{id}", gameAction.SetSilence)
  85. group.GET("/game/getserverlist", gameAction.GetServerList)
  86. gmAction := new(gm.Action)
  87. //group.ALL("/gm/sendmail", gmAction.SendMail)
  88. group.ALL("gm", gmAction)
  89. //group.ALL("/gm/downloadcdk/{cdkid}", gmAction.DownloadCdk)
  90. //group.GET("/gm/get/{id}", gmAction.Get)
  91. //group.ALL("/gm/delete/{id}", gmAction.Delete)
  92. })
  93. // 启动gtoken
  94. base.Token = &gtoken.GfToken{
  95. //Timeout: 10 * 1000,
  96. CacheMode: g.Config().GetInt8("gtoken.cache-mode"),
  97. MultiLogin: g.Config().GetBool("gtoken.multi-login"),
  98. LoginPath: urlPath + "/login/submit",
  99. LoginBeforeFunc: common.LoginSubmit,
  100. LogoutPath: urlPath + "/user/logout",
  101. LogoutBeforeFunc: common.LogoutBefore,
  102. AuthPaths: g.SliceStr{"/user", "/system"},
  103. GlobalMiddleware: true,
  104. AuthBeforeFunc: func(r *ghttp.Request) bool {
  105. // 静态页面不拦截
  106. if r.IsFileRequest() {
  107. return false
  108. }
  109. if strings.HasSuffix(r.URL.Path, "index") {
  110. return false
  111. }
  112. return true
  113. },
  114. }
  115. base.Token.Start()
  116. }
  117. /*
  118. 统一路由注册
  119. */
  120. func init() {
  121. glog.Info("########router start...")
  122. s := g.Server()
  123. // 绑定路由
  124. bindRouter()
  125. if constants.DEBUG {
  126. g.DB().SetDebug(constants.DEBUG)
  127. }
  128. // 上线建议关闭
  129. s.BindHandler("/debug", common.Debug)
  130. // 301错误页面
  131. s.BindStatusHandler(301, common.Error301)
  132. // 404错误页面
  133. s.BindStatusHandler(404, common.Error404)
  134. // 500错误页面
  135. s.BindStatusHandler(500, common.Error500)
  136. // 某些浏览器直接请求favicon.ico文件,特别是产生404时
  137. s.SetRewrite("/favicon.ico", "/resources/images/favicon.ico")
  138. // 管理接口
  139. s.EnableAdmin("/admin")
  140. // 为平滑重启管理页面设置HTTP Basic账号密码
  141. //s.BindHookHandler("/admin/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
  142. // user := g.Config().GetString("admin.user")
  143. // pass := g.Config().GetString("admin.pass")
  144. // if !r.BasicAuth(user, pass) {
  145. // r.ExitAll()
  146. // }
  147. //})
  148. // 强制跳转到HTTPS访问
  149. //g.Server().BindHookHandler("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
  150. // if !r.IsFileServe() && r.TLS == nil {
  151. // r.Response.RedirectTo(fmt.Sprintf("https://%s%s", r.Host, r.URL.String()))
  152. // r.ExitAll()
  153. // }
  154. //})
  155. started.StartLog()
  156. glog.Info("########router finish.")
  157. }