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

@@ -0,0 +1,39 @@
package routes
import (
"easywish/internal/controllers"
"github.com/gin-gonic/gin"
)
func NewRouter(engine *gin.Engine, groups []RouteGroup) *gin.Engine {
apiGroup := engine.Group("/api")
for _, group := range groups {
subgroup := apiGroup.Group(group.BasePath)
subgroup.Use(group.Middleware...)
group.Router.RegisterRoutes(subgroup)
}
return engine
}
type RouteGroup struct {
BasePath string
Middleware []gin.HandlerFunc
Router controllers.Router
}
func NewRouteGroups(
authController controllers.AuthController,
serviceController controllers.ServiceController,
) []RouteGroup {
return []RouteGroup{
{
BasePath: "/auth",
Router: authController,
},
{
BasePath: "/service",
Router: serviceController,
},
}
}

View File

@@ -1,52 +1,12 @@
package routes
import (
"easywish/internal/controllers"
"easywish/internal/middleware"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
)
func SetupRoutes(r *gin.Engine) *gin.Engine {
apiGroup := r.Group("/api")
{
serviceGroup := apiGroup.Group("/service")
{
serviceGroup.GET("/health", controllers.HealthCheck)
}
authGroup := apiGroup.Group("/auth")
{
authGroup.POST("/registrationBegin", controllers.RegistrationBegin)
authGroup.POST("/registrationComplete", controllers.RegistrationComplete)
authGroup.POST("/login", controllers.Login)
authGroup.POST("/refresh", controllers.Refresh)
authGroup.POST("/passwordResetBegin", controllers.PasswordResetBegin)
authGroup.POST("/passwordResetComplete", controllers.PasswordResetComplete)
}
profileGroup := apiGroup.Group("/profile")
{
profileGroup.GET("/:username", controllers.GetProfile)
}
protected := apiGroup.Group("")
protected.Use(middleware.JWTAuthMiddleware())
{
accountGroup := protected.Group("/account")
{
accountGroup.PUT("/changePassword", controllers.ChangePassword)
}
profileGroup := protected.Group("/profile")
{
profileGroup.GET("/me", controllers.GetOwnProfile)
profileGroup.GET("/privacy", controllers.GetPrivacySettings)
profileGroup.PATCH("/privacy", controllers.UpdatePrivacySettings)
}
}
}
return r
}
var Module = fx.Module("routes",
fx.Provide(
NewRouteGroups,
),
fx.Invoke(NewRouter),
)