refactor: transitioned auth controller to use the new controller structure;
feat: setup DI for controllers; refactor: marked old utils and routes package parts as deprecated
This commit is contained in:
@@ -1,30 +1,72 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"easywish/internal/dto"
|
||||
"easywish/internal/middleware"
|
||||
"easywish/internal/services"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type SetupControllersParams struct {
|
||||
fx.In
|
||||
Controllers []Controller `group:"controllers"`
|
||||
Log *zap.Logger
|
||||
Auth services.AuthService
|
||||
Group *gin.Engine
|
||||
}
|
||||
|
||||
func setupControllers(p SetupControllersParams) {
|
||||
|
||||
apiGroup := p.Group.Group("/api")
|
||||
apiGroup.Use(middleware.AuthMiddleware(p.Log, p.Auth))
|
||||
apiGroup.Use(gin.HandlerFunc(func(c *gin.Context) {
|
||||
ip := c.ClientIP()
|
||||
userAgent := c.Request.UserAgent()
|
||||
sessionInfoFromCtx, ok := c.Get("session_info"); if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid or missing session data"})
|
||||
return
|
||||
}
|
||||
|
||||
sessionInfo := sessionInfoFromCtx.(dto.SessionInfo)
|
||||
|
||||
c.Set("client_info", dto.ClientInfo{
|
||||
SessionInfo: sessionInfo,
|
||||
IP: ip,
|
||||
UserAgent: userAgent,
|
||||
})
|
||||
|
||||
c.Next()
|
||||
}))
|
||||
for _, ctrl := range p.Controllers {
|
||||
ctrl.Setup(apiGroup, p.Log, p.Auth)
|
||||
}
|
||||
}
|
||||
|
||||
var Module = fx.Module("controllers",
|
||||
fx.Provide(
|
||||
NewServiceController,
|
||||
NewAuthController,
|
||||
NewProfileController,
|
||||
),
|
||||
fx.Annotate(NewAuthController, fx.ResultTags(`group:"controllers"`)),
|
||||
fx.Annotate(NewServiceController, fx.ResultTags(`group:"controllers"`)),
|
||||
),
|
||||
fx.Invoke(setupControllers),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user