-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
executable file
·218 lines (181 loc) · 5.48 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"bytes"
"io/ioutil"
"net/http"
"strconv"
"github.com/gorilla/mux"
"mime/multipart"
)
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome!\n")
}
func TodoIndex(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(todos); err != nil {
panic(err)
}
}
func TodoShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var todoId int
var err error
if todoId, err = strconv.Atoi(vars["todoId"]); err != nil {
panic(err)
}
todo := RepoFindTodo(todoId)
if todo.Id > 0 {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(todo); err != nil {
panic(err)
}
return
}
// If we didn't find it, 404
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}
/*
curl -H "Content-Type: application/json" -d '{"name":"New Todo"}' http://localhost:8080/todos
*/
func TodoCreate(w http.ResponseWriter, r *http.Request) {
var todo Todo
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, &todo); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
t := RepoCreateTodo(todo)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(t); err != nil {
panic(err)
}
}
/*
curl -H "Content-Type: application/json" -d '{"name":"New Todo"}' http://localhost:8080/todos
*/
func CreatePathLocation(w http.ResponseWriter, r *http.Request) {
var location Location
body, err := ioutil.ReadAll(r.Body)
fmt.Printf("%s\n", string(body))
if err != nil { panic(err)}
if err := r.Body.Close(); err != nil { panic(err) }
if err := json.Unmarshal(body, &location); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
l := RepoCreatePath(location)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(l); err != nil {
panic(err)
}
}
func Upload(client *http.Client, url string, values map[string]io.Reader) (err error) {
var b bytes.Buffer
w := multipart.NewWriter(&b)
for key, r := range values {
var fw io.Writer
if x, ok := r.(io.Closer); ok {
defer x.Close()
}
if x, ok := r.(*os.File); ok {
if fw, err = w.CreateFormFile(key, x.Name()); err != nil {
return
}
} else {
// Add other fields
if fw, err = w.CreateFormField(key); err != nil {
return
}
}
if _, err = io.Copy(fw, r); err != nil {
return err
}
}
// Don't forget to close the multipart writer.
// If you don't close it, your request will be missing the terminating boundary.
w.Close()
// Now that you have a form, you can submit it to your handler.
req, err := http.NewRequest("POST", url, &b)
if err != nil {
return
}
// Don't forget to set the content type, this will contain the boundary.
req.Header.Set("Content-Type", w.FormDataContentType())
res, err := client.Do(req)
if err != nil {
return
}
// Check the response
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("bad status: %s", res.Status)
}
return
}
func UploadFile(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 20) // Limit file size to 10
sensorFile, sensorHandler, sensorErr := r.FormFile("sensors_data")
if sensorErr != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(sensorErr)
return
}
defer sensorFile.Close()
sensorFileBytes, err := ioutil.ReadAll(sensorFile)
if err != nil {
fmt.Println(err)
}
str := string(sensorFileBytes)
fmt.Println(str)
fmt.Printf("Uploaded File: %+v\n", sensorHandler.Filename)
fmt.Printf("File Size: %+v\n", sensorHandler.Size)
fmt.Printf("MIME Header: %+v\n", sensorHandler.Header)
locationFile, locationHandler, locationErr := r.FormFile("location_data")
if locationErr != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(locationErr)
return
}
defer locationFile.Close()
formatSensorData(str)
locationFileBytes, err := ioutil.ReadAll(locationFile)
if err != nil {
fmt.Println(err)
}
s := string(locationFileBytes)
fmt.Println(s)
fmt.Printf("Uploaded File: %+v\n", locationHandler.Filename)
fmt.Printf("File Size: %+v\n", locationHandler.Size)
fmt.Printf("MIME Header: %+v\n", locationHandler.Header)
fmt.Fprintf(w, "Successfully Uploaded File\n")
}
func GetPath(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(paths); err != nil {
panic(err)
}
}