app.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. articleTagRepository interfaces.IArticleTagRepository
  25. fiber *fiber.App
  26. }
  27. func Init() (*App, error) {
  28. app := &App{}
  29. // Init env service
  30. env, err := envService.Init()
  31. if err != nil {
  32. return nil, err
  33. }
  34. app.env = env
  35. // Init DB
  36. db, err := dbService.Init(app.GetEnvService())
  37. if err != nil {
  38. return nil, err
  39. }
  40. app.db = db
  41. // Init repositories
  42. app.articleRepository = repositories.InitArticleRepository(app.GetDBService())
  43. app.tagRepository = repositories.InitTagRepository(app.GetDBService())
  44. app.articleTagRepository = repositories.InitArticleTagRepository(app.GetDBService())
  45. // Init cache service
  46. app.cache = cacheService.Init()
  47. // Init mailer
  48. app.mailer = mailService.Init(app.GetEnvService())
  49. // Init fiber
  50. app.fiber = fiber.New(configService.Init(app))
  51. // Init routes and handlers
  52. routerService.ApplyRoutes(app)
  53. // Configure web root
  54. app.fiber.Static("/", webDir)
  55. // Configure CORS middleware
  56. app.fiber.Use(cors.New())
  57. // Configure recover middleware
  58. app.fiber.Use(recover.New())
  59. return app, nil
  60. }
  61. func (a *App) Run() error {
  62. return a.fiber.Listen(":" + a.env.GetAppPort())
  63. }
  64. func (a *App) GetDBService() *sql.DB {
  65. return a.db
  66. }
  67. func (a *App) GetEnvService() interfaces.IEnv {
  68. return a.env
  69. }
  70. func (a *App) GetCacheService() interfaces.ICache {
  71. return a.cache
  72. }
  73. func (a *App) GetMailerService() interfaces.IMailer {
  74. return a.mailer
  75. }
  76. func (a *App) GetArticleRepository() interfaces.IArticleRepository {
  77. return a.articleRepository
  78. }
  79. func (a *App) GetTagRepository() interfaces.ITagRepository {
  80. return a.tagRepository
  81. }
  82. func (a *App) GetArticleTagRepository() interfaces.IArticleTagRepository {
  83. return a.articleTagRepository
  84. }
  85. func (a *App) GetFiber() *fiber.App {
  86. return a.fiber
  87. }