-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (38 loc) · 1.13 KB
/
index.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
import path from 'path';
import rimraf from 'rimraf';
import AdmZip from 'adm-zip';
import express from 'express';
import rateLimit from 'express-rate-limit';
import testsAPI from './utils/testsAPI.js';
const app = express();
app.use(
'/*',
rateLimit({
windowMs: 5 * 60 * 1000,
max: 100,
})
);
app.get('/', (req, res) => {
res.end('Send Get Requests to `/[username]/[slug]`');
});
app.get('/:username/:slug', async (req, res) => {
const { username, slug } = req.params;
const { testname } = req.query;
const result = await testsAPI(username, slug);
if(result[0] === true) {
if(testname) {
const zipPath = path.join(process.cwd(), `${slug.toLowerCase()}.zip`)
const zip = new AdmZip(zipPath);
const entry = zip.getEntry(`${testname.toLowerCase()}.test.${result[1] === 'nodejs' ? 'js' : result[1] === 'python3' ? 'py' : 'java'}`);
res.status(200).end(zip.readAsText(entry));
rimraf(zipPath, () => {});
} else {
const zipPath = path.join(process.cwd(), `${slug.toLowerCase()}.zip`)
res.status(200).download(zipPath)
rimraf(zipPath, () => {});
}
} else {
res.status(403).end(result);
}
});
app.listen(3000);