MW usage refactor

This commit is contained in:
nquidox 2026-03-21 15:21:30 +03:00
parent 229eebcf66
commit 21b54c4167
9 changed files with 98 additions and 48 deletions

View file

@ -65,6 +65,7 @@ func New(ctx context.Context, cfg config.Config) *App {
DB: dbPool,
Utils: u,
})
newApp.modules = append(newApp.modules, userProv)
newApp.router = router.NewRouter(router.Deps{
Host: cfg.Http.Host,
@ -76,15 +77,11 @@ func New(ctx context.Context, cfg config.Config) *App {
ServiceId: serviceId,
})
//modules
var modules []Module
m := merch.New(merch.Deps{
DB: dbPool,
Utils: u,
})
modules = append(modules, m)
newApp.modules = modules
newApp.modules = append(newApp.modules, m)
newApp.tasker = task.New(task.Deps{
Addr: "",
@ -97,8 +94,11 @@ func New(ctx context.Context, cfg config.Config) *App {
func (app *App) Run(ctx context.Context) error {
log.Infof("%v starting...", pkgLogHeader)
//getting middlewares for modules routes registration
mws := app.router.MWSet()
baseGroup := app.router.BaseGroup()
app.collectRoutes(baseGroup)
app.collectRoutes(baseGroup, mws)
errCh := make(chan error, 10)
@ -140,9 +140,9 @@ func (app *App) shutdown(ctx context.Context) {
log.Infof("%v shutdown complete", pkgLogHeader)
}
func (app *App) collectRoutes(group *gin.RouterGroup) {
func (app *App) collectRoutes(group *gin.RouterGroup, mw *router.Middlewares) {
for _, m := range app.modules {
m.RegisterRoutes(group)
m.RegisterRoutes(group, mw)
}
log.Infof("%v routes registered", pkgLogHeader)
}
@ -169,7 +169,7 @@ func (app *App) isRegistered(ctx context.Context, cfg config.Config) (int32, boo
}
if response == nil {
log.Error("%v error checking registration in auth service", pkgLogHeader)
log.Errorf("%v error checking registration in auth service", pkgLogHeader)
return 0, false
}

View file

@ -1,7 +1,10 @@
package app
import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
"merch-api/pkg/router"
)
type Module interface {
RegisterRoutes(r *gin.RouterGroup)
RegisterRoutes(r *gin.RouterGroup, mw *router.Middlewares)
}

View file

@ -6,6 +6,7 @@ import (
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"merch-api/pkg/responses"
"merch-api/pkg/router"
"merch-api/pkg/utils"
"net/http"
)
@ -24,8 +25,9 @@ func newController(s *service, u utils.Utils) *controller {
}
}
func (h *Handler) RegisterRoutes(r *gin.RouterGroup) {
func (h *Handler) RegisterRoutes(r *gin.RouterGroup, mw *router.Middlewares) {
merchGroup := r.Group("/merch")
merchGroup.Use(mw.AuthMW)
merchGroup.POST("/create", h.controller.create)
merchGroup.GET("/:id", h.controller.getOne)
@ -35,21 +37,28 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup) {
merchGroup.DELETE("/:id", h.controller.deleteMerch)
originsGroup := merchGroup.Group("/origins")
originsGroup.Use(mw.AuthMW)
originsGroup.POST("", h.controller.createOrigin)
originsGroup.GET("", h.controller.getOrigins)
originsGroup.DELETE("", h.controller.deleteOrigin)
chartsGroup := r.Group("/prices")
chartsGroup.Use(mw.AuthMW)
chartsGroup.GET("", h.controller.getChartsPrices)
chartsGroup.GET("/:uuid", h.controller.getDistinctPrices)
zeroPricesGroup := merchGroup.Group("/zeroprices")
zeroPricesGroup.Use(mw.AuthMW)
zeroPricesGroup.GET("", h.controller.getZeroPrices)
zeroPricesGroup.DELETE("", h.controller.deleteZeroPrices)
zeroPricesGroup.DELETE("/period", h.controller.deleteZeroPricesPeriod)
labelsGroup := merchGroup.Group("/labels")
labelsGroup.Use(mw.AuthMW)
labelsGroup.POST("", h.controller.createLabel)
labelsGroup.GET("", h.controller.getLabels)
labelsGroup.PUT("/:uuid", h.controller.updateLabel)

View file

@ -37,7 +37,7 @@ func setupTestRouter(mockRepo Repository) *gin.Engine {
c.Next()
})
handler.RegisterRoutes(router.Group(""))
handler.RegisterRoutes(router.Group(""), nil)
return router
}

View file

@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"merch-api/internal/appLog"
"merch-api/pkg/responses"
"merch-api/pkg/router"
"merch-api/pkg/utils"
"net/http"
)
@ -22,11 +23,11 @@ func newController(s *service, utils utils.Utils) *controller {
}
}
func (h *Handler) RegisterRoutes(r *gin.RouterGroup) {
func (h *Handler) RegisterRoutes(r *gin.RouterGroup, mw *router.Middlewares) {
userGroup := r.Group("/user")
userGroup.POST("", h.controller.create)
userGroup.DELETE("", h.controller.delete)
userGroup.POST("", mw.RegMW, h.controller.create)
userGroup.DELETE("", mw.AuthMW, h.controller.delete)
}
// create godoc

View file

@ -1,7 +0,0 @@
package user
import "context"
type Provider interface {
GetUserId(ctx context.Context, userUuid string) (int64, error)
}