fiber.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package fiber
  2. import (
  3. "errors"
  4. "html/template"
  5. "strconv"
  6. "time"
  7. cache "git.dmitriygnatenko.ru/dima/go-common/cache/ttl_memory_cache"
  8. "git.dmitriygnatenko.ru/dima/go-common/db"
  9. "github.com/gofiber/fiber/v2"
  10. "github.com/gofiber/template/html/v2"
  11. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/repositories"
  12. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/auth"
  13. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/config"
  14. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
  15. )
  16. const (
  17. appName = "dmitriygnatenko"
  18. templatesPath = "./../../internal/templates"
  19. staticPath = "../../web"
  20. metricsURI = "/metrics"
  21. )
  22. type ServiceProvider interface {
  23. ConfigService() *config.Service
  24. AuthService() *auth.Service
  25. CacheService() *cache.Cache
  26. TransactionManager() *db.TxManager
  27. ArticleRepository() *repositories.ArticleRepository
  28. TagRepository() *repositories.TagRepository
  29. ArticleTagRepository() *repositories.ArticleTagRepository
  30. UserRepository() *repositories.UserRepository
  31. }
  32. func Init(sp ServiceProvider) (*fiber.App, error) {
  33. fiberApp := fiber.New(getConfig(sp))
  34. initStatic(fiberApp)
  35. initMiddleware(fiberApp, sp)
  36. initMetrics(fiberApp, sp)
  37. initPublicHandlers(fiberApp, sp)
  38. initAdminHandlers(fiberApp, sp)
  39. return fiberApp, nil
  40. }
  41. func getConfig(sp ServiceProvider) fiber.Config {
  42. return fiber.Config{
  43. AppName: appName,
  44. DisableStartupMessage: true,
  45. Views: getViewsEngine(sp),
  46. ErrorHandler: getErrorHandler(),
  47. }
  48. }
  49. func getViewsEngine(sp ServiceProvider) *html.Engine {
  50. engine := html.New(templatesPath, ".html")
  51. // nolint:gocritic
  52. engine.AddFunc("now", func() time.Time {
  53. return time.Now()
  54. })
  55. // nolint:gosec
  56. engine.AddFunc("noescape", func(str string) template.HTML {
  57. return template.HTML(str)
  58. })
  59. engine.AddFunc("gridsep", func(i, l int) bool {
  60. i++
  61. return i%3 == 0 && i != l
  62. })
  63. engine.AddFunc("version", func() string {
  64. return strconv.FormatUint(uint64(sp.ConfigService().StaticVersion()), 10)
  65. })
  66. return engine
  67. }
  68. func getErrorHandler() fiber.ErrorHandler {
  69. return func(fctx *fiber.Ctx, err error) error {
  70. lang := i18n.Language(fctx)
  71. errCode := fiber.StatusInternalServerError
  72. var e *fiber.Error
  73. if errors.As(err, &e) {
  74. errCode = e.Code
  75. }
  76. var renderData fiber.Map
  77. if errCode == fiber.StatusNotFound {
  78. renderData = fiber.Map{
  79. "pageTitle": i18n.T(lang, "page_not_found_err_title"),
  80. "code": fiber.StatusNotFound,
  81. "text": i18n.T(lang, "page_not_found_err_desc"),
  82. }
  83. } else {
  84. renderData = fiber.Map{
  85. "pageTitle": i18n.T(lang, "internal_err_title"),
  86. "code": fiber.StatusInternalServerError,
  87. "text": i18n.T(lang, "internal_err_desc"),
  88. }
  89. }
  90. renderData["headTitle"] = i18n.T(lang, "head_title")
  91. return fctx.Render("error", renderData, "_layout")
  92. }
  93. }