-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
168 lines (137 loc) · 4.15 KB
/
routes.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
package main
import (
"encoding/json"
"errors"
"github.com/alex1ai/ugr-master-cc/authentication"
. "github.com/alex1ai/ugr-master-cc/data"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"net/http"
"strconv"
)
func sendResponse(writer http.ResponseWriter, data []byte, status ...int) {
writer.Header().Set("Content-Type", "application/json")
if len(status) > 0 {
writer.WriteHeader(status[0])
}
_, err := writer.Write(data)
if err != nil {
http.Error(writer, "Could not send error", http.StatusInternalServerError)
}
}
func sendError(writer http.ResponseWriter, statusCode int, err error) {
http.Error(writer, err.Error(), statusCode)
}
// ROUTES FOR WEBSERVICE
func StatusHandler(w http.ResponseWriter, _ *http.Request) {
sendResponse(w, []byte("{\"status\": \"OK\"}"))
}
func GetHandler(db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
lang := r.FormValue("lang")
id := r.FormValue("id")
idOk, idEmpty := validateId(id)
langOk, langEmpty := validateLang(lang)
query := make(map[string]interface{})
if idEmpty && langEmpty {
query = nil
}
if (!idOk && !idEmpty) || (!langOk && !langEmpty) {
sendError(w, http.StatusBadRequest, errors.New("bad parameters in query"))
}
if idOk {
idi, _ := strconv.Atoi(id)
query["id"] = uint(idi)
}
if langOk {
query["lang"] = lang
}
response, err := db.Query(query)
j, err := json.Marshal(response)
errorPanic(w, err)
sendResponse(w, j)
}
}
func AddContentHandler(db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var instance Content
if err := decoder.Decode(&instance); err != nil {
sendError(w, http.StatusBadRequest, err)
}
_, err := db.Add(instance)
errorPanic(w, err)
sendResponse(w, nil, http.StatusNoContent)
}
}
// TODO: If this is a put request, automatically fill Id-Number according to maximum in database
func EditContentHandler(db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var instance Content
if err := decoder.Decode(&instance); err != nil {
sendError(w, http.StatusBadRequest, err)
}
id, lang := instance.Id, instance.Language
query := map[string]interface{}{
"lang": lang,
"id": id,
}
_, err := db.Update(query, instance)
errorPanic(w, err)
sendResponse(w, nil, http.StatusNoContent)
}
}
func DeleteHandler(db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
id, _ := mux.Vars(r)["id"]
lang, _ := mux.Vars(r)["lang"]
idNumber, err := strconv.Atoi(id)
if err != nil || idNumber < 0 {
sendError(w, http.StatusBadRequest, err)
}
query := map[string]interface{}{
"lang": lang,
"id": uint(idNumber),
}
_, err = db.Delete(query)
if err != nil {
sendError(w, http.StatusInternalServerError, err)
}
sendResponse(w, nil, http.StatusNoContent)
}
}
func LoginHandler(db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var user authentication.User
if err := decoder.Decode(&user); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
tokenString, err := authentication.CreateToken(user.Name, user.Password, db)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
}
_, err = w.Write([]byte(tokenString))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func InitHandler(db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := db.Populate(10)
if err != nil {
sendError(w, http.StatusInternalServerError, err)
}
}
}
func ResetHandler(db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
db.Reset()
}
}
func ErrorHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "This is not the page you are looking for", http.StatusNotFound)
log.Infof("Route not found: %s", r.URL.Path)
}