|
1
|
|
|
package httpserver |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"bytes" |
|
5
|
|
|
"github.com/arazmj/gerdu/cache" |
|
6
|
|
|
"github.com/gorilla/mux" |
|
7
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp" |
|
8
|
|
|
log "github.com/sirupsen/logrus" |
|
9
|
|
|
"net/http" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
func newRouter(gerdu cache.UnImplementedCache) (router *mux.Router) { |
|
13
|
|
|
router = mux.NewRouter() |
|
14
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
|
15
|
|
|
getHandler(w, r, gerdu) |
|
16
|
|
|
}).Methods(http.MethodGet) |
|
17
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
|
18
|
|
|
putHandler(w, r, gerdu) |
|
19
|
|
|
}).Methods(http.MethodPut) |
|
20
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
|
21
|
|
|
deleteHandler(w, r, gerdu) |
|
22
|
|
|
}).Methods(http.MethodDelete) |
|
23
|
|
|
router.Handle("/metrics", promhttp.Handler()) |
|
24
|
|
|
return router |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
func HttpServe(host string, gerdu cache.UnImplementedCache) { |
|
|
|
|
|
|
28
|
|
|
router := newRouter(gerdu) |
|
29
|
|
|
log.Printf("Gerdu started listening HTTP on %s\n", host) |
|
30
|
|
|
log.Fatal(http.ListenAndServe(host, router)) |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
func HttpServeTLS(host string, tlsCert, tlsKey string, gerdu cache.UnImplementedCache) { |
|
|
|
|
|
|
34
|
|
|
router := newRouter(gerdu) |
|
35
|
|
|
log.Printf("Gerdu started listening HTTPS TLS on %s\n", host) |
|
36
|
|
|
log.Fatal(http.ListenAndServeTLS(host, tlsCert, tlsKey, router)) |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
func putHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
|
40
|
|
|
vars := mux.Vars(r) |
|
41
|
|
|
key := vars["key"] |
|
42
|
|
|
buf := new(bytes.Buffer) |
|
43
|
|
|
_, err := buf.ReadFrom(r.Body) |
|
44
|
|
|
if err != nil { |
|
45
|
|
|
log.Fatal(err.Error()) |
|
46
|
|
|
return |
|
47
|
|
|
} |
|
48
|
|
|
value := buf.String() |
|
49
|
|
|
|
|
50
|
|
|
created := gerdu.Put(key, value) |
|
51
|
|
|
if !created { |
|
52
|
|
|
log.Printf("HTTP UPDATE Key: %s Value: %s\n", key, value) |
|
53
|
|
|
} else { |
|
54
|
|
|
log.Printf("HTTP INSERT Key: %s Value: %s\n", key, value) |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if created { |
|
58
|
|
|
w.WriteHeader(http.StatusCreated) |
|
59
|
|
|
} else { |
|
60
|
|
|
w.WriteHeader(http.StatusNoContent) |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
func getHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
|
65
|
|
|
vars := mux.Vars(r) |
|
66
|
|
|
key := vars["key"] |
|
67
|
|
|
if value, ok := gerdu.Get(key); ok { |
|
68
|
|
|
log.Printf("HTTP RETREIVED Key: %s Value: %s\n", key, value) |
|
69
|
|
|
w.WriteHeader(http.StatusOK) |
|
70
|
|
|
_, _ = w.Write([]byte(value)) |
|
71
|
|
|
} else { |
|
72
|
|
|
log.Printf("HTTP MISSED Key: %s \n", key) |
|
73
|
|
|
w.WriteHeader(http.StatusNotFound) |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
func deleteHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache) { |
|
78
|
|
|
vars := mux.Vars(r) |
|
79
|
|
|
key := vars["key"] |
|
80
|
|
|
if ok := gerdu.Delete(key); ok { |
|
81
|
|
|
log.Printf("HTTP DELETED Key: %s\n", key) |
|
82
|
|
|
w.WriteHeader(http.StatusOK) |
|
83
|
|
|
} else { |
|
84
|
|
|
log.Printf("HTTP MISSED Key: %s \n", key) |
|
85
|
|
|
w.WriteHeader(http.StatusNotFound) |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|