-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
66 lines (52 loc) · 1.94 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package app
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/lungria/spendshelf-backend/account"
"github.com/lungria/spendshelf-backend/api"
"github.com/lungria/spendshelf-backend/app/config"
"github.com/lungria/spendshelf-backend/budget"
"github.com/lungria/spendshelf-backend/importer"
"github.com/lungria/spendshelf-backend/importer/mono"
"github.com/lungria/spendshelf-backend/storage"
"github.com/lungria/spendshelf-backend/transaction"
"github.com/lungria/spendshelf-backend/transaction/category"
"github.com/lungria/spendshelf-backend/transaction/interval"
"github.com/rs/zerolog/log"
)
// Initialize dependencies graph.
func Initialize() (*App, error) {
cfg, err := config.FromEnv()
if err != nil {
return nil, err
}
client := mono.NewClient(cfg.MonoBaseURL, cfg.MonoAPIKey)
// todo: ping API?
pool, err := pgxpool.Connect(context.Background(), cfg.DBConnString)
if err != nil {
return nil, err
}
api, acRepo, txRepo := initAPI(cfg, pool)
im := initImporter(cfg, client, acRepo, txRepo)
db := storage.NewWorker(pool)
state := NewApp(&log.Logger)
state.RegisterWorkers(api, im, db)
return state, nil
}
func initAPI(cfg config.Config, pool *pgxpool.Pool) (Worker, *account.Repository, *transaction.Repository) {
acRepo := account.NewRepository(pool)
acHandler := account.NewHandler(acRepo)
bgRepo := budget.NewRepository(pool)
bgHandler := budget.NewHandler(bgRepo)
ctRepo := category.NewRepository(pool)
txRepo := transaction.NewRepository(pool)
txHandler := transaction.NewHandler(txRepo, ctRepo)
return api.NewServer(cfg, acHandler, bgHandler, txHandler), acRepo, txRepo
}
func initImporter(cfg config.Config, cl *mono.Client, a *account.Repository, t *transaction.Repository) Worker {
gen := interval.NewGenerator(t)
acIm := account.NewImporter(cl, a)
txIm := transaction.NewImporter(cl, t, gen)
globalIm := importer.NewImporter(txIm, acIm)
return importer.NewWorker(globalIm, cfg.MonoAccountID)
}