/
builder.go
156 lines (129 loc) · 4.23 KB
/
builder.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package node
import (
"context"
"errors"
"github.com/Filecoin-Titan/titan/journal"
"github.com/Filecoin-Titan/titan/journal/alerting"
"github.com/Filecoin-Titan/titan/node/common"
"github.com/Filecoin-Titan/titan/node/modules"
"github.com/Filecoin-Titan/titan/node/modules/dtypes"
"github.com/Filecoin-Titan/titan/node/modules/helpers"
"github.com/Filecoin-Titan/titan/node/repo"
"github.com/Filecoin-Titan/titan/node/secret"
"github.com/gbrlsnchs/jwt/v3"
logging "github.com/ipfs/go-log/v2"
metricsi "github.com/ipfs/go-metrics-interface"
"go.uber.org/fx"
"golang.org/x/xerrors"
)
var log = logging.Logger("builder")
type invoke int
// Invokes are called in the order they are defined.
//
//nolint:golint
const (
// InitJournal at position 0 initializes the journal global var as soon as
// the system starts, so that it's available for all other components.
InitJournalKey = invoke(iota)
ExtractAPIKey
CheckFDLimit
InitDataTables
RunGateway
SetApiEndpointKey
_nInvokes // keep this last
)
type Settings struct {
// modules is a map of constructors for DI
//
// In most cases the index will be a reflect. Type of element returned by
// the constructor, but for some 'constructors' it's hard to specify what's
// the return type should be (or the constructor returns fx group)
modules map[interface{}]fx.Option
// invokes are separate from modules as they can't be referenced by return
// type, and must be applied in correct order
invokes []fx.Option
nodeType repo.RepoType
Base bool // Base option applied
Config bool // Config option applied
}
// Basic services
func defaults() []Option {
return []Option{
// global system journal.
Override(new(journal.DisabledEvents), journal.EnvDisabledEvents),
Override(new(*alerting.Alerting), alerting.NewAlertingSystem),
}
}
func Repo(r repo.Repo) Option {
return func(settings *Settings) error {
lr, err := r.Lock(settings.nodeType)
if err != nil {
return err
}
c, err := lr.Config()
if err != nil {
return err
}
return Options(
Override(CheckFDLimit, modules.CheckFdLimit),
Override(new(repo.LockedRepo), modules.LockedRepo(lr)), // module handles closing
Override(new(*jwt.HMACSHA), secret.APISecret),
Override(new(*common.CommonAPI), common.NewCommonAPI),
Override(new(helpers.MetricsCtx), func() context.Context {
return metricsi.CtxScope(context.Background(), "titan")
}),
Override(new(dtypes.PermissionWebToken), modules.GenerateTokenWithWebPermission),
ApplyIf(IsType(repo.Scheduler), ConfigScheduler(c)),
ApplyIf(IsType(repo.Locator), ConfigLocator(c)),
ApplyIf(IsType(repo.Edge), ConfigEdge(c)),
ApplyIf(IsType(repo.Candidate), ConfigCandidate(c)),
)(settings)
}
}
type StopFunc func(context.Context) error
// New builds and starts new Titan node
func New(ctx context.Context, opts ...Option) (StopFunc, error) {
settings := Settings{
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, _nInvokes),
}
// apply module options in the right order
if err := Options(Options(defaults()...), Options(opts...))(&settings); err != nil {
return nil, xerrors.Errorf("applying node options failed: %w", err)
}
// gather constructors for fx.Options
ctors := make([]fx.Option, 0, len(settings.modules))
for _, opt := range settings.modules {
ctors = append(ctors, opt)
}
// fill holes in invokes for use in fx.Options
for i, opt := range settings.invokes {
if opt == nil {
settings.invokes[i] = fx.Options()
}
}
app := fx.New(
fx.Options(ctors...),
fx.Options(settings.invokes...),
fx.NopLogger,
)
// TODO: we probably should have a 'firewall' for Closing signal
// on this context, and implement closing logic through lifecycles
// correctly
if err := app.Start(ctx); err != nil {
// comment fx.NopLogger few lines above for easier debugging
return nil, xerrors.Errorf("starting node: %w", err)
}
return app.Stop, nil
}
func IsType(t repo.RepoType) func(s *Settings) bool {
return func(s *Settings) bool { return s.nodeType == t }
}
func Base() Option {
return Options(
func(s *Settings) error { s.Base = true; return nil }, // mark Base as applied
ApplyIf(func(s *Settings) bool { return s.Config },
Error(errors.New("the Base() option must be set before Config option")),
),
)
}