-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
73 lines (63 loc) · 1.87 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
package main
import (
"net/http"
"fmt"
"encoding/json"
"github.com/gorilla/mux"
)
type apiHandler func(*Cache, http.ResponseWriter, *http.Request)
func StartHttpApi(cache *Cache, address string) error {
router := initRouter(cache)
err := http.ListenAndServe(address, router)
if err != nil {
return err
}
return nil
}
func makeApiHandler(cache *Cache, handler apiHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handler(cache, w, r)
}
}
func initRouter(cache *Cache) *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", makeApiHandler(cache, handlerRoot))
r.HandleFunc("/hosts", makeApiHandler(cache, handlerHosts))
r.HandleFunc("/containers", makeApiHandler(cache, handlerContainers))
r.HandleFunc("/containers/{name:.*}", makeApiHandler(cache, handlerContainersInfo))
return r
}
func jsonResponse(data interface{}, w http.ResponseWriter) {
b, err := json.Marshal(data)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid data in the cache. Cannot JSON encode: %s", err), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(b)
}
func handlerRoot(cache *Cache, w http.ResponseWriter, r *http.Request) {
data := []string{"/hosts", "/containers"}
jsonResponse(data, w)
}
func handlerHosts(cache *Cache, w http.ResponseWriter, r *http.Request) {
data, err := cache.ListHosts()
if err != nil {
http.Error(w, "Cannot list hosts", http.StatusInternalServerError)
return
}
jsonResponse(data, w)
}
func handlerContainers(cache *Cache, w http.ResponseWriter, r *http.Request) {
data, err := cache.ListContainers("")
if err != nil {
http.Error(w, "Cannot list containers", http.StatusInternalServerError)
return
}
jsonResponse(data, w)
}
func handlerContainersInfo(cache *Cache, w http.ResponseWriter, r *http.Request) {
data := mux.Vars(r)
jsonResponse(data, w)
}