-
Notifications
You must be signed in to change notification settings - Fork 12
/
component.go
69 lines (59 loc) · 1.6 KB
/
component.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
package inx
import (
"context"
"github.com/labstack/echo/v4"
"go.uber.org/dig"
"github.com/iotaledger/hive.go/app"
"github.com/iotaledger/iota-core/components/protocol"
"github.com/iotaledger/iota-core/pkg/daemon"
protocolpkg "github.com/iotaledger/iota-core/pkg/protocol"
"github.com/iotaledger/iota-core/pkg/requesthandler"
restapipkg "github.com/iotaledger/iota-core/pkg/restapi"
)
func init() {
Component = &app.Component{
Name: "INX",
DepsFunc: func(cDeps dependencies) { deps = cDeps },
Params: params,
IsEnabled: func(_ *dig.Container) bool {
return ParamsINX.Enabled
},
Provide: provide,
Run: run,
}
}
var (
Component *app.Component
deps dependencies
)
type dependencies struct {
dig.In
Protocol *protocolpkg.Protocol
RequestHandler *requesthandler.RequestHandler
Echo *echo.Echo `optional:"true"`
RestRouteManager *restapipkg.RestRouteManager
INXServer *Server
BaseToken *protocol.BaseToken
}
func provide(c *dig.Container) error {
//nolint:gocritic // easier to read which type is returned
if err := c.Provide(func() *Server {
return newServer()
}); err != nil {
Component.LogPanic(err.Error())
}
return nil
}
func run() error {
if err := Component.Daemon().BackgroundWorker("INX", func(ctx context.Context) {
Component.LogInfo("Starting INX ... done")
deps.INXServer.Start()
<-ctx.Done()
Component.LogInfo("Stopping INX ...")
deps.INXServer.Stop()
Component.LogInfo("Stopping INX ... done")
}, daemon.PriorityINX); err != nil {
Component.LogPanicf("failed to start worker: %s", err)
}
return nil
}