'use strict'; const express = require("express"); // const expressWs = require("express-ws"); const knex = require("knex"); const path = require("path"); const bodyParser = require("body-parser"); function projectPath(targetPath) { return path.join(__dirname, "..", targetPath); } module.exports = function () { let db = knex(require("../knexfile")); let imageStore = require("./image-store")(projectPath("./images")); let taskTracker = require("../lib/tasks/tracker")(); let state = {db, imageStore, taskTracker}; let app = express(); // expressWs(app); app.set("view engine", "pug"); app.set("views", projectPath("views")); app.use((req, res, next) => { res.locals.isUnderPrefix = function isUnderPrefix(path, resultingClass) { // FIXME: Proper path segment parsing... if (req.originalUrl.indexOf(path) === 0) { return resultingClass; } else { return ""; } } next(); }); app.use(express.static(projectPath("public"))); app.use(bodyParser.urlencoded({ extended: true })); app.use(require("./routes/index")); app.use("/disk-images", require("./routes/disk-images")(state)); app.use("/instances", require("./routes/instances")(state)); app.use("/hardware/storage-devices", require("./routes/storage-devices")(state)); app.use((err, req, res, next) => { if (err.showChain != null) { console.log(err.showChain()); console.log("#####################"); console.log(err.getAllContext()); } else { console.log(err.stack); } res.render("error", { error: err }); }); return app; };