admin_handlers.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package fiber
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "github.com/gofiber/fiber/v2/middleware/limiter"
  5. jwt "github.com/gofiber/jwt/v3"
  6. adminHandler "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler/admin"
  7. )
  8. func initAdminHandlers(app *fiber.App, sp ServiceProvider) {
  9. admin := app.Group("/admin", jwt.New(getJWTConfig(sp)))
  10. admin.Get(
  11. "/",
  12. adminHandler.NewArticleListPageHandler(sp.ArticleRepository()),
  13. )
  14. admin.All("/login", limiter.New(limiter.Config{
  15. Max: int(sp.ConfigService().LoginRateLimiterMaxRequests()),
  16. Expiration: sp.ConfigService().LoginRateLimiterExpiration(),
  17. }), adminHandler.NewLoginPageHandler(
  18. sp.ConfigService(),
  19. sp.AuthService(),
  20. sp.UserRepository(),
  21. ))
  22. admin.All(
  23. "/logout",
  24. adminHandler.NewLogoutPageHandler(sp.ConfigService()),
  25. )
  26. admin.All(
  27. "/user/change-password",
  28. adminHandler.ChangePassword(
  29. sp.AuthService(),
  30. sp.UserRepository(),
  31. ),
  32. )
  33. admin.All(
  34. "/article/add",
  35. adminHandler.NewArticleAddPageHandler(
  36. sp.TransactionManager(),
  37. sp.ArticleRepository(),
  38. sp.TagRepository(),
  39. sp.ArticleTagRepository(),
  40. sp.LanguageRepository(),
  41. sp.CacheService(),
  42. ),
  43. )
  44. admin.All(
  45. "/article/edit/:id<int>",
  46. adminHandler.NewArticleEditPageHandler(
  47. sp.TransactionManager(),
  48. sp.ArticleRepository(),
  49. sp.TagRepository(),
  50. sp.ArticleTagRepository(),
  51. sp.CacheService(),
  52. ),
  53. )
  54. admin.All(
  55. "/article/delete/:id<int>",
  56. adminHandler.NewArticleDeletePageHandler(
  57. sp.TransactionManager(),
  58. sp.ArticleRepository(),
  59. sp.ArticleTagRepository(),
  60. sp.CacheService(),
  61. ),
  62. )
  63. admin.Get(
  64. "/tag",
  65. adminHandler.TagHandler(sp.TagRepository()),
  66. )
  67. admin.All(
  68. "/tag/add",
  69. adminHandler.AddTagHandler(
  70. sp.TagRepository(),
  71. ),
  72. )
  73. admin.All(
  74. "/tag/edit/:id<int>",
  75. adminHandler.EditTagHandler(
  76. sp.TagRepository(),
  77. sp.CacheService(),
  78. ),
  79. )
  80. admin.All(
  81. "/tag/delete/:id<int>",
  82. adminHandler.DeleteTagHandler(
  83. sp.TagRepository(),
  84. sp.CacheService(),
  85. ),
  86. )
  87. }
  88. // nolint
  89. func getJWTConfig(sp ServiceProvider) jwt.Config {
  90. return jwt.Config{
  91. SigningKey: []byte(sp.ConfigService().JWTSecretKey()),
  92. TokenLookup: "cookie:" + sp.ConfigService().JWTCookie(),
  93. ErrorHandler: func(fctx *fiber.Ctx, err error) error {
  94. return fctx.Redirect("/admin/login")
  95. },
  96. Filter: func(fctx *fiber.Ctx) bool {
  97. method := fctx.Method()
  98. path := fctx.Path()
  99. if method != fiber.MethodGet && method != fiber.MethodPost &&
  100. method != fiber.MethodPut && method != fiber.MethodDelete {
  101. return true
  102. }
  103. if path == "/admin/login" {
  104. return true
  105. }
  106. return false
  107. },
  108. }
  109. }