-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.js
117 lines (104 loc) · 3.29 KB
/
api-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
111
112
113
114
115
116
117
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const helmet = require("helmet");
const bodyParser = require("body-parser");
const { expressjwt: jwt } = require("express-jwt");
const jwksRsa = require("jwks-rsa");
const authConfig = require("./src/auth_config.json");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const axios = require("axios");
const roomRoutes = require("./routes/room");
const app = express();
dotenv.config();
const port = process.env.API_PORT || 3001;
const appPort = process.env.SERVER_PORT || 3000;
const appOrigin = authConfig.appOrigin || `http://localhost:${appPort}`;
const XRapidAPIKey = process.env.XRAPIKEY;
const XRapidAPIHost = process.env.XRAPIHOST;
try {
mongoose.connect(process.env.MONGODB_CONNECTION, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected to MongoDB");
} catch (error) {
console.log("Could not connect to MongoDB");
}
if (
!authConfig.domain ||
!authConfig.audience ||
authConfig.audience === "YOUR_API_IDENTIFIER"
) {
console.log(
"Exiting: Please make sure that auth_config.json is in place and populated with valid domain and audience values"
);
process.exit();
}
app.use(bodyParser.json());
app.use(morgan("dev"));
app.use(helmet());
app.use(cors({ origin: appOrigin }));
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${authConfig.domain}/.well-known/jwks.json`,
}),
audience: authConfig.audience,
issuer: `https://${authConfig.domain}/`,
algorithms: ["RS256"],
});
app.get("/api/external", checkJwt, (req, res) => {
res.send({
msg: "Your access token was successfully validated!",
});
});
app.use("/api/rooms", roomRoutes);
app.post("/api/execute", checkJwt, (req, res) => {
const value = Buffer.from(req.body.code, "utf-8");
const valueB64 = value.toString("base64");
const inputOptions = {
method: "POST",
url: "https://judge0-ce.p.rapidapi.com/submissions/",
params: { base64_encoded: "true", fields: "*", wait: "true" },
headers: {
"content-type": "application/json",
"Content-Type": "application/json",
"X-RapidAPI-Key": XRapidAPIKey,
"X-RapidAPI-Host": XRapidAPIHost,
},
data: `{"language_id":63,"source_code":"${valueB64}","stdin":""}`,
};
axios
.request(inputOptions)
.then(function (response) {
const results = response.data;
console.log(results.token);
const outputOptions = {
method: "GET",
url: `https://judge0-ce.p.rapidapi.com/submissions/${results.token}`,
params: { base64_encoded: "true", fields: "*" },
headers: {
"X-RapidAPI-Key": XRapidAPIKey,
"X-RapidAPI-Host": XRapidAPIHost,
},
};
axios
.request(outputOptions)
.then(function (response) {
const resultsB64 = Buffer.from(response.data.stdout, "base64");
const results = resultsB64.toString("utf-8");
res.send({ results });
})
.catch(function (error) {
console.error(error);
});
})
.catch(function (error) {
console.error(error);
});
});
app.listen(port, () => console.log(`API Server listening on port ${port}`));