-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
167 lines (135 loc) · 4.34 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
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
package main
import (
"context"
"errors"
"fmt"
golog "log"
"os"
"strconv"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/auth"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/environment"
messageBusRedis "github.com/OpenSlides/openslides-autoupdate-service/pkg/redis"
"github.com/OpenSlides/openslides-vote-service/backend"
"github.com/OpenSlides/openslides-vote-service/log"
"github.com/OpenSlides/openslides-vote-service/vote"
"github.com/OpenSlides/openslides-vote-service/vote/http"
"github.com/alecthomas/kong"
)
var envDebugLog = environment.NewVariable("VOTE_DEBUG_LOG", "false", "Show debug log.")
//go:generate sh -c "go run main.go build-doc > environment.md"
var cli struct {
Run struct{} `cmd:"" help:"Runs the service." default:"withargs"`
BuildDoc struct{} `cmd:"" help:"Build the environment documentation."`
Health struct {
Host string `help:"Host of the service" short:"h" default:"localhost"`
Port string `help:"Port of the service" short:"p" default:"9013" env:"VOTE_PORT"`
UseHTTPS bool `help:"Use https to connect to the service" short:"s"`
Insecure bool `help:"Accept invalid cert" short:"k"`
} `cmd:"" help:"Runs a health check."`
}
func main() {
ctx, cancel := environment.InterruptContext()
defer cancel()
log.SetInfoLogger(golog.Default())
kongCTX := kong.Parse(&cli, kong.UsageOnError())
switch kongCTX.Command() {
case "run":
if err := contextDone(run(ctx)); err != nil {
handleError(err)
os.Exit(1)
}
case "build-doc":
if err := contextDone(buildDocu()); err != nil {
handleError(err)
os.Exit(1)
}
case "health":
if err := contextDone(http.HealthClient(ctx, cli.Health.UseHTTPS, cli.Health.Host, cli.Health.Port, cli.Health.Insecure)); err != nil {
handleError(err)
os.Exit(1)
}
}
}
func run(ctx context.Context) error {
lookup := new(environment.ForProduction)
if debug, _ := strconv.ParseBool(envDebugLog.Value(lookup)); debug {
log.SetDebugLogger(golog.Default())
}
service, err := initService(lookup)
if err != nil {
return fmt.Errorf("init services: %w", err)
}
return service(ctx)
}
func buildDocu() error {
lookup := new(environment.ForDocu)
if _, err := initService(lookup); err != nil {
return fmt.Errorf("init services: %w", err)
}
doc, err := lookup.BuildDoc()
if err != nil {
return fmt.Errorf("build doc: %w", err)
}
fmt.Println(doc)
return nil
}
// initService initializes all packages needed for the vote service.
//
// Returns a the service as callable.
func initService(lookup environment.Environmenter) (func(context.Context) error, error) {
var backgroundTasks []func(context.Context, func(error))
httpServer := http.New(lookup)
// Redis as message bus for datastore and logout events.
messageBus := messageBusRedis.New(lookup)
// Datastore Service.
database, err := vote.Flow(lookup, messageBus)
if err != nil {
return nil, fmt.Errorf("init database: %w", err)
}
// Auth Service.
authService, authBackground, err := auth.New(lookup, messageBus)
if err != nil {
return nil, fmt.Errorf("init auth system: %w", err)
}
backgroundTasks = append(backgroundTasks, authBackground)
fastBackendStarter, longBackendStarter, singleInstance, err := backend.Build(lookup)
if err != nil {
return nil, fmt.Errorf("init vote backend: %w", err)
}
service := func(ctx context.Context) error {
fastBackend, err := fastBackendStarter(ctx)
if err != nil {
return fmt.Errorf("start fast backend: %w", err)
}
longBackend, err := longBackendStarter(ctx)
if err != nil {
return fmt.Errorf("start long backend: %w", err)
}
voteService, voteBackground, err := vote.New(ctx, fastBackend, longBackend, database, singleInstance)
if err != nil {
return fmt.Errorf("starting service: %w", err)
}
backgroundTasks = append(backgroundTasks, voteBackground)
for _, bg := range backgroundTasks {
go bg(ctx, handleError)
}
return httpServer.Run(ctx, authService, voteService)
}
return service, nil
}
// contextDone returns an empty error if the context is done or exceeded
func contextDone(err error) error {
if err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil
}
return err
}
// handleError handles an error.
//
// Ignores context closed errors.
func handleError(err error) {
if contextDone(err) == nil {
return
}
log.Info("Error: %v", err)
}