Files
database_editor/server.js
T
admin daadd58a5d Fixed db bug
The DB was moved to a different file and was returning a reference of the DB. When uploading a new database the original variable containing the DB in the DB file was not updating. Added setters and getters.
2025-05-17 11:11:07 +03:00

83 lines
2.1 KiB
JavaScript

const express = require("express");
const path = require("path");
const fileUpload = require("express-fileupload");
const basicAuth = require("express-basic-auth");
const fs = require("fs");
const app_conf = require("./app/config/app.conf.js");
const public_routes = require('./app/routes/public_routes');
const private_routes = require('./app/routes/private_routes');
const logger = require("./app/utils/logger.util.js");
const db = require("./app/database/sqlite.database.js");
const app = express();
var shutting_down = false;
/*
* Static middleware - serve css and js files to public
*/
app.use(express.json());
app.use(fileUpload({
limits: { fileSize: 102400 },
useTempFiles: true,
tempFileDir: path.join(__dirname, app_conf.temp_directory)
}));
app.use("/", public_routes);
app.use("/app", basicAuth({
users: {
[app_conf.username]: app_conf.password
},
challenge: true
}), private_routes);
/*
* Initialise ExpressJS
* Using ejs to pass variables from ExpressJS to HTML pages.
*/
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, app_conf.views_directory));
const server = app.listen(app_conf.port, () => {
logger.info("Starting Database Editor.");
db.connect();
fs.rm(path.join(__dirname, app_conf.temp_directory, "/."), { recursive: true }, (error) => {
if (error == null) {
logger.info("Removed temp directory.");
} else if (error.code === "ENOENT") {
logger.info("Temp directory not found.");
} else {
logger.error(error.message);
}
});
logger.info(`Listening to port ${app_conf.port}.`);
})
/*
* Handle Ctrl+C to shutdown gracefully
*/
process.on("SIGINT", () => {
if (shutting_down) {
logger.emerg("Shutting down forcefully.");
process.exit(-1);
} else {
shutdown();
}
});
/*
* This function shuts the server down
*/
function shutdown() {
shutting_down = true;
logger.info("Closing Database connection.");
try {
db.getDB().close();
} catch (error) {
logger.error(error.message);
}
server.close(() => {
logger.info("Shutting down server.");
});
}