admin_handlers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.ArticleHandler(sp.ArticleRepository()),
  13. )
  14. admin.All("/login", limiter.New(limiter.Config{
  15. Max: int(sp.ConfigService().LoginRateLimiterMaxRequests()),
  16. Expiration: sp.ConfigService().LoginRateLimiterExpiration(),
  17. }), adminHandler.LoginHandler(
  18. sp.ConfigService(),
  19. sp.AuthService(),
  20. sp.UserRepository(),
  21. ))
  22. admin.All(
  23. "/logout",
  24. adminHandler.LogoutHandler(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.AddArticleHandler(
  36. sp.TransactionManager(),
  37. sp.ArticleRepository(),
  38. sp.TagRepository(),
  39. sp.ArticleTagRepository(),
  40. ),
  41. )
  42. admin.All(
  43. "/article/edit/:id<int>",
  44. adminHandler.EditArticleHandler(
  45. sp.TransactionManager(),
  46. sp.ArticleRepository(),
  47. sp.TagRepository(),
  48. sp.ArticleTagRepository(),
  49. sp.CacheService(),
  50. ),
  51. )
  52. admin.All(
  53. "/article/delete/:id<int>",
  54. adminHandler.DeleteArticleHandler(
  55. sp.TransactionManager(),
  56. sp.ArticleRepository(),
  57. sp.ArticleTagRepository(),
  58. sp.CacheService(),
  59. ),
  60. )
  61. admin.Get(
  62. "/tag",
  63. adminHandler.TagHandler(sp.TagRepository()),
  64. )
  65. admin.All(
  66. "/tag/add",
  67. adminHandler.AddTagHandler(
  68. sp.TagRepository(),
  69. ),
  70. )
  71. admin.All(
  72. "/tag/edit/:id<int>",
  73. adminHandler.EditTagHandler(
  74. sp.TagRepository(),
  75. sp.CacheService(),
  76. ),
  77. )
  78. admin.All(
  79. "/tag/delete/:id<int>",
  80. adminHandler.DeleteTagHandler(
  81. sp.TagRepository(),
  82. sp.CacheService(),
  83. ),
  84. )
  85. }
  86. // nolint
  87. func getJWTConfig(sp ServiceProvider) jwt.Config {
  88. return jwt.Config{
  89. SigningKey: []byte(sp.ConfigService().JWTSecretKey()),
  90. TokenLookup: "cookie:" + sp.ConfigService().JWTCookie(),
  91. ErrorHandler: func(fctx *fiber.Ctx, err error) error {
  92. return fctx.Redirect("/admin/login")
  93. },
  94. Filter: func(fctx *fiber.Ctx) bool {
  95. method := fctx.Method()
  96. path := fctx.Path()
  97. if method != fiber.MethodGet && method != fiber.MethodPost &&
  98. method != fiber.MethodPut && method != fiber.MethodDelete {
  99. return true
  100. }
  101. if path == "/admin/login" {
  102. return true
  103. }
  104. return false
  105. },
  106. }
  107. }