-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.go
More file actions
58 lines (50 loc) · 1.42 KB
/
http.go
File metadata and controls
58 lines (50 loc) · 1.42 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
package main
import (
"context"
"encoding/json"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func serve() {
http.HandleFunc("/all", httpAllHandler)
http.HandleFunc("/all/details", httpAllDetailsHandler)
http.HandleFunc("/metric/", httpMetricHandler)
http.HandleFunc("/reset", httpResetHandler)
srv := &http.Server{
Addr: ":8080",
WriteTimeout: time.Second * 10,
ReadTimeout: time.Second * 10,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Info("Stopped serving", "err", err)
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
srv.Shutdown(ctx)
log.Info("Shutting down")
}
func httpAllHandler(w http.ResponseWriter, r *http.Request) {
log.Info("handling http request", "path", r.URL.Path)
json.NewEncoder(w).Encode(summary.getAllCount())
}
func httpAllDetailsHandler(w http.ResponseWriter, r *http.Request) {
log.Info("handling http request", "path", r.URL.Path)
json.NewEncoder(w).Encode(summary.getAllDetails())
}
func httpMetricHandler(w http.ResponseWriter, r *http.Request) {
stat := r.URL.Path[8:]
log.Info("handling http request", "path", r.URL.Path)
json.NewEncoder(w).Encode(summary.get(stat))
}
func httpResetHandler(w http.ResponseWriter, r *http.Request) {
log.Info("handling http request", "path", r.URL.Path)
summary.reset()
}