Rename web-server to src/ and change start script accordingly

This commit is contained in:
Jip Fr
2021-04-09 15:11:59 +02:00
parent 754b9be789
commit 8f042e81ae
7 changed files with 4 additions and 6 deletions

54
src/index.js Normal file
View File

@@ -0,0 +1,54 @@
const logger = require('./logger');
const express = require('express');
const handlebars = require('express-handlebars');
const cfg = require('../config.json');
const app = express();
const routers = {
home: require('./routers/home'),
faq: require('./routers/faq')
};
app.use('*', (req, res, next) => {
logger.info(`${req.method.toUpperCase()} ${req.path}`);
next();
});
app.use('/', routers.home);
app.use('/faq', routers.faq);
app.engine('handlebars', handlebars({
helpers: {
doFaq(value, options) {
let htmlLeft = '';
let htmlRight = '';
for(const [i, v] of Object.entries(value)) {
const appendHtml = options.fn({
...v
}); // Tis is an HTML string
if(i % 2 === 0) {
htmlLeft += appendHtml;
} else {
htmlRight += appendHtml;
}
}
return `
<div class="left questions-left">
${htmlLeft}
</div>
<div class="right questions-right">
${htmlRight}
</div>
`;
}
}
}));
app.set('view engine', 'handlebars');
app.use(express.static('public'));
app.listen(cfg.http.port, () => {
logger.info(`Server listening on *:${cfg.http.port}`);
});

52
src/logger.js Normal file
View File

@@ -0,0 +1,52 @@
const fs = require('fs-extra');
require('colors');
const root = __dirname;
fs.ensureDirSync(`${root}/logs`);
const streams = {
latest: fs.createWriteStream(`${root}/logs/latest.log`),
success: fs.createWriteStream(`${root}/logs/success.log`),
error: fs.createWriteStream(`${root}/logs/error.log`),
warn: fs.createWriteStream(`${root}/logs/warn.log`),
info: fs.createWriteStream(`${root}/logs/info.log`)
};
function success(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [SUCCESS]: ${input}`;
streams.success.write(`${input}\n`);
console.log(`${input}`.green.bold);
}
function error(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [ERROR]: ${input}`;
streams.error.write(`${input}\n`);
console.log(`${input}`.red.bold);
}
function warn(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [WARN]: ${input}`;
streams.warn.write(`${input}\n`);
console.log(`${input}`.yellow.bold);
}
function info(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [INFO]: ${input}`;
streams.info.write(`${input}\n`);
console.log(`${input}`.cyan.bold);
}
module.exports = {
success,
error,
warn,
info
};

8
src/routers/faq.js Normal file
View File

@@ -0,0 +1,8 @@
const { Router } = require('express');
const router = new Router();
router.get('/', (_req, res) => {
res.redirect('/#faq');
});
module.exports = router;

16
src/routers/home.js Normal file
View File

@@ -0,0 +1,16 @@
const getLocale = require('../../util/getLocale');
const { Router } = require('express');
const router = new Router();
const featureLists = require('../../feature-lists.json');
router.get('/', (req, res) => {
const tmpLocale = getLocale('US', 'en');
res.render('home', {
layout: 'main',
locale: tmpLocale,
featuredFeatureList: featureLists[0]
});
});
module.exports = router;