package fiber import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/limiter" jwt "github.com/gofiber/jwt/v3" adminHandler "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler/admin" ) func initAdminHandlers(app *fiber.App, sp ServiceProvider) { admin := app.Group("/admin", jwt.New(getJWTConfig(sp))) admin.Get( "/", adminHandler.ArticleHandler(sp.ArticleRepository()), ) admin.All("/login", limiter.New(limiter.Config{ Max: int(sp.ConfigService().LoginRateLimiterMaxRequests()), Expiration: sp.ConfigService().LoginRateLimiterExpiration(), }), adminHandler.LoginHandler( sp.ConfigService(), sp.AuthService(), sp.UserRepository(), )) admin.All( "/logout", adminHandler.LogoutHandler(sp.ConfigService()), ) admin.All( "/user/change-password", adminHandler.ChangePassword( sp.AuthService(), sp.UserRepository(), ), ) admin.All( "/article/add", adminHandler.AddArticleHandler( sp.TransactionManager(), sp.ArticleRepository(), sp.TagRepository(), sp.ArticleTagRepository(), ), ) admin.All( "/article/edit/:id", adminHandler.EditArticleHandler( sp.TransactionManager(), sp.ArticleRepository(), sp.TagRepository(), sp.ArticleTagRepository(), sp.CacheService(), ), ) admin.All( "/article/delete/:id", adminHandler.DeleteArticleHandler( sp.TransactionManager(), sp.ArticleRepository(), sp.ArticleTagRepository(), sp.CacheService(), ), ) admin.Get( "/tag", adminHandler.TagHandler(sp.TagRepository()), ) admin.All( "/tag/add", adminHandler.AddTagHandler( sp.TagRepository(), ), ) admin.All( "/tag/edit/:id", adminHandler.EditTagHandler( sp.TagRepository(), sp.CacheService(), ), ) admin.All( "/tag/delete/:id", adminHandler.DeleteTagHandler( sp.TagRepository(), sp.CacheService(), ), ) } // nolint func getJWTConfig(sp ServiceProvider) jwt.Config { return jwt.Config{ SigningKey: []byte(sp.ConfigService().JWTSecretKey()), TokenLookup: "cookie:" + sp.ConfigService().JWTCookie(), ErrorHandler: func(fctx *fiber.Ctx, err error) error { return fctx.Redirect("/admin/login") }, Filter: func(fctx *fiber.Ctx) bool { method := fctx.Method() path := fctx.Path() if method != fiber.MethodGet && method != fiber.MethodPost && method != fiber.MethodPut && method != fiber.MethodDelete { return true } if path == "/admin/login" { return true } return false }, } }