-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (164 loc) · 4.55 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/json"
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/tylerb/graceful.v1"
"log"
"net/http"
"net/url"
"os"
"runtime"
"syscall"
"time"
)
var (
httpPort = new(string)
encryptKey = new(string)
pubSubCredsJSON = new(string)
debug = flag.Bool("debug", false, "Debug mode")
encryptBlock cipher.Block
jobChan chan TopicMessage
metrics = struct {
FulfillmentRequest prometheus.Summary
FulfillmentRequestFailures prometheus.Counter
PubSubMessages *prometheus.CounterVec
PubSubFailures *prometheus.CounterVec
}{
prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "web",
Subsystem: "webhooks",
Name: "open_request",
Help: "Request latency for opens.",
}),
prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "web",
Subsystem: "webhooks",
Name: "open_request_failures",
Help: "How many open requests have failed.",
}),
prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "web",
Subsystem: "webhooks",
Name: "pubsub_messages",
Help: "Messages placed in the topic via a publish.",
}, []string{"topic"}),
prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "web",
Subsystem: "webhooks",
Name: "pubsub_failures",
Help: "Failures when attempting to publish messages.",
}, []string{"topic"}),
}
)
func parseEnv(target *string, name, defaultValue string) {
if v := os.Getenv(name); v != "" {
*target = v
} else {
*target = defaultValue
}
}
func init() {
parseEnv(httpPort, "HTTP_PORT", "8080")
flag.StringVar(httpPort, "httpPort", *httpPort, "Listen Address")
parseEnv(encryptKey, "KEY", "")
flag.StringVar(encryptKey, "key", *encryptKey, "AES Encryption key")
parseEnv(pubSubCredsJSON, "PUBSUB_CREDS", "{}")
flag.StringVar(pubSubCredsJSON, "pubSubCreds", *pubSubCredsJSON, "Json of pubsub specifiers of the form: {ProjectID, TopicName}")
flag.Parse()
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
log.SetPrefix(fmt.Sprintf("pid:%d ", syscall.Getpid()))
prometheus.MustRegister(
metrics.FulfillmentRequest,
metrics.FulfillmentRequestFailures,
metrics.PubSubMessages,
metrics.PubSubFailures,
)
}
func main() {
var err error
encryptBlock, err = aes.NewCipher([]byte(*encryptKey))
if err != nil {
log.Fatal("AES block failed ", err)
}
var spec *PubSubSpec
err = json.Unmarshal([]byte(*pubSubCredsJSON), &spec)
if err != nil {
log.Fatal("Invalid pubsub creds json: ", err)
}
jobChan = startPubSubChannel(spec)
serveHTTP()
}
func serveHTTP() {
mux := http.NewServeMux()
// Handle anything /fulfillment/*
mux.HandleFunc("/fulfillment/", handleFulfillment)
mux.Handle("/metrics", promhttp.Handler())
if *debug {
mux.HandleFunc("/getKey", handleGetKey)
}
log.Println("serving on", *httpPort)
graceful.Run(fmt.Sprintf(":%s", *httpPort), 15*time.Second, mux)
}
func handleRecover(w http.ResponseWriter, r *http.Request) {
err := recover()
if nil != err {
w.WriteHeader(http.StatusInternalServerError)
stack := make([]byte, 1<<16)
runtime.Stack(stack, false)
log.Printf("HTTP error caught %s\n%s", err, stack)
}
}
func handleFulfillment(w http.ResponseWriter, r *http.Request) {
var err error
defer handleRecover(w, r)
defer (func(start time.Time) {
metrics.FulfillmentRequest.Observe(time.Since(start).Seconds())
if err != nil {
metrics.FulfillmentRequestFailures.Inc()
}
})(time.Now())
query := r.URL.Query()
payload, err := getPayloadFromQuery(query, Fulfillment)
if nil != err {
w.WriteHeader(http.StatusNotAcceptable)
return
}
payload.RemoteAddr = getIPFromRequest(r)
payload.UserAgent = r.UserAgent()
open := NewRequest(payload, Fulfillment)
msg, err := json.Marshal(open)
if nil != err {
log.Printf("error json.Marshal for open: %s, %s\n", err, open)
w.WriteHeader(http.StatusInternalServerError)
return
}
jobChan <- TopicMessage{
Msg: string(msg),
}
w.WriteHeader(http.StatusOK)
if *debug {
fmt.Printf("Fulfillment: %q\n", payload)
}
}
func handleGetKey(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
var (
link = ""
)
if val, ok := query["url"]; ok {
link = val[0]
}
payload, err := generateRequestPayload(link)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "failed to encrypt! %v", err)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s\nencrypts to\n%v \n", link, url.QueryEscape(payload))
}