-
Notifications
You must be signed in to change notification settings - Fork 3
/
edge.go
163 lines (123 loc) · 3.09 KB
/
edge.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
157
158
159
160
161
162
163
package edge
import (
"strings"
nedge "github.com/micro-community/x-edge/edge"
"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2"
log "github.com/micro/go-micro/v2/logger"
)
//Edge config locate in x-edge/cmd
//Service of edge srv
type Service interface {
Name() string
Init(opts ...Option) error
Run() error
String() string
MService() micro.Service
EService() nedge.Service
}
//edgeApp for edge process
type edgeApp struct {
opts Options
}
//NewService return a edge service application
func NewService(opts ...Option) Service {
options := newOptions(opts...)
s := &edgeApp{
opts: options,
}
return s
}
func (e *edgeApp) buildGoMicroOption() []micro.Option {
serviceOpts := []micro.Option{}
if len(e.opts.Flags) > 0 {
serviceOpts = append(serviceOpts, micro.Flags(e.opts.Flags...))
}
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
if name := ctx.String("server_name"); len(name) > 0 {
e.opts.Name = name
}
if ver := ctx.String("server_version"); len(ver) > 0 {
e.opts.Version = ver
}
if id := ctx.String("server_id"); len(id) > 0 {
e.opts.ID = id
}
if addr := ctx.String("server_address"); len(addr) > 0 {
e.opts.Address = addr
}
if adv := ctx.String("server_advertise"); len(adv) > 0 {
e.opts.Advertise = adv
}
return nil
}))
return serviceOpts
}
func (e *edgeApp) Init(opts ...Option) error {
for _, o := range opts {
o(&e.opts)
}
serviceOpts := e.buildGoMicroOption()
edgeOptions := []nedge.Option{}
if e.opts.EdgeTransport != nil {
//setup transport for edge
edgeOptions = append(edgeOptions, nedge.Transport(e.opts.EdgeTransport))
}
if strings.Trim(e.opts.EdgeHost, " ") != "" {
edgeOptions = append(edgeOptions, nedge.Host(e.opts.EdgeHost))
}
if e.opts.Extractor != nil {
edgeOptions = append(edgeOptions, nedge.WithExtractor(e.opts.Extractor))
} else {
edgeOptions = append(edgeOptions, nedge.WithExtractor(nedge.DefaultExtractor))
}
e.opts.Edge.Init(edgeOptions...)
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
// execute edge service Action
if e.opts.Edge.Options().Action != nil {
e.opts.Edge.Options().Action(ctx)
}
if e.opts.Action != nil {
e.opts.Action(ctx)
}
return nil
}))
e.opts.MicroService.Init(serviceOpts...)
return nil
}
func (e *edgeApp) start() error {
return nil
}
func (e *edgeApp) stop() error {
return nil
}
//Run to launch edge server process
func (e *edgeApp) Run() error {
// init edge itself
log.Init(log.WithFields(map[string]interface{}{"Service": "[Edge]"}))
if err := e.opts.Edge.Start(); err != nil {
log.Fatal(err)
}
// Run go-micro servier
if err := e.opts.MicroService.Run(); err != nil {
log.Fatal(err)
}
if err := e.opts.Edge.Stop(); err != nil {
log.Fatal(err)
}
return nil
}
// return micro.service
func (e *edgeApp) MService() micro.Service {
return e.opts.MicroService
}
// return micro.service
func (e *edgeApp) EService() nedge.Service {
return e.opts.Edge
}
func (e *edgeApp) Name() string {
return e.opts.Name
}
func (e *edgeApp) String() string {
return "edge app"
}