-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
143 lines (115 loc) · 2.9 KB
/
service.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
package mice
import (
"errors"
"flag"
"fmt"
"os"
"github.com/MouseHatGames/mice/client"
"github.com/MouseHatGames/mice/config"
"github.com/MouseHatGames/mice/logger/stdout"
"github.com/MouseHatGames/mice/options"
"github.com/MouseHatGames/mice/server"
"github.com/MouseHatGames/mice/tracing"
)
// Service represents a service that can receive and send requests
type Service interface {
// Apply applies one or more options to the service's configuration
Apply(opts ...options.Option)
Env() options.Environment
Config() config.Config
Name() string
Server() server.Server
Client() client.Client
Start() error
}
type starter interface {
Start() error
}
type service struct {
options options.Options
server server.Server
client client.Client
}
// NewService instantiates a new service and initializes it with options
func NewService(opts ...options.Option) Service {
svc := &service{}
svc.options.RPCPort = options.DefaultRPCPort
svc.options.Environment = getEnvironment()
svc.options.Tracer = tracing.NoopTracer()
svc.Apply(stdout.Logger())
svc.Apply(opts...)
if svc.options.Name == "" {
panic("no name defined")
}
if svc.options.Codec == nil {
panic("no codec defined")
}
if svc.options.Transport == nil {
panic("no transport defined")
}
svc.server = server.NewServer(&svc.options)
svc.client = client.NewClient(&svc.options)
return svc
}
func getEnvironment() options.Environment {
set := flag.NewFlagSet("cmd", flag.ContinueOnError)
arg := set.String("env", "", "")
set.Parse(os.Args)
if *arg != "" {
return options.ParseEnvironment(*arg)
}
if env, ok := os.LookupEnv("MICE_ENV"); ok {
return options.ParseEnvironment(env)
}
return options.EnvironmentDevelopment
}
func (s *service) Apply(opts ...options.Option) {
for _, o := range opts {
o(&s.options)
}
}
func (s *service) Env() options.Environment {
return s.options.Environment
}
func (s *service) Config() config.Config {
if s.options.Config == nil {
panic("no config provider has been set up")
}
return s.options.Config
}
func (s *service) Name() string {
return s.options.Name
}
func (s *service) Start() error {
if s.options.Name == "" {
return errors.New("missing service name")
}
if err := tryStart(map[string]interface{}{
"broker": s.options.Broker,
"codec": s.options.Codec,
"config": s.options.Config,
"discovery": s.options.Discovery,
"logger": s.options.Logger,
"transport": s.options.Transport,
}); err != nil {
return err
}
s.options.Logger.Infof("starting on %s environment", s.options.Environment)
return s.server.Start()
}
func tryStart(objs map[string]interface{}) error {
for k, v := range objs {
if s, ok := v.(starter); ok {
if err := s.Start(); err != nil {
return fmt.Errorf("start %s: %w", k, err)
}
}
}
return nil
}
func (s *service) Server() server.Server {
return s.server
}
func (s *service) Client() client.Client {
return s.client
}