-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
141 lines (121 loc) · 3.22 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"github.com/gorilla/mux"
"io/ioutil"
"log"
"math"
"net/http"
"strconv"
"time"
)
var ds datastorer
var timeout = time.Millisecond * 100
func main() {
if d, err := newDatastore(); err != nil {
log.Fatalln(err)
} else {
ds = datastorer(d)
}
r := mux.NewRouter()
kr := r.PathPrefix("/key/{key}").Subrouter()
kr.Methods("GET").HandlerFunc(getHandler)
kr.Methods("POST").HandlerFunc(postHandler)
kr.Methods("DELETE").HandlerFunc(deleteHandler)
kr.Methods("PUT").HandlerFunc(putHandler)
if err := http.ListenAndServe(":8080", r); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func getHandler(w http.ResponseWriter, r *http.Request) {
t0 := time.Now().UnixNano()
k := mux.Vars(r)["key"]
if err := ds.validKey(k); err != nil {
http.Error(w, k+": invalid key", http.StatusBadRequest)
return
}
ch := make(chan []byte, 1)
go func() {
ch <- ds.get(k)
}()
select {
case v := <-ch:
if v != nil {
log.Println("get ["+k+"] in", timeSpent(t0), "ms")
w.Write(v)
} else {
log.Println(k + ": not found")
http.Error(w, k+": not found", http.StatusNotFound)
}
case <-time.After(timeout):
returnTimeout(w, k)
}
}
func postHandler(w http.ResponseWriter, r *http.Request) {
t0 := time.Now().UnixNano()
k := mux.Vars(r)["key"]
ttl, _ := strconv.Atoi(r.FormValue("ttl"))
if v, err := ioutil.ReadAll(r.Body); err != nil {
http.Error(w, k+": can't get value", http.StatusBadRequest)
return
} else {
if err = ds.validKey(k); err == nil {
if err = ds.validValue(v); err == nil {
go func() {
ds.set(k, v, ttl)
}()
log.Println("set ["+k+"] in", timeSpent(t0), "ms")
w.WriteHeader(http.StatusCreated)
return
}
}
datastoreErrorToHTTPError(err, w)
}
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
t0 := time.Now().UnixNano()
k := mux.Vars(r)["key"]
if err := ds.delete(k); err == nil {
log.Println("delete ["+k+"] in", timeSpent(t0), "ms")
w.WriteHeader(http.StatusNoContent)
} else {
datastoreErrorToHTTPError(err, w)
}
}
func putHandler(w http.ResponseWriter, r *http.Request) {
t0 := time.Now().UnixNano()
k := mux.Vars(r)["key"]
if v, err := ioutil.ReadAll(r.Body); err != nil {
http.Error(w, k+": can't get value", http.StatusBadRequest)
return
} else {
if err = ds.append(k, v); err == nil {
log.Println("append ["+k+"] in", timeSpent(t0), "ms")
w.WriteHeader(http.StatusOK)
} else {
datastoreErrorToHTTPError(err, w)
}
}
}
func returnTimeout(w http.ResponseWriter, k string) {
log.Println(k + ": timeout")
http.Error(w, k+": timeout", http.StatusRequestTimeout)
}
func timeSpent(t0 int64) int64 {
return int64(math.Floor(float64(time.Now().UnixNano()-t0)/1000000 + .5))
}
func datastoreErrorToHTTPError(err error, w http.ResponseWriter) {
switch err {
case errNotFound:
http.Error(w, "key not found", http.StatusNotFound)
case errEmptyBody:
http.Error(w, "empty value", http.StatusBadRequest)
case errOversizedBody:
http.Error(w, "oversized value", http.StatusBadRequest)
case errInvalidKey:
http.Error(w, "invalid key", http.StatusBadRequest)
case errKeyExists:
http.Error(w, "key exists", http.StatusBadRequest)
default:
http.Error(w, "cache server error", http.StatusInternalServerError)
}
}