93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gin-gonic/gin"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
logger "github.com/rs/zerolog/log"
|
|
swaggerfiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
"log"
|
|
handler_products "online-order/api/handlers/products"
|
|
"online-order/api/middlewares"
|
|
"online-order/configs"
|
|
docs "online-order/docs"
|
|
"online-order/ent/migrate"
|
|
"online-order/entity"
|
|
)
|
|
|
|
// To load .env file
|
|
func init() {
|
|
configs.Initialize()
|
|
}
|
|
|
|
// @title Swagger Example API
|
|
// @version 1.0
|
|
// @description This is a sample server celler server.
|
|
// @termsOfService http://swagger.io/terms/
|
|
|
|
// @contact.name API Support
|
|
// @contact.url http://www.swagger.io/support
|
|
// @contact.email support@swagger.io
|
|
|
|
// @license.name Apache 2.0
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
// @host localhost:9000
|
|
// @BasePath /api/v1
|
|
|
|
// @securityDefinitions.basic BasicAuth
|
|
|
|
// @externalDocs.description OpenAPI
|
|
// @externalDocs.url https://swagger.io/resources/open-api/
|
|
func main() {
|
|
logger.Info().Msg("Server starting ...")
|
|
conf := configs.LoadConfigEnv()
|
|
|
|
// Start by connecting to database
|
|
db := configs.NewDBConnection()
|
|
defer db.Close()
|
|
|
|
// Run the automatic migration tool to create all schema resources.
|
|
ctx := context.Background()
|
|
err := db.Schema.Create(
|
|
ctx,
|
|
migrate.WithDropIndex(true),
|
|
migrate.WithDropColumn(true),
|
|
)
|
|
if err != nil {
|
|
log.Fatalf("failed creating schema resources: %v", err)
|
|
}
|
|
|
|
app := gin.Default()
|
|
|
|
docs.SwaggerInfo.BasePath = "/api/v1"
|
|
|
|
api_v1 := app.Group("api/v1")
|
|
api_restricted := app.Group("api/v1/in")
|
|
|
|
router_base := &entity.RouterBase{
|
|
Database: db,
|
|
OpenApp: api_v1,
|
|
}
|
|
router := &entity.Routers{
|
|
RouterBase: *router_base,
|
|
RestrictedApp: api_restricted,
|
|
}
|
|
|
|
api_v1.Use(middlewares.DomainMiddleware(router))
|
|
|
|
//middlewareController := middlewares.NewMiddlewareRouters(router)
|
|
//api_restricted.Use(middlewareController.JwAuthtMiddleware())
|
|
|
|
handler_products.NewProductRouters(router)
|
|
|
|
logger.Info().Msg("Server ready to go ...")
|
|
|
|
app.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler,
|
|
ginSwagger.URL("http://localhost:9000/swagger/doc.json"),
|
|
ginSwagger.DefaultModelsExpandDepth(-1)))
|
|
|
|
app.Run(":" + conf.ServerPort)
|
|
}
|