forked from hull-ships/hull-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute-handler.js
More file actions
58 lines (51 loc) · 1.72 KB
/
compute-handler.js
File metadata and controls
58 lines (51 loc) · 1.72 KB
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
import connect from "connect";
import compute from "../compute";
import bodyParser from "body-parser";
import timeout from "connect-timeout";
import fetchUser from "../middlewares/fetch-user";
function computeHandler(req, res) {
const { client, timings } = req.hull;
let { ship = {}, user } = req.body;
// This condition ensures boot request does work:
// When loading the page, the ship is client-side so what's passed to remote
// doesn't have private_settings embedded
ship = (ship.private_settings) ? ship : req.hull.ship;
user = user || req.hull.user;
res.type("application/json");
if (client && ship && user) {
const startTime = new Date();
compute(user, ship, { preview: true })
.then(result => {
const logs = result.logs;
if (logs && logs.length) {
logs.map(line => req.hull.client.logger.debug("preview.console.log", line));
}
const took = new Date() - startTime;
timings.compute = took;
res.send({ ship, user, took, timings, result })
.end();
}).catch(error => res.status(500).json({ error }));
} else {
res
.status(400)
.json({ reason: "missing_params", message: "Missing Params" });
}
}
function haltOnTimedout(req, res, next) {
if (!req.timedout) next();
}
export default function ComputeHandler(options) {
const app = connect();
const { connector, hostSecret = "" } = options;
app.use(timeout("28s"));
app.use(bodyParser.json());
app.use(haltOnTimedout);
app.use(connector.clientMiddleware({ hostSecret, fetchShip: true, cacheShip: false }));
app.use(fetchUser);
app.use(haltOnTimedout);
app.use(computeHandler);
app.use(haltOnTimedout);
return function c(req, res) {
return app.handle(req, res);
};
}