This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (155 loc) · 6.72 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"fmt"
"github.com/guitarpawat/wsp-ecommerce/handler/template"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/guitarpawat/middleware"
"github.com/guitarpawat/wsp-ecommerce/db"
solidenv "github.com/guitarpawat/wsp-ecommerce/env"
"github.com/guitarpawat/wsp-ecommerce/handler"
)
var env = solidenv.GetEnv()
func main() {
r := mux.NewRouter()
fs := http.StripPrefix("/static/", http.FileServer(http.Dir("static")))
r.PathPrefix("/static/").Handler(fs)
r.HandleFunc("/image/{name}", handler.Images)
r.Handle("/", handlePage(handler.Home))
r.Handle("/about/", handlePage(handler.About))
r.Handle("/cart/", handlePage(handler.Cart))
r.Handle("/contact/", handlePage(handler.Contact))
r.Handle("/product/", handlePage(handler.Product))
r.Handle("/product/sale/", handlePage(handler.Sale))
r.Handle("/product/sort/type={meattype}&priceSort={price_sort}/", handlePage(handler.ProductSortType))
r.Handle("/product/search/name={name}&startPrice={startPrice}&endPrice={endPrice}&priceSort={price_sort}/", handlePage(handler.ProductSearch))
r.Handle("/product/page={page}/", handlePage(handler.ProductPaging))
r.Handle("/product/sale/page={page}/", handlePage(handler.Sale))
r.Handle("/product/sort/type={meattype}&priceSort={price_sort}/page={page}/", handlePage(handler.ProductSortTypePaging))
r.Handle("/product/search/name={name}&startPrice={startPrice}&endPrice={endPrice}&priceSort={price_sort}/page={page}/", handlePage(handler.ProductSearchPaging))
r.Handle("/product-detail/{meatId}/", handlePage(handler.ProductDetail))
r.Handle("/profile/", handlePage(handler.Profile))
r.Handle("/profile-edit/", handlePage(handler.ProfileEdit))
r.Handle("/add-product/", handlePage(handler.AddProduct))
r.Handle("/product-stock/", handlePage(handler.ProductStock))
r.Handle("/sales-history/", handlePage(handler.SaleHistory))
r.Handle("/checkout/", handlePage(handler.Checkout)).Methods("GET")
r.Handle("/checkout/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.ProceedCheckout),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home))).
Methods("POST")
r.Handle("/regis/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.Regis),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home))).
Methods("POST")
r.Handle("/login/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.Login),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home))).
Methods("POST")
r.Handle("/logout/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.Logout),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home)))
r.Handle("/add_meat/", handlePage(handler.AddMeat))
r.Handle("/regis_meat/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.RegisMeat),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home))).
Methods("POST")
r.Handle("/edit_meat/{meatID}/", handlePage(handler.EditMeat))
r.Handle("/update_meat/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.UpdateMeat),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home))).
Methods("POST")
r.Handle("/edit-profile/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.EditProfile),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Profile))).
Methods("POST")
r.Handle("/product/add_cart:{meatId}&quantity={quantity}/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.AddCart),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Cart))).
Methods("GET")
r.Handle("/cart_rm/{meatID}/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.RemoveMeatFromCart),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Cart))).
Methods("GET")
r.Handle("/update_cart/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.UpdateCart),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Cart))).
Methods("POST")
r.Handle("/sales_history/", handlePage(handler.SaleHistory))
r.Handle("/owner/", handlePage(handler.Owner))
httpr := mux.NewRouter()
httpr.PathPrefix("/").HandlerFunc(handler.RedirectToHTTPS)
template.Validate()
if env == solidenv.Production {
log.Fatalln(http.ListenAndServe(":"+solidenv.GetPort(), forceSslHeroku(r)))
} else if env == solidenv.CI {
r.Handle("/mock/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.Mock),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home)))
fmt.Println("Running on port 8000")
log.Fatalln(http.ListenAndServe(":8000", r))
} else {
r.Handle("/mock/", middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.Mock),
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(handler.Home)))
fmt.Println("Running on port 8000 and 4433")
go func() {
log.Fatalln(http.ListenAndServeTLS(":4433", "ssl/server.crt", "ssl/server.key", r))
}()
log.Fatalln(http.ListenAndServe(":8000", httpr))
}
db.MockUser()
}
func handlePage(df middleware.DoableFunc) http.Handler {
return middleware.MakeMiddleware(nil,
middleware.DoableFunc(handler.CheckSession),
middleware.DoableFunc(handler.BuildHeader),
middleware.DoableFunc(df))
}
func forceSslHeroku(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("x-forwarded-proto") != "https" {
sslUrl := "https://" + r.Host + r.RequestURI
http.Redirect(w, r, sslUrl, http.StatusPermanentRedirect)
return
}
next.ServeHTTP(w, r)
})
}