router.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package router
  2. import (
  3. "github.com/dmitriygnatenko/internal/interfaces"
  4. "github.com/dmitriygnatenko/internal/services/handler"
  5. adminHandler "github.com/dmitriygnatenko/internal/services/handler/admin"
  6. "github.com/gofiber/fiber/v2"
  7. "github.com/gofiber/fiber/v2/middleware/basicauth"
  8. "github.com/gofiber/fiber/v2/middleware/monitor"
  9. )
  10. type Route struct {
  11. Method string
  12. Path string
  13. Handlers []fiber.Handler
  14. }
  15. func ApplyRoutes(app interfaces.IApp) {
  16. f := app.GetFiber()
  17. f.Get("/", handler.MainPageHandler(app))
  18. f.Get("/tag/:tag", handler.TagHandler(app))
  19. f.Get("/article/:article", handler.ArticleHandler(app))
  20. // Admin
  21. user := app.GetEnvService().GetAdminUser()
  22. password := app.GetEnvService().GetAdminPassword()
  23. if user != "" && password != "" {
  24. admin := f.Group("/admin", basicauth.New(basicauth.Config{
  25. Users: map[string]string{
  26. user: password,
  27. },
  28. }))
  29. admin.Get("/metrics", monitor.New(monitor.Config{}))
  30. admin.Get("/", adminHandler.ArticleHandler(app))
  31. admin.All("/article/add", adminHandler.AddArticleHandler(app))
  32. admin.All("/article/edit/:id<int>", adminHandler.EditArticleHandler(app))
  33. admin.All("/article/delete/:id<int>", adminHandler.DeleteArticleHandler(app))
  34. admin.Get("/tag", adminHandler.TagHandler(app))
  35. admin.All("/tag/add", adminHandler.AddTagHandler(app))
  36. admin.All("/tag/edit/:id<int>", adminHandler.EditTagHandler(app))
  37. admin.All("/tag/delete/:id<int>", adminHandler.DeleteTagHandler(app))
  38. }
  39. }