/
server.go
225 lines (191 loc) · 5.61 KB
/
server.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package server
import (
"fmt"
"os"
"path/filepath"
"github.com/itzmanish/go-micro/v2"
"github.com/itzmanish/go-micro/v2/config/cmd"
log "github.com/itzmanish/go-micro/v2/logger"
gorun "github.com/itzmanish/go-micro/v2/runtime"
handler "github.com/itzmanish/go-micro/v2/util/file"
"github.com/itzmanish/micro/v2/internal/platform"
"github.com/itzmanish/micro/v2/internal/update"
"github.com/micro/cli/v2"
)
var (
// list of services managed
services = []string{
// runtime services
"config", // ????
"auth", // :8010
"network", // :8085
"runtime", // :8088
"registry", // :8000
"broker", // :8001
"store", // :8002
"router", // :8084
"debug", // :????
"proxy", // :8081
"api", // :8080
"web", // :8082
"bot", // :????
"init", // no port, manage self
}
)
var (
// Name of the server microservice
Name = "go.micro.server"
// Address is the router microservice bind address
Address = ":10001"
)
func Commands(options ...micro.Option) []*cli.Command {
command := &cli.Command{
Name: "server",
Usage: "Run the micro server",
Description: `Launching the micro server ('micro server') will enable one to connect to it by
setting the appropriate Micro environment (see 'micro env' && 'micro env --help') commands.`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Usage: "Set the micro server address :10001",
EnvVars: []string{"MICRO_SERVER_ADDRESS"},
},
&cli.BoolFlag{
Name: "peer",
Usage: "Peer with the global network to share services",
},
&cli.StringFlag{
Name: "profile",
Usage: "Set the runtime profile to use for services e.g local, kubernetes, platform",
EnvVars: []string{"MICRO_RUNTIME_PROFILE"},
},
},
Action: func(ctx *cli.Context) error {
Run(ctx)
return nil
},
}
for _, p := range Plugins() {
if cmds := p.Commands(); len(cmds) > 0 {
command.Subcommands = append(command.Subcommands, cmds...)
}
if flags := p.Flags(); len(flags) > 0 {
command.Flags = append(command.Flags, flags...)
}
}
return []*cli.Command{command}
}
// Run runs the entire platform
func Run(context *cli.Context) error {
log.Init(log.WithFields(map[string]interface{}{"service": "micro"}))
if context.Args().Len() > 0 {
cli.ShowSubcommandHelp(context)
os.Exit(1)
}
// get the network flag
peer := context.Bool("peer")
// pass through the environment
// By default we want a file store when we run micro server.
// This will get overridden if user has set their own MICRO_STORE env var or passed in --store
env := []string{"MICRO_STORE=file"}
profile := context.String("profile")
if len(profile) == 0 {
profile = "server"
}
env = append(env, "MICRO_RUNTIME_PROFILE="+profile)
env = append(env, os.Environ()...)
// connect to the network if specified
if peer {
log.Info("Setting global network")
if v := os.Getenv("MICRO_NETWORK_NODES"); len(v) == 0 {
// set the resolver to use https://micro.mu/network
env = append(env, "MICRO_NETWORK_NODES=network.micro.mu")
log.Info("Setting default network micro.mu")
}
if v := os.Getenv("MICRO_NETWORK_TOKEN"); len(v) == 0 {
// set the network token
env = append(env, "MICRO_NETWORK_TOKEN=micro.mu")
log.Info("Setting default network token")
}
}
log.Info("Loading core services")
// create new micro runtime
muRuntime := cmd.DefaultCmd.Options().Runtime
// Use default update notifier
if context.Bool("auto_update") {
updateURL := context.String("update_url")
if len(updateURL) == 0 {
updateURL = update.DefaultURL
}
options := []gorun.Option{
gorun.WithScheduler(update.NewScheduler(updateURL, platform.Version)),
}
(*muRuntime).Init(options...)
}
for _, service := range services {
name := service
if namespace := context.String("namespace"); len(namespace) > 0 {
name = fmt.Sprintf("%s.%s", namespace, service)
}
log.Infof("Registering %s", name)
// @todo this is a hack
envs := env
switch service {
case "proxy", "web", "api":
envs = append(envs, "MICRO_AUTH=service")
}
cmdArgs := []string{}
// we want to pass through the global args so go up one level in the context lineage
if len(context.Lineage()) > 1 {
globCtx := context.Lineage()[1]
for _, f := range globCtx.FlagNames() {
cmdArgs = append(cmdArgs, "--"+f, context.String(f))
}
}
cmdArgs = append(cmdArgs, service)
// runtime based on environment we run the service in
args := []gorun.CreateOption{
gorun.WithCommand(os.Args[0]),
gorun.WithArgs(cmdArgs...),
gorun.WithEnv(envs),
gorun.WithOutput(os.Stdout),
gorun.WithRetries(10),
}
// NOTE: we use Version right now to check for the latest release
muService := &gorun.Service{Name: name, Version: platform.Version}
if err := (*muRuntime).Create(muService, args...); err != nil {
log.Errorf("Failed to create runtime enviroment: %v", err)
return err
}
}
log.Info("Starting service runtime")
// start the runtime
if err := (*muRuntime).Start(); err != nil {
log.Fatal(err)
return err
}
log.Info("Service runtime started")
// TODO: should we launch the console?
// start the console
// cli.Init(context)
server := micro.NewService(
micro.Name(Name),
micro.Address(Address),
)
// @todo make this configurable
uploadDir := filepath.Join(os.TempDir(), "micro", "uploads")
os.MkdirAll(uploadDir, 0777)
handler.RegisterHandler(server.Server(), uploadDir)
// start the server
server.Run()
log.Info("Stopping service runtime")
// stop all the things
if err := (*muRuntime).Stop(); err != nil {
log.Fatal(err)
return err
}
log.Info("Service runtime shutdown")
// exit success
os.Exit(0)
return nil
}