123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package app
- import (
- "database/sql"
- "github.com/dmitriygnatenko/internal/interfaces"
- "github.com/dmitriygnatenko/internal/repositories"
- cacheService "github.com/dmitriygnatenko/internal/services/cache"
- configService "github.com/dmitriygnatenko/internal/services/config"
- dbService "github.com/dmitriygnatenko/internal/services/db"
- envService "github.com/dmitriygnatenko/internal/services/env"
- mailService "github.com/dmitriygnatenko/internal/services/mailer"
- routerService "github.com/dmitriygnatenko/internal/services/router"
- "github.com/gofiber/fiber/v2"
- "github.com/gofiber/fiber/v2/middleware/cors"
- "github.com/gofiber/fiber/v2/middleware/recover"
- )
- const webDir = "../../web"
- type App struct {
- db *sql.DB
- env interfaces.IEnv
- cache interfaces.ICache
- mailer interfaces.IMailer
- articleRepository interfaces.IArticleRepository
- tagRepository interfaces.ITagRepository
- fiber *fiber.App
- }
- func Init() (*App, error) {
- app := &App{}
- // Init env service
- env, err := envService.Init()
- if err != nil {
- return nil, err
- }
- app.env = env
- // Init DB
- db, err := dbService.Init(app.GetEnvService())
- if err != nil {
- return nil, err
- }
- app.db = db
- // Init repositories
- app.articleRepository = repositories.InitArticleRepository(app.GetDBService())
- app.tagRepository = repositories.InitTagRepository(app.GetDBService())
- // Init cache service
- app.cache = cacheService.Init()
- // Init mailer
- app.mailer = mailService.Init(app.GetEnvService())
- // Init fiber
- app.fiber = fiber.New(configService.Init(app))
- // Init routes and handlers
- routerService.ApplyRoutes(app)
- // Configure web root
- app.fiber.Static("/", webDir)
- // Configure CORS middleware
- app.fiber.Use(cors.New())
- // Configure recover middleware
- app.fiber.Use(recover.New())
- return app, nil
- }
- func (a *App) Run() error {
- return a.fiber.Listen(":" + a.env.GetAppPort())
- }
- func (a *App) GetDBService() *sql.DB {
- return a.db
- }
- func (a *App) GetEnvService() interfaces.IEnv {
- return a.env
- }
- func (a *App) GetCacheService() interfaces.ICache {
- return a.cache
- }
- func (a *App) GetMailerService() interfaces.IMailer {
- return a.mailer
- }
- func (a *App) GetArticleRepository() interfaces.IArticleRepository {
- return a.articleRepository
- }
- func (a *App) GetTagRepository() interfaces.ITagRepository {
- return a.tagRepository
- }
- func (a *App) GetFiber() *fiber.App {
- return a.fiber
- }
|