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)
}