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" |
9
|
|
|
"net/http" |
10
|
|
|
"strconv" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
func httpServe(httpPort int, gerdu cache.UnImplementedCache, verbose bool) (host string, router *mux.Router) { |
14
|
|
|
host = ":" + strconv.Itoa(httpPort) |
15
|
|
|
router = mux.NewRouter() |
16
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
17
|
|
|
getHandler(w, r, gerdu, verbose) |
18
|
|
|
}).Methods(http.MethodGet) |
19
|
|
|
router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) { |
20
|
|
|
putHandler(w, r, gerdu, verbose) |
21
|
|
|
}).Methods(http.MethodPut) |
22
|
|
|
router.Handle("/metrics", promhttp.Handler()) |
23
|
|
|
return host, router |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
func HttpServe(httpPort int, gerdu cache.UnImplementedCache, verbose bool) { |
|
|
|
|
27
|
|
|
host, router := httpServe(httpPort, gerdu, verbose) |
28
|
|
|
log.Printf("Gerdu started listening HTTP on %d port\n", httpPort) |
29
|
|
|
log.Fatal(http.ListenAndServe(host, router)) |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
func HttpServeTLS(httpPort int, tlsCert, tlsKey string, gerdu cache.UnImplementedCache, verbose bool) { |
|
|
|
|
33
|
|
|
host, router := httpServe(httpPort, gerdu, verbose) |
34
|
|
|
log.Printf("Gerdu started listening HTTPS TLS on %d port\n", httpPort) |
35
|
|
|
log.Fatal(http.ListenAndServeTLS(host, tlsCert, tlsKey, router)) |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
func putHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache, verbose bool) { |
39
|
|
|
vars := mux.Vars(r) |
40
|
|
|
key := vars["key"] |
41
|
|
|
buf := new(bytes.Buffer) |
42
|
|
|
_, err := buf.ReadFrom(r.Body) |
43
|
|
|
if err != nil { |
44
|
|
|
log.Fatal(err.Error()) |
45
|
|
|
return |
46
|
|
|
} |
47
|
|
|
value := buf.String() |
48
|
|
|
|
49
|
|
|
created := gerdu.Put(key, value) |
50
|
|
|
if verbose { |
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, verbose bool) { |
65
|
|
|
vars := mux.Vars(r) |
66
|
|
|
key := vars["key"] |
67
|
|
|
if value, ok := gerdu.Get(key); ok { |
68
|
|
|
if verbose { |
69
|
|
|
log.Printf("HTTP RETREIVED Key: %s Value: %s\n", key, value) |
70
|
|
|
} |
71
|
|
|
w.WriteHeader(http.StatusOK) |
72
|
|
|
_, _ = w.Write([]byte(value)) |
73
|
|
|
} else { |
74
|
|
|
if verbose { |
75
|
|
|
log.Printf("HTTP MISSED Key: %s \n", key) |
76
|
|
|
} |
77
|
|
|
w.WriteHeader(http.StatusNotFound) |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|