-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
89 lines (70 loc) · 1.72 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
package main
import (
"flag"
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/hudymi/mockice/pkg/endpoint"
"github.com/hudymi/mockice/pkg/log"
"github.com/hudymi/mockice/pkg/service"
"github.com/hudymi/mockice/pkg/signal"
)
type options struct {
verbose bool
config string
}
type config struct {
Address string `yaml:"address"`
EndpointsConfiguration []endpoint.Config `yaml:"endpoints"`
}
func gatherOptions() options {
o := options{}
flag.StringVar(&o.config, "config", "", "The path to the configuration file")
flag.BoolVar(&o.verbose, "verbose", false, "Enable verbose output")
flag.Parse()
return o
}
func main() {
options := gatherOptions()
log.Setup(options.verbose)
ctx := signal.Context()
cfg, err := loadConfig(options.config)
if err != nil {
logrus.Fatal(err)
}
svc := service.New(cfg.Address)
for _, config := range cfg.EndpointsConfiguration {
endpoint := endpoint.New(config)
logrus.Infof("Registering /%s endpoint", endpoint.Name())
err := svc.Register(endpoint)
if err != nil {
logrus.Fatal(err)
}
}
logrus.Infof("Service listen at %s", cfg.Address)
err = svc.Start(ctx)
if err != nil {
logrus.Error(err)
os.Exit(1)
}
}
func loadConfig(path string) (config, error) {
cfg := config{
Address: ":8080",
}
if path == "" {
cfg.EndpointsConfiguration = endpoint.DefaultConfig()
return cfg, nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return config{}, errors.Wrapf(err, "while reading configuration from %s", path)
}
err = yaml.Unmarshal(content, &cfg)
if err != nil {
return config{}, errors.Wrapf(err, "while loading configuration from %s", path)
}
return cfg, nil
}