102 lines
2.9 KiB
JavaScript
Executable File
102 lines
2.9 KiB
JavaScript
Executable File
require('dotenv').config();
|
|
|
|
const redisHost = process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost';
|
|
const redisPort = process.env.REDIS_PORT ? process.env.REDIS_PORT : 6379;
|
|
|
|
const createError = require('http-errors');
|
|
const express = require('express');
|
|
const session = require('express-session');
|
|
let { redisClient } = require('./redis');
|
|
let RedisStore = require('connect-redis')(session);
|
|
const csrf = require('csurf');
|
|
const path = require('path');
|
|
const cookieParser = require('cookie-parser');
|
|
const logger = require('morgan');
|
|
const i18n = require('i18n-2');
|
|
const debugLocale = require('debug')('soundbirder:locale');
|
|
|
|
const indexRouter = require('./routes/index');
|
|
const apiRouter = require('./routes/api');
|
|
const authRouter = require('./routes/auth');
|
|
|
|
const app = express();
|
|
|
|
// view engine setup
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.set('view engine', 'pug');
|
|
|
|
app.use(logger('dev'));
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: false }));
|
|
app.use(cookieParser());
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
const sess = {
|
|
secret: 'keyboard cat',
|
|
resave: true,
|
|
saveUninitialized: true,
|
|
cookie: { secure: false }
|
|
}
|
|
|
|
// if (app.get('env') === 'production') {
|
|
// app.set('trust proxy', 1); // trust first proxy
|
|
// sess.cookie.secure = true; // serve secure cookies
|
|
// sess.store = new RedisStore({ host: redisHost, port: redisPort, client: redisClient });
|
|
// }
|
|
|
|
app.use(session(sess));
|
|
|
|
i18n.expressBind(app, {
|
|
locales: ['en', 'es', 'fr', 'de'],
|
|
defaultLocale: 'en',
|
|
cookieName: 'locale',
|
|
extension: '.json'
|
|
});
|
|
|
|
app.use(function (req, res, next) {
|
|
req.i18n.setLocaleFromQuery();
|
|
req.i18n.setLocaleFromCookie();
|
|
// set locale from url prefix
|
|
var rxLocale = /^\/(fr|en)/i;
|
|
if (rxLocale.test(req.url)) {
|
|
const arr = rxLocale.exec(req.url);
|
|
const locale = arr[1];
|
|
debugLocale("Setting locale from url prefix", locale);
|
|
req.i18n.setLocale(locale);
|
|
debugLocale("Locale set to", req.i18n.locale);
|
|
}
|
|
res.cookie('locale', req.i18n.locale, { maxAge: 900000, sameSite: true });
|
|
// add extra logic
|
|
next();
|
|
});
|
|
|
|
app.use('/dist/leaflet', express.static('node_modules/leaflet/dist'));
|
|
app.use('/dist/feather', express.static('node_modules/feather-icons/dist'));
|
|
app.use('/dist/axios', express.static('node_modules/axios/dist'));
|
|
|
|
app.use('/api/', apiRouter);
|
|
|
|
const csrfProtection = csrf({ cookie: true });
|
|
app.use(csrfProtection);
|
|
app.use(/\/(fr|en)/i, indexRouter);
|
|
app.use(/\/(fr|en)\/auth/i, authRouter);
|
|
app.use("/", indexRouter);
|
|
app.use("/auth", authRouter);
|
|
|
|
// catch 404 and forward to error handler
|
|
app.use(function (req, res, next) {
|
|
next(createError(404));
|
|
});
|
|
|
|
// error handler
|
|
app.use(function (err, req, res, next) {
|
|
// set locals, only providing error in development
|
|
res.locals.message = err.message;
|
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
|
|
|
// render the error page
|
|
res.status(err.status || 500);
|
|
res.render('error');
|
|
});
|
|
|
|
module.exports = app; |