forked from hull-ships/hull-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·101 lines (81 loc) · 2.75 KB
/
server.js
File metadata and controls
executable file
·101 lines (81 loc) · 2.75 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
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
import express from "express";
import bodyParser from "body-parser";
import path from "path";
import ejs from "ejs";
import devMode from "./util/dev-mode";
import SyncAgent from "./sync-agent";
import KueRouter from "./util/kue-router";
module.exports = function server(options = {}) {
const { Hull, hostSecret, queue } = options;
const { Routes } = Hull;
const { Readme, Manifest } = Routes;
const app = express();
if (options.devMode) {
app.use(devMode());
}
app.engine("html", ejs.renderFile);
app.set("views", path.resolve(__dirname, "..", "views"));
app.use(express.static(path.resolve(__dirname, "..", "dist")));
app.use(express.static(path.resolve(__dirname, "..", "assets")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/manifest.json", Manifest(__dirname));
app.get("/", Readme);
app.get("/readme", Readme);
app.use("/kue", KueRouter({ hostSecret, queue }));
app.use(Hull.Middleware({ hostSecret, fetchShip: true, cacheShip: false, requireCredentials: true }));
app.use((req, res, next) => {
req.agent = new SyncAgent({ ...req.hull, queue });
next();
});
function checkConfiguration({ agent }, res, next) {
if (!agent.isConfigured()) {
console.error({ status: "not configured" });
res.status(403).json({ status: "not configured" });
} else {
next();
}
}
app.get("/admin.html", ({ agent }, res) => {
if (agent.isConfigured()) {
const query = agent.getQuery();
res.render("connected.html", {
query,
last_sync_at: null,
...agent.ship.private_settings
});
} else {
res.render("home.html", {});
}
});
app.post("/run", ({ body, agent }, res) => {
const query = body.query || agent.getQuery();
agent
.runQuery(query, { timeout: 20000 })
.then(data => res.json(data))
.catch(({ status, message }) =>
res.status(status || 500).send({ message })
);
});
app.post("/import", checkConfiguration, ({ agent }, res) => {
agent.async("startImport");
res.json({ status: "scheduled" });
});
app.post("/sync", checkConfiguration, ({ agent }, res) => {
const response = { status: "ignored" };
if (agent.isEnabled()) {
response.status = "scheduled";
agent.async("startSync");
}
res.json(response);
});
// Error Handler
app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
if (err) {
const logger = req.hull.client ? req.hull.client.logger : Hull.logger;
logger.error("unhandled error", { message: err.message, status: err.status, method: req.method, url: req.url, params: req.params });
}
return res.status(err.status || 500).send({ message: err.message });
});
return app;
};