admin_handlers.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/service/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(
  13. sp.ArticleRepository(),
  14. ),
  15. )
  16. admin.All("/login", limiter.New(limiter.Config{
  17. Max: int(sp.ConfigService().LoginRateLimiterMaxRequests()),
  18. Expiration: sp.ConfigService().LoginRateLimiterExpiration(),
  19. }), adminHandler.NewLoginPageHandler(
  20. sp.ConfigService(),
  21. sp.AuthService(),
  22. sp.UserRepository(),
  23. ))
  24. admin.All(
  25. "/logout",
  26. adminHandler.NewLogoutPageHandler(
  27. sp.ConfigService(),
  28. ),
  29. )
  30. admin.All(
  31. "/user/change-password",
  32. adminHandler.NewChangePasswordPageHandler(
  33. sp.AuthService(),
  34. sp.UserRepository(),
  35. ),
  36. )
  37. admin.All(
  38. "/article/add",
  39. adminHandler.NewAddArticlePageHandler(
  40. sp.TransactionManager(),
  41. sp.ArticleRepository(),
  42. sp.TagRepository(),
  43. sp.ArticleTagRepository(),
  44. sp.LanguageRepository(),
  45. sp.CacheService(),
  46. ),
  47. )
  48. admin.All(
  49. "/article/edit/:id<int>",
  50. adminHandler.NewEditArticlePageHandler(
  51. sp.TransactionManager(),
  52. sp.ArticleRepository(),
  53. sp.TagRepository(),
  54. sp.ArticleTagRepository(),
  55. sp.LanguageRepository(),
  56. sp.CacheService(),
  57. ),
  58. )
  59. admin.All(
  60. "/article/delete/:id<int>",
  61. adminHandler.NewDeleteArticlePageHandler(
  62. sp.TransactionManager(),
  63. sp.ArticleRepository(),
  64. sp.ArticleTagRepository(),
  65. sp.CacheService(),
  66. ),
  67. )
  68. admin.Get(
  69. "/tag",
  70. adminHandler.NewTagListPageHandler(
  71. sp.TagRepository(),
  72. ),
  73. )
  74. admin.All(
  75. "/tag/add",
  76. adminHandler.NewAddTagPageHandler(
  77. sp.TagRepository(),
  78. ),
  79. )
  80. admin.All(
  81. "/tag/edit/:id<int>",
  82. adminHandler.NewEditTagPageHandler(
  83. sp.TagRepository(),
  84. sp.LanguageRepository(),
  85. sp.CacheService(),
  86. ),
  87. )
  88. admin.All(
  89. "/tag/delete/:id<int>",
  90. adminHandler.NewDeleteTagPageHandler(
  91. sp.TagRepository(),
  92. sp.LanguageRepository(),
  93. sp.CacheService(),
  94. ),
  95. )
  96. }
  97. // nolint
  98. func getJWTConfig(sp ServiceProvider) jwt.Config {
  99. return jwt.Config{
  100. SigningKey: []byte(sp.ConfigService().JWTSecretKey()),
  101. TokenLookup: "cookie:" + sp.ConfigService().JWTCookie(),
  102. ErrorHandler: func(fctx *fiber.Ctx, err error) error {
  103. return fctx.Redirect("/admin/login")
  104. },
  105. Filter: func(fctx *fiber.Ctx) bool {
  106. method := fctx.Method()
  107. path := fctx.Path()
  108. if method != fiber.MethodGet && method != fiber.MethodPost &&
  109. method != fiber.MethodPut && method != fiber.MethodDelete {
  110. return true
  111. }
  112. if path == "/admin/login" {
  113. return true
  114. }
  115. return false
  116. },
  117. }
  118. }