-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (129 loc) · 4.28 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
package main
import (
"./models"
"log"
"fmt"
"net/http"
"io/ioutil"
)
//Env decribes struct that contains handlers for all requests
type Env struct {
db models.Datastore
}
func main() {
const port = ":8090"
db, err := models.NewDB("warehouse.db")
if err != nil {
log.Panic(err)
}
env := &Env{db}
http.HandleFunc("/add/product", makeTxHandler(env.db.InsertProduct))
http.HandleFunc("/add/person", makeTxHandler(env.db.InsertPerson))
http.HandleFunc("/add/order", makeTxHandler(env.db.InsertOrder))
http.HandleFunc("/delete/product", makeDeleteHandler(env.db.DeleteProduct))
http.HandleFunc("/delete/person", makeDeleteHandler(env.db.DeletePerson))
http.HandleFunc("/delete/order", makeDeleteHandler(env.db.DeleteOrder))
http.HandleFunc("/products", makeGetAllHandler(env.db.AllProducts))
http.HandleFunc("/persons", makeGetAllHandler(env.db.AllPersons))
http.HandleFunc("/orders", makeGetAllHandler(env.db.AllOrders))
http.HandleFunc("/filter/product", makeFilterHandler(env.db.FilterProduct))
http.HandleFunc("/filter/person", makeFilterHandler(env.db.FilterPerson))
http.HandleFunc("/filter/order", makeFilterHandler(env.db.FilterOrder))
http.HandleFunc("/edit/product", makeTxHandler(env.db.EditProduct))
http.HandleFunc("/edit/person", makeTxHandler(env.db.EditPerson))
http.HandleFunc("/edit/order", makeTxHandler(env.db.EditOrder))
log.Print("server has started on http://127.0.0.1" + port)
log.Fatal(http.ListenAndServe(port, nil))
}
//Enable CORS policy in chrom just in case
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func makeDeleteHandler(fn func([]byte) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
if r.Method != "DELETE" {
http.Error(w, http.StatusText(405), 405)
return
}
r.ParseForm()
req, _ := ioutil.ReadAll(r.Body)
err := fn(req)
if (err != nil) {
http.Error(w, http.StatusText(500), 500)
log.Panic(err)
return
}
}
}
func makeGetAllHandler(fn func() (models.Serializable, error)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
if r.Method != "GET" {
http.Error(w, http.StatusText(405), 405)
return
}
entities, err := fn()
if err != nil {
http.Error(w, http.StatusText(500), 500)
log.Print(err)
return
}
resp, err := entities.ToJSON()
if err != nil {
http.Error(w, http.StatusText(500), 500)
log.Print(err)
return
}
fmt.Fprintln(w, resp)
}
}
func makeTxHandler(fn func([]byte) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
keys := make([]string, 0)
enableCors(&w)
if r.Method != "POST" {
http.Error(w, http.StatusText(405), 405)
return
}
r.ParseForm()
for key := range r.Form {
keys = append(keys, key)
}
err := fn([]byte(keys[0]))
if (err != nil) {
http.Error(w, http.StatusText(500), 500)
log.Print(err)
return
}
}
}
func makeFilterHandler(fn func([]byte, string) (models.Serializable, error)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
keys := make([]string, 0)
enableCors(&w)
if r.Method != "GET" {
http.Error(w, http.StatusText(405), 405)
return
}
r.ParseForm()
sort := r.Header.Get("sort")
for key := range r.Form {
keys = append(keys, key)
fmt.Println(key)
}
entities, err := fn([]byte(keys[0]), sort)
if err != nil {
http.Error(w, http.StatusText(500), 500)
log.Print(err)
return
}
resp, err := entities.ToJSON()
if err != nil {
http.Error(w, http.StatusText(500), 500)
log.Print(err)
return
}
fmt.Fprintln(w, resp)
}
}