/
appcontext.go
64 lines (51 loc) · 1.32 KB
/
appcontext.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
package appcontext
import (
"github.com/pkg/errors"
"gorm.io/gorm"
"github.com/caraml-dev/timber/dataset-service/config"
"github.com/caraml-dev/timber/dataset-service/mlp"
"github.com/caraml-dev/timber/dataset-service/service"
"github.com/caraml-dev/timber/dataset-service/storage"
)
// AppContext captures the config of all related internal services to run Dataset Service
type AppContext struct {
Services service.Services
DB *gorm.DB
}
// NewAppContext initializes a AppContext struct
func NewAppContext(cfg *config.Config) (*AppContext, error) {
// Init Services
var allServices service.Services
mlpSvc, err := mlp.NewClient(cfg.DatasetServiceConfig.MlpURL)
if err != nil {
return nil, errors.Wrapf(err, "Failed initializing MLP Service")
}
obsSvc, err := service.NewObservationService(
cfg.CommonDeploymentConfig,
cfg.ObservationServiceConfig,
)
if err != nil {
return nil, err
}
logWriterSvc, err := service.NewLogWriterService(
cfg.CommonDeploymentConfig,
cfg.LogWriterConfig,
)
if err != nil {
return nil, err
}
allServices = service.NewServices(
mlpSvc,
obsSvc,
logWriterSvc,
)
db, err := storage.InitDB(cfg.DatasetServiceConfig.DatabaseConfig)
if err != nil {
return nil, err
}
appContext := &AppContext{
Services: allServices,
DB: db,
}
return appContext, nil
}