-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
323 lines (285 loc) · 10.8 KB
/
server.ts
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const express = require('express')
// const fs = require('fs')
// const atob = require('atob')
const download = require('image-downloader')
const next = require('next')
const bodyParser = require('body-parser')
const multer = require('multer')
const storage = require('./storage')
import axios from 'axios'
import api from './api'
interface INeighborhood {
id: number
neighborhoodId: string,
Name: string,
City: string,
State: string,
QRLink: string | null,
OrgName: string | null,
published_at: string | null,
created_at: string,
updated_at: string,
houses: string[]
}
export interface IAxRegisterTreaterReqBody {
treaterId: string
neighborhoodId: string
username: string,
email: string,
password: string
provider: string,
confirmed: boolean,
blocked: boolean,
role: {
id: number,
name: string,
description: string,
type: string
},
created_at: string,
updated_at: string,
Phone: string | null,
photoLink: string | null,
age: number | null,
visitedStories: string[],
neighborhoods: INeighborhood[]
}
interface IGenerateQRData {
id: string
for?: 'neighborhood' | 'house' | 'treater'
}
export interface IAxGenerateQRReqBody {
type: 'neighborhood' | 'house' | 'treater'
data: IGenerateQRData
}
const BUCKET_NAME = 'goats-hackathon-2020'
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const multerMid = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024,
},
})
// const dataURItoBlob = (dataURI: string) => {
// // convert base64 to raw binary data held in a string
// // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
// var byteString = atob(dataURI.split(',')[1])
// // separate out the mime component
// var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// // write the bytes of the string to an ArrayBuffer
// var ab = new ArrayBuffer(byteString.length)
// // create a view into the buffer
// var ia = new Uint8Array(ab)
// // set the bytes of the buffer to the correct values
// for (var i = 0; i < byteString.length; i++) {
// ia[i] = byteString.charCodeAt(i)
// }
// // write the ArrayBuffer to a blob, and you're done
// var blob = new Blob([ab], { type: mimeString.toLowerCase() })
// return blob
// }
app.prepare()
.then(() => {
const server = express()
server.use(multerMid.single('file'))
server.use(bodyParser.urlencoded({ extended: false }))
server.use(bodyParser.json())
server.use('/api', api)
server.get('/p/:id', (req, res) => {
const actualPage = '/post'
const queryParams = { id: req.params.id }
app.render(req, res, actualPage, queryParams)
})
server.get('/', (req, res) => {
const actualPage = '/home'
const queryParams = { id: req.params.id }
app.render(req, res, actualPage, queryParams)
})
server.post('/wines', async (req, res) => {
try {
const resp = await axios.get(`https://sampleapis.com/wines/api/${req.body.wineType}`)
res.send({ error: false, datum: resp.data })
} catch (err) {
res.send({ error: true, datum: err })
}
})
server.post('/register-treater', async (req, res) => {
const defaults: IAxRegisterTreaterReqBody = {
treaterId: "fh7gy38b93b2bb29b2di92b92di",
username: "kyle",
neighborhoodId: '7g2d838h82d8g828g48',
email: "[email protected]",
password: "Grammerhub101",
provider: "local",
confirmed: true,
blocked: false,
role: {
id: 4,
name: "treater",
description: "Trick or Treater",
type: "treater"
},
created_at: "2020-10-24T23:03:39.693Z",
updated_at: "2020-10-24T23:04:44.672Z",
Phone: null,
photoLink: null,
age: null,
visitedStories: [],
neighborhoods: [
{
id: 1,
Name: "Neighborhood 1",
City: "Bushwick",
State: "FL",
QRLink: null,
OrgName: null,
published_at: null,
created_at: "2020-10-24T23:04:26.347Z",
updated_at: "2020-10-24T23:04:26.357Z",
neighborhoodId: '7g2d838h82d8g828g48',
houses: [],
},
]
}
const body = { ...defaults, ...req.body } as IAxRegisterTreaterReqBody
try {
const resp = await axios.post(`https://arcane-depths-05392.herokuapp.com/auth/local/register`, body)
const qrImage = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${JSON.stringify(body)}`
// resp = await axios.get(qrImage)
console.log(qrImage)
const localFile = `to-be-uploaded/treaterPhoto/${resp.data.user.id}.png`
// fs.createWriteStream(localFile).write(resp.data);
const opts = {
url: qrImage,
dest: localFile // will be saved to /path/to/dest/image.jpg
}
const downloaded = await download.image(opts)
console.log('DOWNLOADED============', downloaded.filename)
const options = {
destination: storage.bucket(BUCKET_NAME).file(`treaterPhoto/${resp.data.user.id}.png`),
resumable: false
}
const storageResp = await storage.bucket(BUCKET_NAME).upload(localFile, options)
console.log(storageResp)
// https://storage.googleapis.com/goats-hackathon-2020//neighborhood/1.png
const qr = `https://storage.googleapis.com/${BUCKET_NAME}/treaterPhoto/${resp.data.user.id}.png`
const updateQRResp = await axios.put(`https://arcane-depths-05392.herokuapp.com/users/${resp.data.user.id}`, { photoLink: qr })
console.log('UPDATED', updateQRResp)
res.send({ error: false, datum: updateQRResp.data, sent: { ...defaults, ...req.body }, id: body.treaterId })
} catch (err) {
console.log(err)
res.send({ error: true, datum: err })
}
})
server.post('/generate-qr', async (req, res) => {
const body = req.body as IAxGenerateQRReqBody
try {
// let resp: AxiosResponse<any>
let localFile: string = 'to-be-uploaded/'
let qrImage
let opts
let downloaded
let options
let storageResp
let qr
let updateQRResp
switch(body.type) {
case 'neighborhood':
qrImage = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${JSON.stringify(body)}`
// resp = await axios.get(qrImage)
console.log(qrImage)
localFile = `to-be-uploaded/${body.type}/${body.data.id}.png`
// fs.createWriteStream(localFile).write(resp.data);
opts = {
url: qrImage,
dest: localFile // will be saved to /path/to/dest/image.jpg
}
downloaded = await download.image(opts)
console.log('DOWNLOADED============', downloaded.filename)
options = {
destination: storage.bucket(BUCKET_NAME).file(`${body.type}/${body.data.id}.png`),
resumable: false
}
storageResp = await storage.bucket(BUCKET_NAME).upload(localFile, options)
console.log(storageResp)
// https://storage.googleapis.com/goats-hackathon-2020//neighborhood/1.png
qr = `https://storage.googleapis.com/${BUCKET_NAME}/${body.type}/${body.data.id}.png`
updateQRResp = await axios.put(`https://arcane-depths-05392.herokuapp.com/${body.type}s/${body.data.id}`, { QRLink: qr })
console.log('UPDATED', updateQRResp)
res.send({ error: false, datum: updateQRResp.data, id: body.data.id, qr })
return
case 'house':
qrImage = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${JSON.stringify(body)}`
// resp = await axios.get(qrImage)
console.log(qrImage)
localFile = `to-be-uploaded/${body.type}/${body.data.id}.png`
// fs.createWriteStream(localFile).write(resp.data);
opts = {
url: qrImage,
dest: localFile // will be saved to /path/to/dest/image.jpg
}
downloaded = await download.image(opts)
console.log('DOWNLOADED============', downloaded.filename)
options = {
destination: storage.bucket(BUCKET_NAME).file(`${body.type}/${body.data.id}.png`),
resumable: false
}
storageResp = await storage.bucket(BUCKET_NAME).upload(localFile, options)
console.log(storageResp)
// https://storage.googleapis.com/goats-hackathon-2020//neighborhood/1.png
qr = `https://storage.googleapis.com/${BUCKET_NAME}/${body.type}/${body.data.id}.png`
updateQRResp = await axios.put(`https://arcane-depths-05392.herokuapp.com/${body.type}s/${body.data.id}`, { QRLink: qr })
console.log('UPDATED', updateQRResp)
res.send({ error: false, datum: updateQRResp.data, id: body.data.id, qr })
return
case 'treater':
qrImage = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${JSON.stringify(body)}`
// resp = await axios.get(qrImage)
console.log(qrImage)
localFile = `to-be-uploaded/${body.type}/${body.data.id}.png`
// fs.createWriteStream(localFile).write(resp.data);
opts = {
url: qrImage,
dest: localFile // will be saved to /path/to/dest/image.jpg
}
downloaded = await download.image(opts)
console.log('DOWNLOADED============', downloaded.filename)
options = {
destination: storage.bucket(BUCKET_NAME).file(`${body.type}/${body.data.id}.png`),
resumable: false
}
storageResp = await storage.bucket(BUCKET_NAME).upload(localFile, options)
console.log(storageResp)
// https://storage.googleapis.com/goats-hackathon-2020//neighborhood/1.png
qr = `https://storage.googleapis.com/${BUCKET_NAME}/${body.type}/${body.data.id}.png`
updateQRResp = await axios.put(`https://arcane-depths-05392.herokuapp.com/${body.type}s/${body.data.id}`, { QRLink: qr })
console.log('UPDATED', updateQRResp)
res.send({ error: false, datum: updateQRResp.data, id: body.data.id, qr })
return
return
default:
res.send({ error: true, datum: 'Unkown qr type' })
return
}
} catch (err) {
console.log(err)
res.send({ error: true, datum: err })
return
}
})
// This is here to catch all other routes
server.get('*', (req, res) => {
return handle(req, res)
})
// This starts the server
server.listen(process.env.PORT || 3000, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${process.env.PORT || 3000}`)
})
})
.catch((ex:any) => {
console.error(ex.stack)
process.exit(1)
})