-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (151 loc) · 3.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
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
168
169
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/ww24/pubsub-gateway/gateway"
"github.com/ww24/pubsub-gateway/internal/config"
"github.com/ww24/pubsub-gateway/receiver"
)
const (
defaultPort = "8080"
defaultMode = "gateway"
shutdownTimeout = 30 * time.Second
)
var (
port = os.Getenv("PORT")
mode = os.Getenv("MODE")
confYaml = os.Getenv("CONFIG_YAML")
confFile = flag.String("config", "", "set path to config (for receiver mode)")
)
func main() {
flag.Parse()
if port == "" {
port = defaultPort
}
if mode == "" {
mode = defaultMode
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
defer stop()
var closeFn func(context.Context)
if mode == "" {
mode = "gateway"
}
switch mode {
case "gateway":
closeFn = gatewayMode(ctx, port)
case "receiver":
closeFn = receiverMode(ctx)
}
// wait signal
<-ctx.Done()
stop()
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
closeFn(ctx)
}
func gatewayMode(ctx context.Context, port string) func(context.Context) {
ctx, cancel := context.WithCancel(ctx)
log.Println("Listen at", port)
srv := &http.Server{
Addr: ":" + port,
Handler: gateway.New(ctx),
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println("Server Error:", err)
}
}()
return func(ctx context.Context) {
cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Println("Shutdown Error:", err)
}
}
}
func receiverMode(ctx context.Context) func(context.Context) {
var confData []byte
if *confFile != "" {
f, err := os.Open(*confFile)
if err != nil {
log.Fatalln("failed to open config file:", err)
}
defer f.Close()
confData, err = io.ReadAll(f)
if err != nil {
log.Fatalln("failed to load config file:", err)
}
} else {
confData = []byte(confYaml)
}
if confData == nil {
fmt.Fprintln(os.Stderr, "either -config flag or CONFIG_YAML env is required")
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
ctx, cancel := context.WithCancel(ctx)
cfg, err := config.Parse(confData)
if err != nil {
cancel()
log.Fatalln("failed to load config:", err)
return func(context.Context) {}
}
s, err := receiver.New(ctx)
if err != nil {
cancel()
log.Fatalln("failed initialize receiver:", err)
return func(context.Context) {}
}
for _, handler := range cfg.Handlers {
fmt.Println("subscription:", handler.Subscription)
handler := handler
go func() {
err := s.Subscribe(ctx, handler.Subscription, func(ctx context.Context, data []byte) bool {
log.Println("received:", string(data))
m := make(map[string]interface{})
if err := json.Unmarshal(data, &m); err != nil {
log.Println("failed to unmarshal json:", err)
return true
}
var action receiver.Executable
var payload []byte
switch handler.Action.Type {
case config.ActionHTTP:
a := handler.Action.HTTPRequestAction
action = receiver.NewHTTPAction(a.Header, a.Method, a.URL)
var err error
payload, err = a.Payload.RenderJSON(data)
if err != nil {
log.Println("failed to render:", err)
return false
}
default:
log.Println("unexpected action type:", handler.Action.Type)
return false
}
log.Println("payload:", string(payload))
if err := action.Exec(ctx, payload); err != nil {
log.Println("failed to exec action", err)
return false
}
return true
})
if err != nil {
panic(err)
}
}()
}
return func(context.Context) {
cancel()
}
}