-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
179 lines (148 loc) · 4.21 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
// Represents a character creation request.
type CreateRequest struct {
ID string `json:"id"`
Race string `json:"race"`
Strength int `json:"strength"`
Dexterity int `json:"dexterity"`
Wisdom int `json:"wisdom"`
}
type UpdateRequest struct {
ID string `json:"id"`
ProID string `json:"pro_id"`
Experience int `json:"points"`
Gold int `json:"gold"`
Name string `json:"name"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
// Given a message, responds with a JSON object containing that message
// as an error string/
func RespondBadRequest(w http.ResponseWriter, message string) {
log.WithFields(log.Fields{
"time": time.Now(),
"message": message,
}).Error("Received a bad request")
errorResponse := ErrorResponse{Error: message}
http.Error(w, "", http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(errorResponse)
}
func CharactersByPoints(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"time": time.Now(),
}).Info("Received characters sorted by points request")
characters, err := CharactersInPointOrder()
if err != nil {
RespondBadRequest(w, "unable to query characters by points")
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err = json.NewEncoder(w).Encode(characters); err != nil {
RespondBadRequest(w, err.Error())
return
}
}
// Handler for character creation
// ENDPOINT: /characters/create
func CharacterCreate(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"time": time.Now(),
}).Info("Received character create request")
var requestData CreateRequest
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err != nil {
RespondBadRequest(w, err.Error())
return
}
if err := r.Body.Close(); err != nil {
RespondBadRequest(w, err.Error())
return
}
if err := json.Unmarshal(body, &requestData); err != nil {
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
character, err := CreateNewCharacter(requestData)
fmt.Println(requestData.ID)
if err != nil {
RespondBadRequest(w, err.Error())
return
}
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(character); err != nil {
RespondBadRequest(w, err.Error())
return
}
}
// Handler for character updating
// ENDPOINT: /characters/update
func CharacterUpdate(w http.ResponseWriter, r *http.Request) {
var requestData UpdateRequest
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
RespondBadRequest(w, err.Error())
return
}
if err := r.Body.Close(); err != nil {
RespondBadRequest(w, err.Error())
return
}
if err := json.Unmarshal(body, &requestData); err != nil {
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
RespondBadRequest(w, "Bad character update request")
return
}
}
character, err := UpdateCharacter(requestData)
if err != nil {
RespondBadRequest(w, "Error updating character")
return
}
log.WithFields(log.Fields{
"time": time.Now(),
"id": character.ID,
}).Info("Received character update request")
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(character); err != nil {
RespondBadRequest(w, err.Error())
return
}
}
// Handler for getting a character
// ENDPOINT: /character/{identifier}
func CharacterShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
identifier := vars["identifier"]
log.WithFields(log.Fields{
"time": time.Now(),
"id": identifier,
}).Info("Received character display request")
result, err := FindCharacter(identifier)
if err != nil {
log.WithFields(log.Fields{
"time": time.Now(),
"id": identifier,
}).Warn("Character not found")
http.Error(w, err.Error(), http.StatusNotFound)
return
}
resultString, _ := json.Marshal(result)
fmt.Fprintln(w, "", string(resultString))
}