starter.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.Port = "8080"
  60. _r.Engine.Run(fmt.Sprintf(":%s", "8080"))
  61. }
  62. // NOTE x swagger setting
  63. func endpointManagement(r common.Router) common.Router {
  64. // programatically set swagger info
  65. docs.SwaggerInfo.Title = "metarare api docs"
  66. docs.SwaggerInfo.Description = "metarare api docs"
  67. docs.SwaggerInfo.Version = "1.0"
  68. docs.SwaggerInfo.Host = "apibeta.meta-rare.net"
  69. // docs.SwaggerInfo.Host = "localhost:9000"
  70. docs.SwaggerInfo.BasePath = "/v1"
  71. //SECTION current version
  72. r.Version = r.Engine.Group("/v1")
  73. //SECTION x endpoints by function
  74. health.NewHealthV1Router(r, "")
  75. home.NewHomeV1Router(r, "home")
  76. user.NewUserV1Router(r, "user")
  77. collection.NewCollectionV1Router(r, "collection")
  78. explore.NewExploreV1Router(r, "explore")
  79. token.NewTokenV1Router(r, "token")
  80. sale.NewSaleV1Router(r, "sale")
  81. test.NewTestV1Router(r, "test")
  82. //SECTION admin endpoints
  83. admin.NewAdminAuthV1Router(r, "admin/auth")
  84. admin.NewAdminUserV1Router(r, "admin/user")
  85. admin.NewAdminArtistV1Router(r, "admin/artist")
  86. admin.NewAdminCollectionV1Router(r, "admin/collection")
  87. admin.NewAdminSettingV1Router(r, "admin/setting")
  88. admin.NewAdminAdministratorV1Router(r, "admin/administrator")
  89. admin.NewAdminLogV1Router(r, "admin/log")
  90. r.Engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  91. return r
  92. }