-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (77 loc) · 3.67 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
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func init() {
log.SetFlags(log.Flags() | log.Lshortfile)
}
func main() {
r := mux.NewRouter()
r.Use(accessControlMiddleware)
r.HandleFunc("/", handleUsersList).Methods("GET")
r.HandleFunc("/inactive", handleUsersListInactive).Methods("GET")
r.HandleFunc("/confirm", handleConfirmPage).Methods("GET")
// lager
r.HandleFunc("/storage/{product}/setimg/{image}", handleProductSetImage).Methods("GET")
r.HandleFunc("/storage/{product}/delete", handleProductDelete).Methods("GET")
r.HandleFunc("/storage/{product}/stock/{amount}", handleProductStock).Methods("GET")
r.HandleFunc("/storage/new", handleNewProductSubmit).Methods("POST")
r.HandleFunc("/storage", handleStorage).Methods("GET")
// images
r.HandleFunc("/img/{image}", handleImage).Methods("GET")
r.HandleFunc("/img/upload", handlePutImage).Methods("PUT")
// user releated stffs
// new user page
r.HandleFunc("/user/new", handleNewUser).Methods("GET")
r.HandleFunc("/user/new/imgselector", handleNewUserImgSelector).Methods("GET")
r.HandleFunc("/user/new", handleNewUserSubmit).Methods("POST")
//API START
r.HandleFunc("/api/img/{image}", handleImage).Methods("GET")
r.HandleFunc("/api/img/new", handlePutImageAPI).Methods("POST")
r.HandleFunc("/api/storage", handleProductsAPIList).Methods("GET")
r.HandleFunc("/api/storage/new", handleProductsAPINew).Methods("POST")
r.HandleFunc("/api/storage/new", PreflightRoot).Methods("OPTIONS")
r.HandleFunc("/api/storage/{product}", handleProductAPI).Methods("POST")
r.HandleFunc("/api/storage/{product}", PreflightRoot).Methods("OPTIONS")
r.HandleFunc("/api/products", handleProductsAPI).Methods("GET")
r.HandleFunc("/api/products/new", handleNewProductSubmit).Methods("POST")
r.HandleFunc("/api/user/new", handleUserAPINew).Methods("POST")
r.HandleFunc("/api/users", handleUserListAPI).Methods("GET")
r.HandleFunc("/api/user/{id}", handleUserGetAPI).Methods("GET")
r.HandleFunc("/api/user/{id}", handleUserAPIList).Methods("POST")
r.HandleFunc("/api/user/{id}", PreflightRoot).Methods("OPTIONS")
r.HandleFunc("/api/user/{id}/transactions", handleTransactionAPI).Methods("GET")
r.HandleFunc("/api/user/{id}/transactions", handleTransactionAPIPost).Methods("POST")
//API END
r.HandleFunc("/user/{id}", handleUserPage).Methods("GET")
r.HandleFunc("/user/{id}/buy/{product}", handleBuy).Methods("GET")
r.HandleFunc("/user/{id}/transactions/{transaction}/undo", handleTransactionUndo).Methods("GET")
r.HandleFunc("/user/{id}/transactions", handleTransactionsPage).Methods("GET")
r.HandleFunc("/user/{id}/settings", handleEditUser).Methods("GET")
r.HandleFunc("/user/{id}/delete", handleDeleteAsk).Methods("GET")
r.HandleFunc("/user/{id}/settings", handleEditUserSubmit).Methods("POST")
r.HandleFunc("/user/{id}/wallet", handleWallet).Methods("GET")
r.HandleFunc("/user/{id}/wallet/deposit/{amount}", handleWalletDeposit).Methods("GET")
r.HandleFunc("/user/{id}/wallet/withdraw/{amount}", handleWalletWithdraw).Methods("GET")
r.PathPrefix("/s/").
Handler(http.StripPrefix("/s/",
AddPrefix("html/static/",
http.FileServer(http.FS(staticFS)),
),
))
log.Println("Listening on :8080")
log.Fatal(http.ListenAndServe(":8080", r))
}
// access control and CORS middleware
func accessControlMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
next.ServeHTTP(w, r)
})
}
func PreflightRoot(respWriter http.ResponseWriter, request *http.Request) {
respWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
respWriter.Header().Set("Access-Control-Allow-Headers", "*")
}