Initial commit
This commit is contained in:
69
application/application.go
Normal file
69
application/application.go
Normal file
@@ -0,0 +1,69 @@
|
||||
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 := make(inmemorycache.Cache)
|
||||
|
||||
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)
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user