-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathmain.go
124 lines (105 loc) · 4.46 KB
/
main.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
/*******************************************************************************
* Copyright 2020 Dell Inc.
* Copyright 2022-2023 IOTech Ltd.
* Copyright 2023 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package command
import (
"context"
"sync"
"time"
"github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap/flags"
"github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap/handlers"
"github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap/interfaces"
"github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap/startup"
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/v4/config"
"github.com/edgexfoundry/go-mod-bootstrap/v4/di"
"github.com/edgexfoundry/edgex-go"
"github.com/edgexfoundry/edgex-go/internal/core/command/config"
"github.com/edgexfoundry/edgex-go/internal/core/command/container"
"github.com/edgexfoundry/edgex-go/internal/core/command/controller/messaging"
"github.com/edgexfoundry/go-mod-core-contracts/v4/common"
"github.com/labstack/echo/v4"
)
func Main(ctx context.Context, cancel context.CancelFunc, router *echo.Echo, args []string) {
startupTimer := startup.NewStartUpTimer(common.CoreCommandServiceKey)
// All common command-line flags have been moved to DefaultCommonFlags. Service specific flags can be added here,
// by inserting service specific flag prior to call to commonFlags.Parse().
// Example:
// flags.FlagSet.StringVar(&myvar, "m", "", "Specify a ....")
// ....
// flags.Parse(args)
//
f := flags.New()
f.Parse(args)
configuration := &config.ConfigurationStruct{}
dic := di.NewContainer(di.ServiceConstructorMap{
container.ConfigurationName: func(get di.Get) interface{} {
return configuration
},
})
httpServer := handlers.NewHttpServer(router, true, common.CoreCommandServiceKey)
bootstrap.Run(
ctx,
cancel,
f,
common.CoreCommandServiceKey,
common.ConfigStemCore,
configuration,
startupTimer,
dic,
true,
bootstrapConfig.ServiceTypeOther,
[]interfaces.BootstrapHandler{
handlers.NewClientsBootstrap().BootstrapHandler,
MessagingBootstrapHandler,
handlers.NewServiceMetrics(common.CoreCommandServiceKey).BootstrapHandler, // Must be after Messaging
NewBootstrap(router, common.CoreCommandServiceKey).BootstrapHandler,
httpServer.BootstrapHandler,
handlers.NewStartMessage(common.CoreCommandServiceKey, edgex.Version).BootstrapHandler,
})
// code here!
}
// MessagingBootstrapHandler sets up the MessageBus and External MQTT connections as well as subscriptions
func MessagingBootstrapHandler(ctx context.Context, wg *sync.WaitGroup, startupTimer startup.Timer, dic *di.Container) bool {
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
configuration := container.ConfigurationFrom(dic.Get)
if len(configuration.Service.RequestTimeout) == 0 {
lc.Error("Service.RequestTimeout found empty in service's configuration, missing common config? Use -cp or -cc flags for common config")
return false
}
requestTimeout, err := time.ParseDuration(configuration.Service.RequestTimeout)
if err != nil {
lc.Errorf("Failed to parse Service.RequestTimeout configuration value: %v", err)
return false
}
if configuration.ExternalMQTT.Enabled {
if !handlers.NewExternalMQTT(messaging.OnConnectHandler(requestTimeout, dic)).BootstrapHandler(ctx, wg, startupTimer, dic) {
return false
}
}
if !handlers.MessagingBootstrapHandler(ctx, wg, startupTimer, dic) {
return false
}
if err := messaging.SubscribeCommandRequests(ctx, requestTimeout, dic); err != nil {
lc.Errorf("Failed to subscribe commands request from internal message bus, %v", err)
return false
}
if err := messaging.SubscribeCommandQueryRequests(ctx, dic); err != nil {
lc.Errorf("Failed to subscribe command query request from internal message bus, %v", err)
return false
}
return true
}