forked from micro/micro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
executable file
·88 lines (72 loc) · 1.99 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
package server
import (
"os"
pb "github.com/2637309949/micro/v3/proto/runtime"
"github.com/2637309949/micro/v3/service"
log "github.com/2637309949/micro/v3/service/logger"
"github.com/2637309949/micro/v3/service/runtime"
"github.com/2637309949/micro/v3/service/runtime/handler"
"github.com/2637309949/micro/v3/service/runtime/manager"
"github.com/urfave/cli/v2"
)
var (
// name of the runtime
name = "runtime"
// address of the runtime
address = ":8088"
// Flags specific to the runtime service
Flags = []cli.Flag{
&cli.StringFlag{
Name: "source",
Usage: "Set the runtime source, e.g. micro/services",
EnvVars: []string{"MICRO_RUNTIME_SOURCE"},
},
&cli.IntFlag{
Name: "retries",
Usage: "Set the max retries per service",
EnvVars: []string{"MICRO_RUNTIME_RETRIES"},
},
}
)
// Run the runtime service
func Run(ctx *cli.Context) error {
if len(ctx.String("address")) > 0 {
address = ctx.String("address")
}
if len(ctx.String("server_name")) > 0 {
name = ctx.String("server_name")
}
var srvOpts []service.Option
if len(address) > 0 {
srvOpts = append(srvOpts, service.Address(address))
}
// create runtime
if ctx.IsSet("source") {
runtime.DefaultRuntime.Init(runtime.WithSource(ctx.String("source")))
}
// append name
srvOpts = append(srvOpts, service.Name(name))
// new service
srv := service.New(srvOpts...)
// create a new runtime manager
manager := manager.New()
// start the manager
if err := manager.Start(); err != nil {
log.Errorf("failed to start: %s", err)
os.Exit(1)
}
// register the handlers
pb.RegisterRuntimeHandler(srv.Server(), &handler.Runtime{Runtime: manager})
pb.RegisterBuildHandler(srv.Server(), new(handler.Build))
pb.RegisterSourceHandler(srv.Server(), new(handler.Source))
// start runtime service
if err := srv.Run(); err != nil {
log.Errorf("error running service: %v", err)
}
// stop the manager
if err := manager.Stop(); err != nil {
log.Errorf("failed to stop: %s", err)
os.Exit(1)
}
return nil
}