fiber.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package fiber
  2. import (
  3. "html/template"
  4. "log"
  5. "strconv"
  6. "time"
  7. "github.com/dmitriygnatenko/internal/interfaces"
  8. "github.com/dmitriygnatenko/internal/services/handler"
  9. adminHandler "github.com/dmitriygnatenko/internal/services/handler/admin"
  10. "github.com/gofiber/fiber/v2"
  11. "github.com/gofiber/fiber/v2/middleware/basicauth"
  12. "github.com/gofiber/fiber/v2/middleware/cors"
  13. "github.com/gofiber/fiber/v2/middleware/monitor"
  14. "github.com/gofiber/fiber/v2/middleware/recover"
  15. "github.com/gofiber/template/html"
  16. )
  17. const (
  18. appName = "dmitriygnatenko"
  19. templatesPath = "./../../internal/templates"
  20. staticPath = "../../web"
  21. )
  22. func Init(sp interfaces.IServiceProvider) (*fiber.App, error) {
  23. fiberApp := fiber.New(getConfig(sp))
  24. // Configure web root
  25. fiberApp.Static("/", staticPath)
  26. // Configure CORS middleware
  27. fiberApp.Use(cors.New())
  28. // Configure recover middleware
  29. fiberApp.Use(recover.New())
  30. // Configure handlers
  31. fiberApp.Get("/", handler.MainPageHandler(sp))
  32. fiberApp.Get("/tag/:tag", handler.TagHandler(sp))
  33. fiberApp.Get("/article/:article", handler.ArticleHandler(sp))
  34. // Admin
  35. user := sp.GetEnvService().GetAdminUser()
  36. password := sp.GetEnvService().GetAdminPassword()
  37. if user != "" && password != "" {
  38. admin := fiberApp.Group("/admin", basicauth.New(basicauth.Config{
  39. Users: map[string]string{
  40. user: password,
  41. },
  42. }))
  43. admin.Get("/metrics", monitor.New(monitor.Config{}))
  44. admin.Get("/", adminHandler.ArticleHandler(sp))
  45. admin.All("/article/add", adminHandler.AddArticleHandler(sp))
  46. admin.All("/article/edit/:id<int>", adminHandler.EditArticleHandler(sp))
  47. admin.All("/article/delete/:id<int>", adminHandler.DeleteArticleHandler(sp))
  48. admin.Get("/tag", adminHandler.TagHandler(sp))
  49. admin.All("/tag/add", adminHandler.AddTagHandler(sp))
  50. admin.All("/tag/edit/:id<int>", adminHandler.EditTagHandler(sp))
  51. admin.All("/tag/delete/:id<int>", adminHandler.DeleteTagHandler(sp))
  52. }
  53. return fiberApp, nil
  54. }
  55. func getConfig(sp interfaces.IServiceProvider) fiber.Config {
  56. return fiber.Config{
  57. AppName: appName,
  58. DisableStartupMessage: true,
  59. Views: initEngine(sp.GetEnvService()),
  60. ErrorHandler: initErrorHandler(sp),
  61. }
  62. }
  63. func initEngine(env interfaces.IEnv) *html.Engine {
  64. engine := html.New(templatesPath, ".html")
  65. engine.AddFunc("now", func() time.Time {
  66. return time.Now()
  67. })
  68. engine.AddFunc("noescape", func(str string) template.HTML {
  69. return template.HTML(str)
  70. })
  71. engine.AddFunc("gridsep", func(i, l int) bool {
  72. i++
  73. return i%3 == 0 && i != l
  74. })
  75. engine.AddFunc("version", func() string {
  76. return env.GetStaticVersion()
  77. })
  78. engine.AddFunc("ga", func() bool {
  79. return env.IsGAEnabled()
  80. })
  81. return engine
  82. }
  83. func initErrorHandler(sp interfaces.IServiceProvider) fiber.ErrorHandler {
  84. return func(ctx *fiber.Ctx, err error) error {
  85. errCode := fiber.StatusInternalServerError
  86. errText := err.Error()
  87. if e, ok := err.(*fiber.Error); ok {
  88. errCode = e.Code
  89. }
  90. var renderData fiber.Map
  91. if errCode == fiber.StatusNotFound {
  92. renderData = fiber.Map{
  93. "pageTitle": "Страница не найдена",
  94. "code": fiber.StatusNotFound,
  95. "text": "Запрашиваемая вами страница не найдена",
  96. }
  97. } else {
  98. renderData = fiber.Map{
  99. "pageTitle": "Внутренняя ошибка",
  100. "code": fiber.StatusInternalServerError,
  101. "text": "Внутренняя ошибка сервера, идем исправлять...",
  102. }
  103. if errText != "" {
  104. sendErr := sp.GetMailerService().Send(
  105. sp.GetEnvService().GetAdminEmail(),
  106. "Ошибка на сайте dmitriygnatenko.ru",
  107. "Код: "+strconv.Itoa(errCode)+"\r\n"+"Текст: "+errText,
  108. )
  109. if sendErr != nil {
  110. log.Print(sendErr)
  111. }
  112. }
  113. }
  114. renderData["headTitle"] = "От слона к суслику - статьи про PHP, Go, алгоритмы"
  115. return ctx.Render("error", renderData, "_layout")
  116. }
  117. }