111 lines
2.7 KiB
JavaScript
111 lines
2.7 KiB
JavaScript
"use strict";
|
|
|
|
console.log("Loading photos.ketr");
|
|
|
|
const express = require("express"),
|
|
morgan = require("morgan"),
|
|
bodyParser = require("body-parser"),
|
|
config = require("config"),
|
|
scanner = require("./scanner"),
|
|
db = require("./db");
|
|
|
|
require("./console-line.js"); /* Monkey-patch console.log with line numbers */
|
|
|
|
const picturesPath = config.get("picturesPath"),
|
|
serverConfig = config.get("server");
|
|
|
|
let basePath = config.get("basePath");
|
|
|
|
basePath = "/" + basePath.replace(/^\/+/, "").replace(/\/+$/, "") + "/";
|
|
|
|
console.log("Loading pictures out of: " + picturesPath);
|
|
console.log("Hosting server from: " + basePath);
|
|
|
|
const app = express();
|
|
|
|
app.set("basePath", basePath);
|
|
|
|
/* App is behind an nginx proxy which we trust, so use the remote address
|
|
* set in the headers */
|
|
app.set("trust proxy", true);
|
|
|
|
/* Handle static files first so excessive logging doesn't occur */
|
|
app.use(basePath, express.static("frontend", { index: false }));
|
|
|
|
app.use(basePath, express.static(".", { index: false }));
|
|
|
|
app.use(morgan("common"));
|
|
|
|
app.use(bodyParser.urlencoded({
|
|
extended: false
|
|
}));
|
|
|
|
/* body-parser does not support text/*, so add support for that here */
|
|
app.use(function(req, res, next){
|
|
if (!req.is('text/*')) {
|
|
return next();
|
|
}
|
|
req.setEncoding('utf8');
|
|
let text = '';
|
|
req.on('data', function(chunk) {
|
|
text += chunk;
|
|
});
|
|
req.on('end', function() {
|
|
req.text = text;
|
|
next();
|
|
});
|
|
});
|
|
|
|
app.use(basePath + "api/v1/photos", require("./routes/photos"));
|
|
|
|
/* Declare the "catch all" index route last; the final route is a 404 dynamic router */
|
|
app.use(basePath, require("./routes/index"));
|
|
|
|
app.use(function(err, req, res, next) {
|
|
res.status(err.status || 500).json({
|
|
message: err.message,
|
|
error: {}
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Create HTTP server and listen for new connections
|
|
*/
|
|
app.set("port", serverConfig.port);
|
|
|
|
const server = require("http").createServer(app);
|
|
|
|
Promise.all([
|
|
db, scanner.then(function(files) { app.set("files", files); })
|
|
]).then(function() {
|
|
console.log("Done scanning. Opening server.");
|
|
server.listen(serverConfig.port);
|
|
}).catch(function(error) {
|
|
console.error(error);
|
|
process.exit(-1);
|
|
});
|
|
|
|
server.on("error", function(error) {
|
|
if (error.syscall !== "listen") {
|
|
throw error;
|
|
}
|
|
|
|
// handle specific listen errors with friendly messages
|
|
switch (error.code) {
|
|
case "EACCES":
|
|
console.error(serverConfig.port + " requires elevated privileges");
|
|
process.exit(1);
|
|
break;
|
|
case "EADDRINUSE":
|
|
console.error(serverConfig.port + " is already in use");
|
|
process.exit(1);
|
|
break;
|
|
default:
|
|
throw error;
|
|
}
|
|
});
|
|
|
|
server.on("listening", function() {
|
|
console.log("Listening on " + serverConfig.port);
|
|
});
|