/
main.go
163 lines (137 loc) · 3.33 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
"github.com/bbhmakerlab/debug"
"github.com/bbhmakerlab/ikea-catalogue-giveaway-2016/store"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/lib/pq"
)
const (
MaxEntries = 8000
PostalCodeLength = 5
)
func home(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Content-Type", "text/html")
http.ServeFile(w, r, "public/main.html")
}
func submit(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Access-Control-Allow-Origin", "http://www.malaysia-ikea.com")
// Name
name := r.FormValue("name")
if len(name) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Name is empty"))
return
}
// Address 1
address1 := r.FormValue("address1")
if len(address1) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Address 1 is empty"))
return
}
// Address 2
address2 := r.FormValue("address2")
// Postal Code
postalCode := r.FormValue("postal_code")
if len(postalCode) != 5 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Postal code length must be " + itoa(PostalCodeLength) + " digits long"))
return
}
// City
city := r.FormValue("city")
if len(city) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("City is empty"))
return
}
// State
state := r.FormValue("state")
if len(state) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("State is empty"))
return
}
// Country
country := r.FormValue("country")
if country != "Malaysia" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Country name is invalid"))
return
}
// Email
email := r.FormValue("email")
if len(email) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Email is empty"))
return
}
// Check number of entries already submitted
if n, err := store.CountEntries(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else if n >= MaxEntries {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("out of stock"))
return
}
// Insert to database
if err := store.InsertEntry(name, address1, address2, city, state, country, postalCode, email); err != nil {
e, ok := err.(*pq.Error)
if ok && e.Code == "23505" { // unique violation
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("duplicate"))
return
} else {
debug.Warn(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
}
// Check number of entries already submitted
func count(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Access-Control-Allow-Origin", "http://www.malaysia-ikea.com")
if n, err := store.CountEntries(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.Write([]byte(fmt.Sprintf("%d", n)))
}
}
func itoa(i int) string {
return strconv.Itoa(i)
}
func main() {
// parse CLI options
flag.Parse()
// start database
store.Init()
// configure server
router := mux.NewRouter()
router.HandleFunc("/", home)
router.HandleFunc("/submit", submit)
router.HandleFunc("/count", count)
// start server
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
n := negroni.Classic()
n.UseHandler(router)
n.Run(":" + port)
}