-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (101 loc) · 2.81 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"sync"
)
func main() {
store := &Store{
data: map[string][]*entry{},
mu: sync.Mutex{},
}
mux := http.NewServeMux()
mux.HandleFunc("POST /entries/{key}", postEntryFunc(store))
mux.HandleFunc("GET /entries/{key}", getEntryFunc(store))
mux.HandleFunc("DELETE /entries/{key}", deleteEntryFunc(store))
mux.HandleFunc("GET /entries/{key}/history", getHistoryFunc(store))
mux.HandleFunc("DELETE /entries/{key}/history", deleteHistoryFunc(store))
http.ListenAndServe(":8080", mux)
}
func postEntryFunc(store *Store) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
key := r.PathValue("key")
if key == "" {
http.Error(w, "key not provided", http.StatusBadRequest)
return
}
var p struct {
Value string `json:"value"`
}
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
if err.Error() == "EOF" {
http.Error(w, "request body is empty", http.StatusBadRequest)
return
}
http.Error(w, "request body decode error", http.StatusBadRequest)
return
}
if p.Value == "" {
http.Error(w, "value must be set", http.StatusBadRequest)
return
}
store.Update(key, p.Value)
fmt.Fprintf(w, "POST %v=%v", key, p.Value)
}
}
func getEntryFunc(store *Store) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
key := r.PathValue("key")
if key == "" {
http.Error(w, "key not provided", http.StatusBadRequest)
return
}
value, err := store.Get(key)
if err != nil {
http.Error(w, fmt.Sprintf("%v does not exist", key), http.StatusNotFound)
return
}
fmt.Fprintf(w, "GET %v=%v", key, value)
}
}
func deleteEntryFunc(store *Store) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
key := r.PathValue("key")
if key == "" {
http.Error(w, "key not provided", http.StatusBadRequest)
return
}
store.Delete(key)
}
}
func getHistoryFunc(store *Store) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
key := r.PathValue("key")
if key == "" {
http.Error(w, "key not provided", http.StatusBadRequest)
return
}
entries, err := store.GetAll(key)
if err != nil {
http.Error(w, fmt.Sprintf("%v does not exist", key), http.StatusNotFound)
return
}
err = json.NewEncoder(w).Encode(entries)
if err != nil {
http.Error(w, fmt.Sprintf("encode: %v", err), http.StatusInternalServerError)
return
}
}
}
func deleteHistoryFunc(store *Store) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
key := r.PathValue("key")
if key == "" {
http.Error(w, "key not provided", http.StatusBadRequest)
return
}
store.DeleteAll(key)
}
}