-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
58 lines (45 loc) · 2.43 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
(async () => {
const launchMillis = Date.now();
const express = require("express");
const app = express();
const serveStatic = require("serve-static");
const bodyParser = require("body-parser");
const config = require("./config");
const autoUpdater = require("./autoUpdater");
console.log(`Starting PasteServer v${autoUpdater.currentVersion}...`);
// update-check
const updateAvailable = await autoUpdater.checkForUpdates();
if (updateAvailable && config.autoUpdate.enabled) {
if (await autoUpdater.downloadUpdate())
await autoUpdater.installUpdate();
}
// connecting to the given database
const database = config.storage.type;
console.log(`Trying to use database '${database}'...`);
const documentStorage = database === "file" ? require("./storage/fileStorage") :
database === "arangodb" ? require("./storage/arangoStorage") : require("./storage/redisStorage");
if (!documentStorage) {
console.log(`There is no support for '${database}'!`);
process.exit();
}
await documentStorage.prepare(config.storage);
// bodyParser to handle requests in json-format
const jsonParser = bodyParser.json({limit: config.document.dataLimit, extended: true});
app.use((request, response, next) =>
request.path.toLowerCase() === "/documents" && request.method === "POST" ? next() : jsonParser(request, response, next));
// setting route for the rest api
app.use("/documents", (require("./routes/documents")(documentStorage)));
// sending the static files on the root and when the url contains a key
app.use(serveStatic(__dirname + "/static"));
app.use("/:key", serveStatic(__dirname + "/static"));
// else, redirecting to the root
app.use((request, response) => response.redirect("/"));
console.log(`Trying to bind on port ${config.server.port}...`);
app.listen(config.server.port, console.log(`Now listening on port ${config.server.port}.`));
// commands
const {CommandProvider, defaultCommand} = require("./commands/commands");
const commandProvider = new CommandProvider(defaultCommand);
commandProvider.registerCommands((require("./commands/documentCommands")(documentStorage)));
commandProvider.registerCommands((require("./commands/updateCommands")(autoUpdater)));
console.log(`Done (${Date.now() - launchMillis}ms). Execute '${defaultCommand.name}' for a list of all commands.`)
})();