forked from datawire/aes-example-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx-dc-plugin.go
More file actions
71 lines (58 loc) · 1.32 KB
/
x-dc-plugin.go
File metadata and controls
71 lines (58 loc) · 1.32 KB
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
package main
import (
"log"
"net/http"
"net/url"
"github.com/hashicorp/consul/api"
)
func PluginMain(w http.ResponseWriter, r *http.Request) {
// Get userID from URL param
u, err := r.URL.Parse(r.URL.String())
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusOK)
return
}
m, err := url.ParseQuery(u.RawQuery)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusOK)
return
}
uID := m["userID"]
if len(uID) <= 0 {
log.Println("userID not provided")
w.WriteHeader(http.StatusOK)
return
}
log.Println("userID: ", uID[0])
//Connect to consul api for key-value lookup
log.Printf("Connecting to consul api\n")
config := api.DefaultConfig()
config.Address = "consul-server:8500"
consul, err := api.NewClient(config)
if err != nil {
w.WriteHeader(http.StatusOK)
return
}
// Query for userID
query := api.QueryOptions{}
KVPair, qm, err := consul.KV().Get(uID[0], &query)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusOK)
return
}
if qm != nil {
log.Println(qm)
}
if KVPair == nil {
log.Printf("userID: %s not found.\n", uID[0])
w.WriteHeader(http.StatusOK)
return
}
// Write the DC value to the X-DC header
log.Printf("Directing userID %s to DC %s\n", uID[0], KVPair.Value)
w.Header().Set("X-DC", string(KVPair.Value))
w.WriteHeader(http.StatusOK)
}