starter.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package routes
  2. import (
  3. "fmt"
  4. "github.com/gin-contrib/cors"
  5. "github.com/gin-gonic/gin"
  6. //SECTION api routers
  7. "github.com/metarare/metarare_api/api/admin"
  8. "github.com/metarare/metarare_api/api/collection"
  9. "github.com/metarare/metarare_api/api/explore"
  10. "github.com/metarare/metarare_api/api/health"
  11. "github.com/metarare/metarare_api/api/home"
  12. "github.com/metarare/metarare_api/api/sale"
  13. "github.com/metarare/metarare_api/api/test"
  14. "github.com/metarare/metarare_api/api/token"
  15. "github.com/metarare/metarare_api/api/user"
  16. "github.com/metarare/metarare_api/auth"
  17. common "github.com/metarare/metarare_api/common"
  18. "github.com/metarare/metarare_api/helpers"
  19. //SECTION swagger
  20. "github.com/metarare/metarare_api/docs"
  21. ginSwagger "github.com/swaggo/gin-swagger"
  22. swaggerFiles "github.com/swaggo/gin-swagger/swaggerFiles"
  23. )
  24. var (
  25. whiteList = []string{
  26. "*",
  27. }
  28. allowMethods = []string{"OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"}
  29. allowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization", "uid"}
  30. )
  31. // NOTE x project initialize
  32. func Initialize() common.Router {
  33. v := helpers.LoadEnvs()
  34. portNumber := v.GetString("port")
  35. gin.SetMode(gin.DebugMode)
  36. helpers.InitMysqlCommon(v)
  37. //CORS
  38. corsConfig := cors.DefaultConfig()
  39. corsConfig.AllowOrigins = whiteList
  40. corsConfig.AllowMethods = allowMethods
  41. corsConfig.AllowHeaders = allowHeaders
  42. corsConfig.AllowCredentials = true
  43. g := gin.Default()
  44. g.Use(cors.New(corsConfig))
  45. return common.Router{
  46. Engine: g,
  47. Port: portNumber,
  48. Db: common.DataBases{
  49. MasterDB: helpers.MasterDB,
  50. ReadDB: helpers.ReadDB,
  51. },
  52. Env: v,
  53. }
  54. }
  55. // NOTE x project run
  56. func Start(r common.Router) {
  57. auth.Init(r.Env)
  58. _r := endpointManagement(r)
  59. _r.Engine.Run(fmt.Sprintf(":%s", _r.Port))
  60. }
  61. // NOTE x swagger setting
  62. func endpointManagement(r common.Router) common.Router {
  63. // programatically set swagger info
  64. docs.SwaggerInfo.Title = "metarare api docs"
  65. docs.SwaggerInfo.Description = "metarare api docs"
  66. docs.SwaggerInfo.Version = "1.0"
  67. docs.SwaggerInfo.Host = "apibeta.meta-rare.net"
  68. // docs.SwaggerInfo.Host = "localhost:9000"
  69. docs.SwaggerInfo.BasePath = "/v1"
  70. //SECTION current version
  71. r.Version = r.Engine.Group("/v1")
  72. //SECTION x endpoints by function
  73. health.NewHealthV1Router(r, "")
  74. home.NewHomeV1Router(r, "home")
  75. user.NewUserV1Router(r, "user")
  76. collection.NewCollectionV1Router(r, "collection")
  77. explore.NewExploreV1Router(r, "explore")
  78. token.NewTokenV1Router(r, "token")
  79. sale.NewSaleV1Router(r, "sale")
  80. test.NewTestV1Router(r, "test")
  81. //SECTION admin endpoints
  82. admin.NewAdminAuthV1Router(r, "admin/auth")
  83. admin.NewAdminUserV1Router(r, "admin/user")
  84. admin.NewAdminArtistV1Router(r, "admin/artist")
  85. admin.NewAdminCollectionV1Router(r, "admin/collection")
  86. admin.NewAdminSettingV1Router(r, "admin/setting")
  87. admin.NewAdminAdministratorV1Router(r, "admin/administrator")
  88. admin.NewAdminLogV1Router(r, "admin/log")
  89. r.Engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  90. return r
  91. }