-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (112 loc) · 2.83 KB
/
main.go
File metadata and controls
131 lines (112 loc) · 2.83 KB
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
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
// See page 195.
// Http4 is an e-commerce server that registers the /list and /price
// endpoint by calling http.HandleFunc.
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"gopl.io/ch7/http4/htmllist"
)
const tpl = `
<html>
<body>
<table>
<tr>
<th>item</th>
<th>price</a></th>
</tr>
{{range .}}
<tr>
<td>{{.Item}}</td>
<td>{{.Price}}</td>
</td>
{{end}}
</body>
</html>`
//!+main
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("/update", db.update)
http.HandleFunc("/delete", db.delete)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
//!-main
type dollars float64
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
type database map[string]dollars
//Data that will be transferred to template list
type Data struct {
Item string
Price dollars
}
func (db database) list(w http.ResponseWriter, req *http.Request) {
var d []Data
for item, price := range db {
//fmt.Fprintf(w, "%s: %s\n", item, price)
d = append(d, Data{item, price})
}
htmllist.Templist(w, d, tpl, "listtemplate")
}
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) create(w http.ResponseWriter, r *http.Request) {
item := r.FormValue("item")
pricestr := r.FormValue("price")
if item == "" {
http.Error(w, "please input a name for the item", http.StatusBadRequest)
return
}
if _, ok := db[item]; ok {
http.Error(w, "item already existe in database", http.StatusBadRequest)
return
}
price, err := strconv.ParseFloat(pricestr, 2)
if err != nil {
fmt.Fprintf(w, "please input a number for the item: %q\n", item)
return
}
if db == nil {
db = make(map[string]dollars, 0)
}
db[item] = dollars(price)
}
func (db database) update(w http.ResponseWriter, r *http.Request) {
item := r.FormValue("item")
pricestr := r.FormValue("price")
if item == "" {
http.Error(w, "please input a name for the item", http.StatusBadRequest)
return
}
price, err := strconv.ParseFloat(pricestr, 2)
if err != nil {
fmt.Fprintf(w, "please input a number for the item: %q\n", item)
return
}
if _, ok := db[item]; ok {
db[item] = dollars(price)
} else {
http.Error(w, "item not found", http.StatusBadRequest)
}
}
func (db database) delete(w http.ResponseWriter, r *http.Request) {
item := r.FormValue("item")
if _, ok := db[item]; ok {
delete(db, item)
} else {
http.Error(w, "item not found", http.StatusBadRequest)
}
}