app.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package app
  2. import (
  3. "database/sql"
  4. "github.com/dmitriygnatenko/internal/interfaces"
  5. "github.com/dmitriygnatenko/internal/repositories"
  6. cacheService "github.com/dmitriygnatenko/internal/services/cache"
  7. configService "github.com/dmitriygnatenko/internal/services/config"
  8. dbService "github.com/dmitriygnatenko/internal/services/db"
  9. envService "github.com/dmitriygnatenko/internal/services/env"
  10. mailService "github.com/dmitriygnatenko/internal/services/mailer"
  11. routerService "github.com/dmitriygnatenko/internal/services/router"
  12. "github.com/gofiber/fiber/v2"
  13. "github.com/gofiber/fiber/v2/middleware/cors"
  14. "github.com/gofiber/fiber/v2/middleware/recover"
  15. )
  16. const webDir = "../../web"
  17. type App struct {
  18. db *sql.DB
  19. env interfaces.IEnv
  20. cache interfaces.ICache
  21. mailer interfaces.IMailer
  22. articleRepository interfaces.IArticleRepository
  23. tagRepository interfaces.ITagRepository
  24. fiber *fiber.App
  25. }
  26. func Init() (*App, error) {
  27. app := &App{}
  28. // Init env service
  29. env, err := envService.Init()
  30. if err != nil {
  31. return nil, err
  32. }
  33. app.env = env
  34. // Init DB
  35. db, err := dbService.Init(app.GetEnvService())
  36. if err != nil {
  37. return nil, err
  38. }
  39. app.db = db
  40. // Init repositories
  41. app.articleRepository = repositories.InitArticleRepository(app.GetDBService())
  42. app.tagRepository = repositories.InitTagRepository(app.GetDBService())
  43. // Init cache service
  44. app.cache = cacheService.Init()
  45. // Init mailer
  46. app.mailer = mailService.Init(app.GetEnvService())
  47. // Init fiber
  48. app.fiber = fiber.New(configService.Init(app))
  49. // Init routes and handlers
  50. routerService.ApplyRoutes(app)
  51. // Configure web root
  52. app.fiber.Static("/", webDir)
  53. // Configure CORS middleware
  54. app.fiber.Use(cors.New())
  55. // Configure recover middleware
  56. app.fiber.Use(recover.New())
  57. return app, nil
  58. }
  59. func (a *App) Run() error {
  60. return a.fiber.Listen(":" + a.env.GetAppPort())
  61. }
  62. func (a *App) GetDBService() *sql.DB {
  63. return a.db
  64. }
  65. func (a *App) GetEnvService() interfaces.IEnv {
  66. return a.env
  67. }
  68. func (a *App) GetCacheService() interfaces.ICache {
  69. return a.cache
  70. }
  71. func (a *App) GetMailerService() interfaces.IMailer {
  72. return a.mailer
  73. }
  74. func (a *App) GetArticleRepository() interfaces.IArticleRepository {
  75. return a.articleRepository
  76. }
  77. func (a *App) GetTagRepository() interfaces.ITagRepository {
  78. return a.tagRepository
  79. }
  80. func (a *App) GetFiber() *fiber.App {
  81. return a.fiber
  82. }