118 lines
3.7 KiB
JavaScript
118 lines
3.7 KiB
JavaScript
import path from 'path';
|
|
import fs from 'fs/promises'
|
|
import { fileURLToPath } from 'url';
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
import express from 'express';
|
|
import cors from 'cors'
|
|
import http from 'http'
|
|
import chalk from 'chalk'
|
|
import moment from 'moment'
|
|
|
|
import { initWebSocket } from './ws.js'
|
|
import Database from "./db/db.js"
|
|
import getAllDownloadsFiles from "./db/getDownloads.js"
|
|
|
|
class Server {
|
|
db;
|
|
UIPath = path.join(__dirname, '../ui')
|
|
|
|
registerRoutes(router) {
|
|
router.get('/*', this.get)
|
|
return router
|
|
}
|
|
|
|
index = async (req, res) => {
|
|
let filePath = path.join(this.UIPath, "index.html");
|
|
res.sendFile(filePath)
|
|
return
|
|
|
|
let html = await fs.readFile(filePath, 'utf-8');
|
|
|
|
let downloads = await getAllDownloadsFiles()
|
|
const jsonString = JSON.stringify(downloads, null, 2); // pretty-print optional
|
|
const scriptTag = `<script type="application/json" id="jsonData">\n${jsonString}\n</script>`;
|
|
|
|
const headEndRegex = /<\/head\s*>/i;
|
|
|
|
const match = html.match(headEndRegex);
|
|
const insertPos = match.index;
|
|
let result = html.slice(0, insertPos) + scriptTag + '\n' + html.slice(insertPos);
|
|
res.type('html').send(result);
|
|
}
|
|
|
|
get = async (req, res) => {
|
|
let url = req.url
|
|
let filePath;
|
|
if(url.startsWith("/_")) {
|
|
filePath = path.join(this.UIPath, url);
|
|
} else if(url.includes("75820185")) {
|
|
filePath = path.join(this.UIPath, url.split("75820185")[1]);
|
|
} else {
|
|
filePath = path.join(this.UIPath, "index.html");
|
|
}
|
|
|
|
res.sendFile(filePath);
|
|
}
|
|
|
|
logRequest(req, res, next) {
|
|
const formattedDate = moment().format('M.D');
|
|
const formattedTime = moment().format('h:mma');
|
|
if(req.url.includes("/api/")) {
|
|
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
|
} else {
|
|
if(req.url === "/")
|
|
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
|
}
|
|
next();
|
|
}
|
|
|
|
logResponse(req, res, next) {
|
|
const originalSend = res.send;
|
|
res.send = function (body) {
|
|
if(res.statusCode >= 400) {
|
|
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
|
|
} else {
|
|
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
|
|
}
|
|
originalSend.call(this, body);
|
|
};
|
|
next();
|
|
}
|
|
|
|
constructor() {
|
|
this.db = new Database()
|
|
const app = express();
|
|
app.use(cors({ origin: '*' }));
|
|
app.use(express.json());
|
|
|
|
app.use(this.logRequest);
|
|
app.use(this.logResponse);
|
|
|
|
let router = express.Router();
|
|
this.registerRoutes(router)
|
|
app.use('/', router);
|
|
|
|
const server = http.createServer(app);
|
|
initWebSocket(server);
|
|
const PORT = 80;
|
|
server.listen(PORT, () => {
|
|
console.log("\n")
|
|
console.log(chalk.yellow("**************Downloads****************"))
|
|
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
|
console.log(chalk.yellow("***************************************"))
|
|
console.log("\n")
|
|
});
|
|
|
|
process.on('SIGINT', async () => {
|
|
console.log(chalk.red('Closing server...'));
|
|
console.log(chalk.green('Database connection closed.'));
|
|
process.exit(0);
|
|
});
|
|
|
|
Object.preventExtensions(this);
|
|
}
|
|
}
|
|
|
|
const server = new Server() |