templates.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package fiber
  2. import (
  3. "errors"
  4. "github.com/gofiber/fiber/v2"
  5. "github.com/gofiber/template/html/v2"
  6. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper"
  7. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
  8. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/templates/functions"
  9. )
  10. func getViewsEngine(sp ServiceProvider) *html.Engine {
  11. engine := html.New(templatesPath, ".html")
  12. engine.AddFunc("now", functions.Now)
  13. engine.AddFunc("noescape", functions.NoEscape)
  14. engine.AddFunc("concat", functions.Concat)
  15. engine.AddFunc("gridsep", functions.GridSep)
  16. engine.AddFunc("version", functions.Version(sp))
  17. engine.AddFunc("trans", functions.Trans)
  18. engine.AddFunc("link", functions.Link)
  19. return engine
  20. }
  21. func getErrorHandler() fiber.ErrorHandler {
  22. return func(fctx *fiber.Ctx, err error) error {
  23. lang := mapper.LanguageFromContext(fctx)
  24. errCode := fiber.StatusInternalServerError
  25. var e *fiber.Error
  26. if errors.As(err, &e) {
  27. errCode = e.Code
  28. }
  29. var renderData fiber.Map
  30. if errCode == fiber.StatusNotFound {
  31. renderData = fiber.Map{
  32. "pageTitle": i18n.T(lang, "page_not_found_err_title"),
  33. "code": fiber.StatusNotFound,
  34. "text": i18n.T(lang, "page_not_found_err_desc"),
  35. }
  36. } else {
  37. renderData = fiber.Map{
  38. "pageTitle": i18n.T(lang, "internal_err_title"),
  39. "code": fiber.StatusInternalServerError,
  40. "text": i18n.T(lang, "internal_err_desc"),
  41. }
  42. }
  43. renderData["headTitle"] = i18n.T(lang, "head_title")
  44. return fctx.Render("error", renderData, "_layout")
  45. }
  46. }