-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
123 lines (94 loc) · 2.57 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cac
import (
"github.com/cloudentity/cac/internal/cac/api"
"github.com/cloudentity/cac/internal/cac/client"
"github.com/cloudentity/cac/internal/cac/config"
"github.com/cloudentity/cac/internal/cac/data"
"github.com/cloudentity/cac/internal/cac/logging"
"github.com/cloudentity/cac/internal/cac/storage"
"golang.org/x/exp/slog"
"strings"
)
type Application struct {
Config *config.Configuration
RootConfig *config.RootConfiguration
Client api.Source
Storage storage.Storage
Validator data.ValidatorApi
}
func InitApp(configPath string, profile string, tenant bool) (app *Application, err error) {
app = &Application{}
if app.RootConfig, err = config.InitConfig(configPath); err != nil {
return app, err
}
if app.Config, err = app.RootConfig.ForProfile(profile); err != nil {
return app, err
}
if err = logging.InitLogging(app.Config.Logging); err != nil {
return app, err
}
slog.Debug("config", "c", app.Config.Client)
if app.Config.Client != nil {
var c *client.Client
if c, err = client.InitClient(app.Config.Client); err != nil {
return app, err
}
app.Client = c
if tenant {
app.Client = c.Tenant()
}
}
var constructor = storage.InitServerStorage
app.Validator = &data.ServerValidator{}
if tenant {
constructor = storage.InitTenantStorage
app.Validator = &data.TenantValidator{}
}
if app.Config.Storage != nil {
if app.Storage, err = storage.InitMultiStorage(app.Config.Storage, constructor); err != nil {
return app, err
}
}
slog.Info("Initiated application")
return app, nil
}
func (a *Application) PickSource(source string, tenant bool) (api.Source, error) {
var (
conf *config.Configuration
sourceType api.SourceType
err error
profile, sourceS, found = strings.Cut(source, "@")
)
if !found {
sourceS = profile
profile = a.Config.Name
slog.With("profile", profile).With("source", sourceS).Debug("@ not found in source, using default profile")
}
if conf, err = a.RootConfig.ForProfile(profile); err != nil {
return nil, err
}
if sourceType, err = api.SourceFromString(sourceS); err != nil {
return nil, err
}
var constructor = storage.InitServerStorage
if tenant {
constructor = storage.InitTenantStorage
}
switch sourceType {
case api.SourceLocal:
return storage.InitMultiStorage(conf.Storage, constructor)
case api.SourceRemote:
var (
c *client.Client
err error
)
if c, err = client.InitClient(conf.Client); err != nil {
return nil, err
}
if tenant {
return c.Tenant(), nil
}
return c, nil
}
return nil, api.ErrUnknownSource
}