Skip to content

Commit 5fa040a

Browse files
committed
clean up api server
1 parent 513900c commit 5fa040a

File tree

4 files changed

+25
-38
lines changed

4 files changed

+25
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.env
Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const http = require('http');
43
const fetch = require('node-fetch');
54

65
const AUTH = `Basic ${Buffer.from(`engine262-bot:${process.env.GH_TOKEN}`).toString('base64')}`;
@@ -31,40 +30,16 @@ function createGist(content, state) {
3130
.then((r) => r.json());
3231
}
3332

34-
const server = http.createServer((req, res) => {
33+
module.exports = (req, res) => {
3534
if (req.headers.origin !== 'https://engine262.js.org') {
36-
res.writeHead(403);
37-
res.end('403');
38-
return;
39-
}
40-
if (req.url !== '/gist') {
41-
res.writeHead(404);
42-
res.end('404');
43-
return;
35+
return res.status(403).send('403');
4436
}
4537
if (req.method !== 'POST') {
46-
res.writeHead(405);
47-
res.end('405');
48-
return;
38+
return res.status(405).send('405');
4939
}
50-
let body = '';
51-
req.on('data', (chunk) => {
52-
body += chunk;
53-
});
54-
req.on('end', () => {
55-
Promise.resolve()
56-
.then(() => JSON.parse(body))
57-
.then(({ content, state }) => createGist(content, state))
58-
.then((data) => {
59-
res.writeHead(200, { 'Content-Type': 'application/json' });
60-
res.end(JSON.stringify(data));
61-
})
62-
.catch((e) => {
63-
console.error(e); // eslint-disable-line no-console
64-
res.writeHead(500);
65-
res.end('500');
66-
});
67-
});
68-
});
69-
70-
server.listen(5000);
40+
if (!req.body || !req.body.content || !req.body.state) {
41+
return res.status(400).send('400');
42+
}
43+
return createGist(req.body.content, req.body.state)
44+
.then((body) => res.status(200).json(body));
45+
};

server/now.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "engine262-api",
3+
"version": 2,
4+
"routes": [
5+
{ "src": "/api/gist", "dest": "/api/gist.js" },
6+
{
7+
"src": ".*",
8+
"status": 302,
9+
"headers": { "Location": "https://engine262.js.org" }
10+
}
11+
],
12+
"env": {
13+
"GH_TOKEN": "@gh_token"
14+
}
15+
}

server/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
"name": "server",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "index.js",
6-
"scripts": {
7-
"start": "node index.js"
8-
},
95
"author": "",
106
"license": "MIT",
117
"dependencies": {

0 commit comments

Comments
 (0)