This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
120 lines (107 loc) · 2.62 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
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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
ApplicationName = "flaggio"
ApplicationDescription = "Self hosted feature flag solution"
ApplicationVersion = "0.1.0"
GitSummary = ""
GitBranch = ""
BuildStamp = ""
)
const (
logFormatterText = "text"
logFormatterJSON = "json"
)
func build() string {
return fmt.Sprintf("%s[%s] (%s)", GitBranch, GitSummary, BuildStamp)
}
func main() { // nolint:gocyclo // dependencies
app := cli.App{
Name: ApplicationName,
Description: ApplicationDescription,
Version: ApplicationVersion,
Flags: flags,
Action: func(_ *cli.Context) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger := logrus.New()
logLevel, err := logrus.ParseLevel(cfg.logLevel)
if err != nil {
return err
}
logger.SetLevel(logLevel)
switch cfg.logFormatter {
case logFormatterText:
logger.SetFormatter(new(logrus.TextFormatter))
case logFormatterJSON:
logger.SetFormatter(new(logrus.JSONFormatter))
default:
return fmt.Errorf("invalid formatter: %s", cfg.logFormatter)
}
logger.
WithFields(logrus.Fields{"version": ApplicationVersion, "build": build()}).
Infof("starting %s application", ApplicationName)
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// setup tracer
if cfg.isTracingEnabled() {
tracer, closer, err := newTracer(cfg.jaegerAgentHost, logger.WithField("app", "tracer"))
if err != nil {
return err
}
defer closer.Close()
opentracing.SetGlobalTracer(tracer)
}
errs := make(chan error, 1)
var wg sync.WaitGroup
if !cfg.noAPI {
// start API server
go func() {
err := startAPI(ctx, &wg, logger.WithField("app", "api"))
if err != nil {
errs <- err
}
}()
}
if !cfg.noAdmin {
// start Admin server
go func() {
err := startAdmin(ctx, &wg, logger.WithField("app", "admin"))
if err != nil {
errs <- err
}
}()
}
for {
select {
case err := <-errs:
cancel()
wg.Wait()
return err
case <-done:
logger.Debug("got os.Interrupt, cancelling main context")
cancel()
case <-ctx.Done():
logger.Trace("context done")
wg.Wait()
logger.Info("shutdown completed")
return ctx.Err()
}
}
},
}
if err := app.Run(os.Args); err != nil && !errors.Is(err, context.Canceled) {
logrus.Fatal(err)
}
}