package routes import ( "fmt" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" //SECTION api routers "github.com/metarare/metarare_api/api/admin" "github.com/metarare/metarare_api/api/collection" "github.com/metarare/metarare_api/api/explore" "github.com/metarare/metarare_api/api/health" "github.com/metarare/metarare_api/api/home" "github.com/metarare/metarare_api/api/sale" "github.com/metarare/metarare_api/api/test" "github.com/metarare/metarare_api/api/token" "github.com/metarare/metarare_api/api/user" "github.com/metarare/metarare_api/auth" common "github.com/metarare/metarare_api/common" "github.com/metarare/metarare_api/helpers" //SECTION swagger "github.com/metarare/metarare_api/docs" ginSwagger "github.com/swaggo/gin-swagger" swaggerFiles "github.com/swaggo/gin-swagger/swaggerFiles" ) var ( whiteList = []string{ "*", } allowMethods = []string{"OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"} allowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization", "uid"} ) // NOTE x project initialize func Initialize() common.Router { v := helpers.LoadEnvs() portNumber := v.GetString("port") gin.SetMode(gin.DebugMode) helpers.InitMysqlCommon(v) //CORS corsConfig := cors.DefaultConfig() corsConfig.AllowOrigins = whiteList corsConfig.AllowMethods = allowMethods corsConfig.AllowHeaders = allowHeaders corsConfig.AllowCredentials = true g := gin.Default() g.Use(cors.New(corsConfig)) return common.Router{ Engine: g, Port: portNumber, Db: common.DataBases{ MasterDB: helpers.MasterDB, ReadDB: helpers.ReadDB, }, Env: v, } } // NOTE x project run func Start(r common.Router) { auth.Init(r.Env) _r := endpointManagement(r) _r.Engine.Run(fmt.Sprintf(":%s", _r.Port)) } // NOTE x swagger setting func endpointManagement(r common.Router) common.Router { // programatically set swagger info docs.SwaggerInfo.Title = "metarare api docs" docs.SwaggerInfo.Description = "metarare api docs" docs.SwaggerInfo.Version = "1.0" docs.SwaggerInfo.Host = "apibeta.meta-rare.net" // docs.SwaggerInfo.Host = "localhost:9000" docs.SwaggerInfo.BasePath = "/v1" //SECTION current version r.Version = r.Engine.Group("/v1") //SECTION x endpoints by function health.NewHealthV1Router(r, "") home.NewHomeV1Router(r, "home") user.NewUserV1Router(r, "user") collection.NewCollectionV1Router(r, "collection") explore.NewExploreV1Router(r, "explore") token.NewTokenV1Router(r, "token") sale.NewSaleV1Router(r, "sale") test.NewTestV1Router(r, "test") //SECTION admin endpoints admin.NewAdminAuthV1Router(r, "admin/auth") admin.NewAdminUserV1Router(r, "admin/user") admin.NewAdminArtistV1Router(r, "admin/artist") admin.NewAdminCollectionV1Router(r, "admin/collection") admin.NewAdminSettingV1Router(r, "admin/setting") admin.NewAdminAdministratorV1Router(r, "admin/administrator") admin.NewAdminLogV1Router(r, "admin/log") r.Engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) return r }