forked from miku/microblob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
61 lines (56 loc) · 1.82 KB
/
server.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
package microblob
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/segmentio/encoding/json"
"github.com/thoas/stats"
)
// NewHandler sets up routes for serving and stats.
func NewHandler(backend Backend, blobfile string) http.Handler {
metrics := stats.New()
blobHandler := metrics.Handler(
WithLastResponseTime(
&BlobHandler{Backend: backend}))
r := mux.NewRouter()
r.Handle("/debug/vars", http.DefaultServeMux)
r.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(metrics.Data()); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"name": "microblob",
"version": Version,
"stats": fmt.Sprintf("http://%s/stats", r.Host),
"vars": fmt.Sprintf("http://%s/debug/vars", r.Host),
}); err != nil {
http.Error(w, "could not serialize", http.StatusInternalServerError)
return
}
})
r.HandleFunc("/count", func(w http.ResponseWriter, r *http.Request) {
if c, ok := backend.(Counter); ok {
count, err := c.Count()
if err != nil {
http.Error(w, fmt.Sprintf("count failed: %s", err), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(map[string]int64{"count": count}); err != nil {
http.Error(w, "could not serialize", http.StatusInternalServerError)
return
}
} else {
http.Error(w, "not implemented", http.StatusNotFound)
return
}
})
r.Handle("/update", UpdateHandler{Backend: backend, Blobfile: blobfile})
r.Handle("/blob", blobHandler) // Legacy route.
r.Handle("/{key:.+}", blobHandler) // Preferred.
return r
}