Files
easywish/backend/cmd/main.go

83 lines
1.6 KiB
Go

// @title Easywish client API
// @version 1.0
// @description Easy and feature-rich wishlist.
// @license.name GPL 3.0
// @BasePath /api/
// @Schemes http
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"easywish/config"
docs "easywish/docs"
"easywish/internal/controllers"
"easywish/internal/database"
"easywish/internal/logger"
"easywish/internal/routes"
"easywish/internal/services"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func main() {
if _, err := config.Load(); err != nil {
panic(err)
}
fx.New(
fx.Provide(
logger.NewLogger,
gin.Default,
),
database.Module,
services.Module,
controllers.Module,
routes.Module,
fx.Invoke(func(lc fx.Lifecycle, router *gin.Engine) {
// Swagger
docs.SwaggerInfo.Schemes = []string{"http"}
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
// Gin
server := &http.Server{
Addr: fmt.Sprintf(":%s", config.GetConfig().Port),
Handler: router,
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return server.Shutdown(shutdownCtx)
},
})
}),
).Run()
}