aboutsummaryrefslogtreecommitdiff
path: root/src/api/structures/Server.js
diff options
context:
space:
mode:
authorKana <[email protected]>2020-12-24 21:41:24 +0900
committerGitHub <[email protected]>2020-12-24 21:41:24 +0900
commit2412a60bd4cb2364a477a3af79a8c6dcb6b0ddab (patch)
treedbf2b2cad342f31849a62089dedd40165758af86 /src/api/structures/Server.js
parentEnable deleting files with the API key (diff)
parentbug: fix showlist resetting itself every time the page is changed (diff)
downloadhost.fuwn.me-2412a60bd4cb2364a477a3af79a8c6dcb6b0ddab.tar.xz
host.fuwn.me-2412a60bd4cb2364a477a3af79a8c6dcb6b0ddab.zip
Merge pull request #228 from Zephyrrus/begone_trailing_commas
Merge own dev branch into main dev branch
Diffstat (limited to 'src/api/structures/Server.js')
-rw-r--r--src/api/structures/Server.js49
1 files changed, 39 insertions, 10 deletions
diff --git a/src/api/structures/Server.js b/src/api/structures/Server.js
index 2039ed5..83b2880 100644
--- a/src/api/structures/Server.js
+++ b/src/api/structures/Server.js
@@ -1,6 +1,5 @@
require('dotenv').config();
-const log = require('../utils/Log');
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
@@ -8,7 +7,11 @@ const RateLimit = require('express-rate-limit');
const bodyParser = require('body-parser');
const jetpack = require('fs-jetpack');
const path = require('path');
+const morgan = require('morgan');
+const log = require('../utils/Log');
+const ThumbUtil = require('../utils/ThumbUtil');
+// eslint-disable-next-line no-unused-vars
const rateLimiter = new RateLimit({
windowMs: parseInt(process.env.RATE_LIMIT_WINDOW, 10),
max: parseInt(process.env.RATE_LIMIT_MAX, 10),
@@ -32,22 +35,48 @@ class Server {
});
this.server.use(bodyParser.urlencoded({ extended: true }));
this.server.use(bodyParser.json());
+ if (process.env.NODE_ENV !== 'production') {
+ this.server.use(morgan('combined', {
+ skip(req) {
+ let ext = req.path.split('.').pop();
+ if (ext) { ext = `.${ext.toLowerCase()}`; }
+
+ if (
+ ThumbUtil.imageExtensions.indexOf(ext) > -1
+ || ThumbUtil.videoExtensions.indexOf(ext) > -1
+ || req.path.indexOf('_nuxt') > -1
+ || req.path.indexOf('favicon.ico') > -1
+ ) {
+ return true;
+ }
+ return false;
+ },
+ 'stream': {
+ write(str) { log.debug(str); }
+ }
+ }));
+ }
// this.server.use(rateLimiter);
// Serve the uploads
- this.server.use(express.static(path.join(__dirname, '..', '..', '..', 'uploads')));
- this.routesFolder = path.join(__dirname, '..', 'routes');
+ this.server.use(express.static(path.join(__dirname, '../../../uploads')));
+ this.routesFolder = path.join(__dirname, '../routes');
}
registerAllTheRoutes() {
- jetpack.find(this.routesFolder, { matching: '*.js' }).forEach(routeFile => {
- const RouteClass = require(path.join('..', '..', '..', routeFile));
+ jetpack.find(this.routesFolder, { matching: '*.js' }).forEach((routeFile) => {
+ // eslint-disable-next-line import/no-dynamic-require, global-require
+ const RouteClass = require(path.join('../../../', routeFile));
let routes = [RouteClass];
if (Array.isArray(RouteClass)) routes = RouteClass;
for (const File of routes) {
- const route = new File();
- this.server[route.method](process.env.ROUTE_PREFIX + route.path, route.authorize.bind(route));
- log.info(`Found route ${route.method.toUpperCase()} ${process.env.ROUTE_PREFIX}${route.path}`);
+ try {
+ const route = new File();
+ this.server[route.method](process.env.ROUTE_PREFIX + route.path, route.authorize.bind(route));
+ log.info(`Found route ${route.method.toUpperCase()} ${process.env.ROUTE_PREFIX}${route.path}`);
+ } catch (e) {
+ log.error(`Failed loading route from file ${routeFile} with error: ${e.message}`);
+ }
}
});
}
@@ -55,7 +84,7 @@ class Server {
serveNuxt() {
// Serve the frontend if we are in production mode
if (process.env.NODE_ENV === 'production') {
- this.server.use(express.static(path.join(__dirname, '..', '..', '..', 'dist')));
+ this.server.use(express.static(path.join(__dirname, '../../../dist')));
}
/*
@@ -66,7 +95,7 @@ class Server {
*/
this.server.all('*', (_req, res) => {
try {
- res.sendFile(path.join(__dirname, '..', '..', '..', 'dist', 'index.html'));
+ res.sendFile(path.join(__dirname, '../../../dist/index.html'));
} catch (error) {
res.json({ success: false, message: 'Something went wrong' });
}