-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
executable file
·84 lines (80 loc) · 1.77 KB
/
entry.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
package server
import (
"context"
"encoding/json"
"github.com/pkg/errors"
"github.com/thavlik/t4vd/base/pkg/base"
"github.com/thavlik/t4vd/base/pkg/iam"
"github.com/thavlik/t4vd/base/pkg/pubsub"
compiler "github.com/thavlik/t4vd/compiler/pkg/api"
filter "github.com/thavlik/t4vd/filter/pkg/api"
"github.com/thavlik/t4vd/gateway/pkg/api"
sources "github.com/thavlik/t4vd/sources/pkg/api"
"go.uber.org/zap"
)
var gatewayTopic = "gateway"
func Entry(
port int,
adminPort int,
iam iam.IAM,
seerOpts base.ServiceOptions,
sources sources.Sources,
compiler compiler.Compiler,
filter filter.Filter,
slideshow base.ServiceOptions,
pubSub pubsub.PubSub,
corsHeader string,
log *zap.Logger,
) error {
s := NewServer(
iam,
seerOpts,
sources,
compiler,
filter,
slideshow,
pubsub.Publisher(pubSub),
corsHeader,
log,
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sub, err := pubSub.Subscribe(ctx, gatewayTopic)
if err != nil {
return errors.Wrap(err, "pubsub.Subscribe")
}
go func() {
defer panic("gateway subscriber returned")
ch := sub.Messages(ctx)
defer sub.Cancel(ctx)
done := ctx.Done()
for {
select {
case <-done:
return
case msg, ok := <-ch:
if !ok {
return
}
var event api.Event
if err := json.Unmarshal(msg, &event); err != nil {
panic(errors.Wrap(err, "json.Unmarshal"))
}
if err := s.pushEventLocal(event); err != nil {
panic(errors.Wrap(err, "pushEventLocal"))
}
}
}
}()
mainErr := make(chan error, 1)
go func() { mainErr <- s.ListenAndServe(port) }()
adminErr := make(chan error, 1)
go func() { adminErr <- s.AdminListenAndServe(adminPort) }()
base.SignalReady(log)
select {
case err := <-mainErr:
return err
case err := <-adminErr:
return err
}
}