-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
132 lines (116 loc) · 3.33 KB
/
http.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
)
// POST||GET /notify
func HTTPNotifyHandler(w http.ResponseWriter, r *http.Request) {
if r.FormValue("key") != apiKey {
http.Error(w, "Auth failed: incorrect key", 403)
return
}
log.Println("Got HTTP prefetch notification, reloading zones...")
if zoneUrl != "" {
prefetch(zones, false)
}
fmt.Fprintln(w, "ok")
}
// POST /notify/zones
func HTTPNotifyZonesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", 405)
return
}
if r.FormValue("key") != apiKey {
http.Error(w, "Auth failed: incorrect key", 403)
return
}
log.Println("Got HTTP zone push notification, updating cache...")
tmpmap := make(map[string][]Record)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading response body: "+err.Error(), 400)
return
}
if err := json.Unmarshal(body, &tmpmap); err != nil {
http.Error(w, "Error parsing JSON zones file: "+err.Error(), 400)
return
}
zones.apply(tmpmap, false)
dbWriteZones(tmpmap, false)
fmt.Fprintf(w, "Loaded %d zone(s) in cache\n", len(tmpmap))
fmt.Fprintln(w, "ok")
log.Printf("Loaded %d zone(s) in cache\n", len(tmpmap))
}
// GET /hits
func HTTPHitsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
}
if r.FormValue("key") != apiKey {
http.Error(w, "Auth failed: incorrect key", 403)
return
}
zones.RLock()
defer zones.RUnlock()
w.Header().Set("Content-Type", "application/json")
jsonData, _ := json.MarshalIndent(zones.hits, "", ` `)
fmt.Fprintf(w, "%s", jsonData)
}
// GET /zones
func HTTPZonesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
}
if r.FormValue("key") != apiKey {
http.Error(w, "Auth failed: incorrect key", 403)
return
}
tmpmap, err := dbReadZones()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
jsonData, err := json.MarshalIndent(tmpmap, "", ` `)
if err == nil {
fmt.Fprintf(w, "%s", jsonData)
} else {
http.Error(w, err.Error(), 500)
}
}
// GET /metrics
func HTTPMetricsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
}
/* key-checking is currently disabled
if r.FormValue("key") != apiKey {
http.Error(w, "Auth failed: incorrect key", 403)
return
}
*/
promhttp.Handler().ServeHTTP(w, r)
}
func StartHTTP(c *cli.Context) {
handlers := http.NewServeMux()
handlers.HandleFunc("/notify", HTTPNotifyHandler)
handlers.HandleFunc("/notify/zones", HTTPNotifyZonesHandler)
handlers.HandleFunc("/hits", HTTPHitsHandler)
handlers.HandleFunc("/zones", HTTPZonesHandler)
handlers.HandleFunc("/metrics", HTTPMetricsHandler)
if c.Bool("https") {
log.Println("Starting HTTPS notification listener on", c.String("http-listen"))
log.Fatal(http.ListenAndServeTLS(c.String("http-listen"),
c.String("ssl-cert"), c.String("ssl-key"), handlers))
} else {
log.Println("Starting HTTP notification listener on", c.String("http-listen"))
log.Fatal(http.ListenAndServe(c.String("http-listen"), handlers))
}
}