-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
110 lines (89 loc) · 3.19 KB
/
server.js
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
const Koa = require('koa')
const Router = require('koa-router')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const cors = require('@koa/cors')
const fs = require('fs')
const { helpHandler } = require('./routes/help')
const { retrieveFiles, retrieveFile, retrieveCase } = require('./routes/retrieve')
const { createSubmission } = require('./routes/submit')
const { getResponseBody } = require('./utils/helpers')
const { isAPIKeyValid, isFileValid } = require('./utils/utils')
const app = new Koa()
const router = new Router()
async function validateKey(ctx, next) {
if(ctx.request.headers['authorization'] === undefined){
ctx.status = 401
ctx.body = getResponseBody('Authorization failed!', 401)
return;
}
ctx.state.key = ctx.request.headers['authorization'].split('Bearer ')[1]
const {filename, type } = ctx.request.body
const validKey = await isAPIKeyValid(ctx.state.key)
// const validFilename = await isFileValid(filename ,type)
if (!validKey) {
ctx.status = 401
ctx.body = getResponseBody('Invalid API Key!', 401)
}
// else if (!validFilename) {
// ctx.status = 400
// ctx.body = 'Bad filename'
// }
else {
await next()
}
}
// app.use(async (ctx, next) => {
// ctx.req.on('data', (data) => console.log(data.toString('utf-8')))
// ctx.req.on('end', ()=> next())
// })
/*********************************************
* TODO: Write middleware that reads master file
* only once and stores it in ctx.
*********************************************/
app.use(async (ctx, next) => {
const masterFileLocation = `${__dirname}/files/dir.json`
ctx.state.masterFile = JSON.parse(fs.readFileSync(masterFileLocation))
await next()
})
app.use(bodyparser({
multipart: true,
jsonLimit: '20mb'
}))
app.use(logger())
app.use(cors({
'origin': '*', //TODO: Limit Origins?
'credentials': 'true',
'allowMethods': 'POST,GET,OPTIONS,PUT,DELETE',
'allowHeaders': 'Accept,Content-Type,Content-Length,Accept-Encoding,X-CSRF-Token,Authorization'
}))
app.use(router.routes())
app.use(router.allowedMethods())
/***************** ROUTES *******************************/
router.use(['/validate', '/status', '/files', '/files/:submissionId', '/files/:submissionId/:caseId', '/case/:caseId', '/submit'], validateKey)
router.get('/help', helpHandler)
router.get('/validate', ctx => {
ctx.status = 200
ctx.body = getResponseBody('Key Valid!', 200)
})
router.get('/status', ctx => {
ctx.status = 200
ctx.body = getResponseBody('Ok!', 200)
})
router.get('/files', retrieveFiles)
router.get('/files/:submissionId', retrieveFile)
router.get('/files/:submissionId/:caseId', retrieveFile)
router.get('/case/:caseId', retrieveCase)
router.post('/submit', async(ctx) => await createSubmission(ctx))
router.all('/', (ctx) => {
ctx.status = 200
ctx.body = getResponseBody('Ok!', 200)
})
router.get('*', ctx => {
ctx.status = 404
ctx.body = getResponseBody('Resource Not Found!', 404)
})
/**********************************************************/
const port = process.env.PORT || 3000
app.listen(port)
console.log(`🌍 Server listening on port ${port}`)