experiment: successfully implemented dependency injections for controllers and services

This commit is contained in:
2025-06-20 16:14:55 +03:00
parent 654c1eb7b5
commit aab55a143f
10 changed files with 208 additions and 156 deletions

View File

@@ -1,27 +1,31 @@
// @title Easywish client API
// @version 1.0
// @description Easy and feature-rich wishlist.
// @license.name GPL 3.0
// @title Easywish client API
// @version 1.0
// @description Easy and feature-rich wishlist.
// @license.name GPL 3.0
// @BasePath /api/
// @Schemes http
// @BasePath /api/
// @Schemes http
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
package main
import (
"time"
"net/http"
"context"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"easywish/config"
"easywish/internal/controllers"
"easywish/internal/logger"
"easywish/internal/routes"
docs "easywish/docs"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"easywish/internal/services"
// swaggerfiles "github.com/swaggo/files"
// ginSwagger "github.com/swaggo/gin-swagger"
)
func main() {
@@ -32,12 +36,43 @@ func main() {
defer logger.Sync()
r := gin.Default()
r = routes.SetupRoutes(r)
fx.New(
services.Module,
fx.Provide(
// func() *gin.Engine {
// engine := gin.Default()
// engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
// return engine
// },
gin.New,
controllers.NewAuthController,
controllers.NewServiceController,
),
routes.Module,
docs.SwaggerInfo.Schemes = []string{"http"}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
r.Run(":8080")
fx.Invoke(func(lc fx.Lifecycle, router *gin.Engine) {
server := &http.Server{
Addr: ":8080",
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()
}