This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (154 loc) · 4.2 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"sync"
)
func main() {
db := database{"shoes": 50, "socks": 5}
http.HandleFunc("/list", db.list)
http.HandleFunc("/price", db.price)
http.HandleFunc("/create", db.create)
http.HandleFunc("/get", db.get)
http.HandleFunc("/update", db.update)
http.HandleFunc("/delete", db.delete)
fmt.Println("usage:")
fmt.Println("http://localhost:8000/list")
fmt.Println("http://localhost:8000/price?item=:item")
fmt.Println("http://localhost:8000/create?item=:item&price=:price")
fmt.Println("http://localhost:8000/get?item=:item")
fmt.Println("http://localhost:8000/update?item=:item&price=:price")
fmt.Println("http://localhost:8000/delete?item=:item")
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
type dollars float32
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
type database map[string]dollars
func (db database) list(w http.ResponseWriter, req *http.Request) {
t := template.Must(template.New("db").Parse(`
<table>
<thead>
<tr>
<th>item</th>
<th>price</th>
</tr>
</thead>
<tbody>
{{range $key, $value := .}}
<tr>
<td>{{$key}}</td>
<td>{{$value}}</td>
</tr>
{{end}}
</tbody>
</table>
`))
if err := t.Execute(w, db); err != nil {
w.WriteHeader(http.StatusInternalServerError) // 500
fmt.Fprintln(w, "Internal Server Error")
}
}
func (db database) price(w http.ResponseWriter, req *http.Request) {
item := req.URL.Query().Get("item")
if price, ok := db[item]; ok {
fmt.Fprintf(w, "%s\n", price)
} else {
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such item: %q\n", item)
}
}
func (db database) update(w http.ResponseWriter, req *http.Request) {
item := req.URL.Query().Get("item")
if item == "" {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "missing item: %q\n", item)
return
}
if price, ok := db[item]; ok {
newPrice := req.URL.Query().Get("price")
if newPrice == "" {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "missing price: %q\n", newPrice)
} else {
p, err := strconv.ParseFloat(newPrice, 32)
if err != nil {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "not number price: %q\n", newPrice)
} else {
mutex := &sync.Mutex{}
mutex.Lock()
db[item] = dollars(p)
mutex.Unlock()
fmt.Fprintf(w, "update %q %s -> %s\n", item, price, dollars(p))
}
}
} else {
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such item: %q\n", item)
}
}
func (db database) delete(w http.ResponseWriter, req *http.Request) {
item := req.URL.Query().Get("item")
if item == "" {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "missing item: %q\n", item)
return
}
if _, ok := db[item]; ok {
mutex := &sync.Mutex{}
mutex.Lock()
delete(db, item)
mutex.Unlock()
fmt.Fprintf(w, "delete %q \n", item)
} else {
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such item: %q\n", item)
}
}
func (db database) create(w http.ResponseWriter, req *http.Request) {
item := req.URL.Query().Get("item")
if item == "" {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "missing item: %q\n", item)
return
}
if _, ok := db[item]; ok {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "%q already exists\n", item)
return
}
price := req.URL.Query().Get("price")
if price == "" {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "missing price: %q\n", price)
return
}
p, err := strconv.ParseFloat(price, 32)
if err != nil {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "not number price: %q\n", price)
return
}
mutex := &sync.Mutex{}
mutex.Lock()
db[item] = dollars(p)
mutex.Unlock()
fmt.Fprintf(w, "create %s: %s \n", item, dollars(p))
}
func (db database) get(w http.ResponseWriter, req *http.Request) {
item := req.URL.Query().Get("item")
if item == "" {
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "missing item: %q\n", item)
return
}
if price, ok := db[item]; ok {
fmt.Fprintf(w, "%s: %s\n", item, price)
} else {
w.WriteHeader(http.StatusNotFound) // 404
fmt.Fprintf(w, "no such item: %q\n", item)
}
}