forked from launchdarkly/ld-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ld-relay.go
226 lines (178 loc) · 5.12 KB
/
ld-relay.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/launchdarkly/eventsource"
"github.com/launchdarkly/gcfg"
ld "github.com/launchdarkly/go-client"
ldr "github.com/launchdarkly/go-client/redis"
"github.com/streamrail/concurrent-map"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
)
var (
Debug *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
)
var VERSION = "DEV"
var uuidHeaderPattern = regexp.MustCompile(`^(?:api_key )?((?:[a-z]{3}-)?[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12})$`)
type EnvConfig struct {
ApiKey string
Prefix string
}
type Config struct {
Main struct {
ExitOnError bool
StreamUri string
BaseUri string
Port int
HeartbeatIntervalSecs int
}
Redis struct {
Host string
Port int
}
Environment map[string]*EnvConfig
}
type StatusEntry struct {
Status string `json:"status"`
}
var configFile string
func main() {
flag.StringVar(&configFile, "config", "/etc/ld-relay.conf", "configuration file location")
flag.Parse()
initLogging(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
var c Config
Info.Printf("Starting LaunchDarkly relay version %s with configuration file %s\n", formatVersion(VERSION), configFile)
err := gcfg.ReadFileInto(&c, configFile)
if err != nil {
Error.Println("Failed to read configuration file. Exiting.")
os.Exit(1)
}
publisher := eventsource.NewServer()
publisher.Gzip = true
publisher.AllowCORS = true
publisher.ReplayAll = true
handlers := cmap.New()
clients := cmap.New()
for envName, _ := range c.Environment {
clients.Set(envName, nil)
}
for envName, envConfig := range c.Environment {
go func(envName string, envConfig EnvConfig) {
var baseFeatureStore ld.FeatureStore
if c.Redis.Host != "" && c.Redis.Port != 0 {
baseFeatureStore = ldr.NewRedisFeatureStore(c.Redis.Host, c.Redis.Port, envConfig.Prefix, 0)
} else {
baseFeatureStore = ld.NewInMemoryFeatureStore()
}
clientConfig := ld.DefaultConfig
clientConfig.Stream = true
clientConfig.FeatureStore = NewSSERelayFeatureStore(envConfig.ApiKey, publisher, baseFeatureStore, c.Main.HeartbeatIntervalSecs)
clientConfig.StreamUri = c.Main.StreamUri
clientConfig.BaseUri = c.Main.BaseUri
client, err := ld.MakeCustomClient(envConfig.ApiKey, clientConfig, time.Second*10)
clients.Set(envName, client)
if err != nil {
Error.Printf("Error initializing LaunchDarkly client for %s: %+v\n", envName, err)
if c.Main.ExitOnError {
os.Exit(1)
}
} else {
Info.Printf("Initialized LaunchDarkly client for %s\n", envName)
// create a handler from the publisher for this environment
handler := publisher.Handler(envConfig.ApiKey)
handlers.Set(envConfig.ApiKey, handler)
}
}(envName, *envConfig)
}
http.Handle("/status", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
envs := make(map[string]StatusEntry)
healthy := true
for item := range clients.IterBuffered() {
if item.Val == nil {
envs[item.Key] = StatusEntry{Status: "disconnected"}
healthy = false
} else {
client := item.Val.(*ld.LDClient)
if client.Initialized() {
envs[item.Key] = StatusEntry{Status: "connected"}
} else {
envs[item.Key] = StatusEntry{Status: "disconnected"}
healthy = false
}
}
}
resp := make(map[string]interface{})
resp["environments"] = envs
if healthy {
resp["status"] = "healthy"
} else {
resp["status"] = "degraded"
}
data, _ := json.Marshal(resp)
w.Write(data)
}))
// Now make a single handler that dispatches to the appropriate handler based on the Authorization header
http.Handle("/features", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
apiKey, err := fetchAuthToken(req)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
if h, ok := handlers.Get(apiKey); ok {
handler := h.(http.Handler)
handler.ServeHTTP(w, req)
} else {
w.WriteHeader(http.StatusNotFound)
return
}
}))
Info.Printf("Listening on port %d\n", c.Main.Port)
http.ListenAndServe(fmt.Sprintf(":%d", c.Main.Port), nil)
}
func fetchAuthToken(req *http.Request) (string, error) {
authHdr := req.Header.Get("Authorization")
match := uuidHeaderPattern.FindStringSubmatch(authHdr)
// successfully matched UUID from header
if len(match) == 2 {
return match[1], nil
}
return "", errors.New("No valid token found")
}
func formatVersion(version string) string {
split := strings.Split(version, "+")
if len(split) == 2 {
return fmt.Sprintf("%s (build %s)", split[0], split[1])
}
return version
}
func initLogging(
debugHandle io.Writer,
infoHandle io.Writer,
warningHandle io.Writer,
errorHandle io.Writer) {
Debug = log.New(debugHandle,
"DEBUG: ",
log.Ldate|log.Ltime|log.Lshortfile)
Info = log.New(infoHandle,
"INFO: ",
log.Ldate|log.Ltime|log.Lshortfile)
Warning = log.New(warningHandle,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile)
Error = log.New(errorHandle,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)
}