forked from austintgriffith/speedrun-grader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (81 loc) · 2.59 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
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
const express = require("express");
const fs = require("fs");
const https = require("https");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const { downloadAndTestContract } = require("./utils/contract");
const { MESSAGES } = require("./utils/messages");
let challenges = JSON.parse(fs.readFileSync("challenges.json").toString());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", async function (req, res) {
res.status(200).send("HELLO WORLD!");
});
app.get("/address", async function (req, res) {
const { stdout, stderr } = await exec(
"cd " + challenges[0].name + " && yarn account"
);
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
res.status(200).send(stdout);
});
// For testing purposes.
app.get("/:challengeId/:network/:address", async function (req, res) {
console.log("GET /:challengeId/:network/:address", req.params);
const challengeId = req.params.challengeId;
const network = req.params.network;
const address = req.params.address;
let result;
try {
result = await downloadAndTestContract(challengeId, network, address);
} catch (e) {
return res.status(200).send(`
<html>
<body>
<p>Can't get the contract from ${network} in ${address}.</p>
<p>${e.message}</p>${MESSAGES.telegramHelp(challenges[challengeId])}
</body>
</html>
`);
}
return res.status(200).send(`<html><body>${result.feedback}</body></html>`);
});
// Main API endpoint.
app.post("/", async function (req, res) {
console.log("⏩ POST", req.body);
const challengeId = req.body.challenge;
const network = req.body.network;
const address = req.body.address;
let result;
try {
result = await downloadAndTestContract(challengeId, network, address);
} catch (e) {
return res.status(404).json({
error: `<p>Can't get the contract from ${network} in ${address}.</p><p>${
e.message
}</p>${MESSAGES.telegramHelp(challenges[challengeId])}`,
});
}
return res.json(result);
});
if (fs.existsSync("server.key") && fs.existsSync("server.cert")) {
https
.createServer(
{
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.cert"),
},
app
)
.listen(54727, () => {
console.log("HTTPS Listening: 54727");
});
} else {
var server = app.listen(54727, function () {
console.log("HTTP Listening on port:", server.address().port);
});
}