This repository has been archived on 2023-12-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
protect_trans_info/application/application.go

70 lines
1.3 KiB
Go

package application
import (
"context"
"fmt"
"sync"
"time"
"github.com/uptrace/bun"
"go.uber.org/zap"
"git.derfenix.pro/fenix/protect_trans_info/adapters/inmemorycache"
"git.derfenix.pro/fenix/protect_trans_info/application/repository"
"git.derfenix.pro/fenix/protect_trans_info/port/http"
)
func NewApplication(ctx context.Context, cfg Config, logger *zap.Logger) (*Application, error) {
db, err := repository.NewDB(cfg.DB)
if err != nil {
return nil, fmt.Errorf("new db: %w", err)
}
cache := inmemorycache.NewCache()
repo, err := repository.NewConnLogs(ctx, db, cache, logger, cfg.UpdateInterval)
if err != nil {
return nil, fmt.Errorf("conn log repo: %w", err)
}
service, err := http.NewService(cfg.HTTPHost, cfg.HTTPPort, repo, logger)
if err != nil {
return nil, fmt.Errorf("new service: %w", err)
}
return &Application{
cfg: cfg,
service: service,
db: db,
}, nil
}
type Application struct {
cfg Config
service *http.Service
db *bun.DB
}
func (a *Application) Start(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer wg.Done()
a.service.Start()
}()
}
func (a *Application) Stop(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
a.service.Stop(ctx)
}()
}