-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathhttp.go
149 lines (121 loc) · 2.88 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"time"
"github.com/abh/geodns/v3/appconfig"
"github.com/abh/geodns/v3/monitor"
"github.com/abh/geodns/v3/zones"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
)
type httpServer struct {
mux *http.ServeMux
zones *zones.MuxManager
serverInfo *monitor.ServerInfo
}
type rate struct {
Name string
Count int64
Metrics zones.ZoneMetrics
}
type rates []*rate
func (s rates) Len() int { return len(s) }
func (s rates) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type ratesByCount struct{ rates }
func (s ratesByCount) Less(i, j int) bool {
ic := s.rates[i].Count
jc := s.rates[j].Count
if ic == jc {
return s.rates[i].Name < s.rates[j].Name
}
return ic > jc
}
func topParam(req *http.Request, def int) int {
req.ParseForm()
topOption := def
topParam := req.Form["top"]
if len(topParam) > 0 {
var err error
topOption, err = strconv.Atoi(topParam[0])
if err != nil {
topOption = def
}
}
return topOption
}
func NewHTTPServer(mm *zones.MuxManager, serverInfo *monitor.ServerInfo) *httpServer {
hs := &httpServer{
zones: mm,
mux: &http.ServeMux{},
serverInfo: serverInfo,
}
hs.mux.HandleFunc("/", hs.mainServer)
hs.mux.Handle("/metrics", promhttp.Handler())
return hs
}
func (hs *httpServer) Mux() *http.ServeMux {
return hs.mux
}
func (hs *httpServer) Run(ctx context.Context, listen string) error {
log.Println("Starting HTTP interface on", listen)
srv := http.Server{
Addr: listen,
Handler: &basicauth{h: hs.mux},
ReadTimeout: 5 * time.Second,
IdleTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
err := srv.ListenAndServe()
if err != nil {
if !errors.Is(err, http.ErrServerClosed) {
return err
}
}
return nil
})
g.Go(func() error {
<-ctx.Done()
log.Printf("shutting down http server")
return srv.Shutdown(ctx)
})
return g.Wait()
}
func (hs *httpServer) mainServer(w http.ResponseWriter, req *http.Request) {
if req.RequestURI != "/version" {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(200)
io.WriteString(w, `GeoDNS `+hs.serverInfo.Version+`\n`)
}
type basicauth struct {
h http.Handler
}
func (b *basicauth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// cfgMutex.RLock()
user := appconfig.Config.HTTP.User
password := appconfig.Config.HTTP.Password
// cfgMutex.RUnlock()
if len(user) == 0 {
b.h.ServeHTTP(w, r)
return
}
ruser, rpass, ok := r.BasicAuth()
if ok {
if ruser == user && rpass == password {
b.h.ServeHTTP(w, r)
return
}
}
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "GeoDNS Status"))
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}