智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

router.go 1.2 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package router
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/hdl"
  5. "applet/app/mw"
  6. "github.com/gin-gonic/gin"
  7. )
  8. //初始化路由
  9. func Init() *gin.Engine {
  10. // debug, release, test 项目阶段
  11. mode := "release"
  12. if cfg.Debug {
  13. mode = "debug"
  14. }
  15. gin.SetMode(mode)
  16. //创建一个新的启动器
  17. r := gin.New()
  18. r.Use(mw.ChangeHeader)
  19. // 是否打印访问日志, 在非正式环境都打印
  20. if mode != "release" {
  21. r.Use(gin.Logger())
  22. }
  23. r.Use(gin.Recovery())
  24. // r.Use(mw.Limiter)
  25. //r.LoadHTMLGlob("static/html/*")
  26. r.GET("/favicon.ico", func(c *gin.Context) {
  27. c.Status(204)
  28. })
  29. r.NoRoute(func(c *gin.Context) {
  30. c.JSON(404, gin.H{"code": 404, "msg": "page not found", "data": []struct{}{}})
  31. })
  32. r.NoMethod(func(c *gin.Context) {
  33. c.JSON(405, gin.H{"code": 405, "msg": "method not allowed", "data": []struct{}{}})
  34. })
  35. r.Use(mw.Cors)
  36. routeInternal(r.Group("/api/v1/internal"))
  37. return r
  38. }
  39. func routeInternal(r *gin.RouterGroup) {
  40. r.Use(mw.DB) // 以下接口需要用到数据库
  41. {
  42. r.POST("/playlet_order", hdl.GetPlayletOrder)
  43. }
  44. r.Use(mw.Checker) // 以下接口需要检查Header: platform
  45. {
  46. }
  47. r.Use(mw.Auth) // 以下接口需要JWT验证
  48. {
  49. }
  50. }