added: auth middleware

This commit is contained in:
nquidox 2025-07-07 17:46:31 +03:00
parent 099a586484
commit 7e0c1e71de
3 changed files with 147 additions and 3 deletions

View file

@ -6,18 +6,22 @@ import (
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"merch-parser-api/internal/interfaces"
"merch-parser-api/internal/shared"
"net/http"
)
type router struct {
apiPrefix string
engine *gin.Engine
ginMode string
apiPrefix string
engine *gin.Engine
ginMode string
excludeRoutes map[string]shared.ExcludeRoute
tokenProv interfaces.JWTProvider
}
type Deps struct {
ApiPrefix string
GinMode string
TokenProv interfaces.JWTProvider
}
func NewRouter(deps Deps) interfaces.Router {
@ -35,6 +39,7 @@ func NewRouter(deps Deps) interfaces.Router {
return &router{
apiPrefix: deps.ApiPrefix,
engine: engine,
tokenProv: deps.TokenProv,
}
}
@ -49,5 +54,23 @@ func (r *router) Set() *gin.Engine {
//swagger
r.engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.engine.Use(authMiddleware(mwDeps{
prefix: r.apiPrefix,
excludeRoutes: &r.excludeRoutes,
tokenProv: r.tokenProv,
}))
return r.engine
}
func (r *router) ExcludeRoutes(routes []shared.ExcludeRoute) {
log.Debug("Excluded routes:")
excludedRoutes := make(map[string]shared.ExcludeRoute, len(routes))
for _, route := range routes {
log.Debugf("%s %s", route.Method, route.Route)
ex := joinPaths(r.apiPrefix, route.Route)
excludedRoutes[ex] = shared.ExcludeRoute{Route: ex, Method: route.Method}
}
r.excludeRoutes = excludedRoutes
}