soundbirder/app.js

102 lines
2.9 KiB
JavaScript
Raw Normal View History

require('dotenv').config();
2024-01-08 23:02:04 +01:00
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');
2022-08-29 14:05:34 +02:00
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');
2022-08-17 14:27:37 +02:00
const i18n = require('i18n-2');
2022-08-29 10:38:42 +02:00
const debugLocale = require('debug')('soundbirder:locale');
2022-08-17 14:27:37 +02:00
const indexRouter = require('./routes/index');
const apiRouter = require('./routes/api');
const authRouter = require('./routes/auth');
const app = express();
2022-08-17 14:27:37 +02:00
// 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 = {
2022-08-28 09:48:24 +02:00
secret: 'keyboard cat',
resave: true,
saveUninitialized: true,
cookie: { secure: false }
}
2024-01-08 23:02:04 +01:00
// 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));
2022-08-17 14:27:37 +02:00
i18n.expressBind(app, {
locales: ['en', 'es', 'fr', 'de'],
defaultLocale: 'en',
2022-08-29 10:42:31 +02:00
cookieName: 'locale',
extension: '.json'
2022-08-17 14:27:37 +02:00
});
2022-08-28 09:48:24 +02:00
app.use(function (req, res, next) {
2022-08-17 14:27:37 +02:00
req.i18n.setLocaleFromQuery();
req.i18n.setLocaleFromCookie();
// set locale from url prefix
2022-08-28 09:48:24 +02:00
var rxLocale = /^\/(fr|en)/i;
if (rxLocale.test(req.url)) {
const arr = rxLocale.exec(req.url);
const locale = arr[1];
2022-08-29 10:38:42 +02:00
debugLocale("Setting locale from url prefix", locale);
req.i18n.setLocale(locale);
2022-08-29 10:38:42 +02:00
debugLocale("Locale set to", req.i18n.locale);
}
2022-08-29 10:38:42 +02:00
res.cookie('locale', req.i18n.locale, { maxAge: 900000, sameSite: true });
2022-08-28 09:48:24 +02:00
// 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'));
2024-02-04 07:07:37 +01:00
app.use('/api/', apiRouter);
const csrfProtection = csrf({ cookie: true });
app.use(csrfProtection);
2022-08-28 09:48:24 +02:00
app.use(/\/(fr|en)/i, indexRouter);
app.use(/\/(fr|en)\/auth/i, authRouter);
2022-08-29 07:26:21 +02:00
app.use("/", indexRouter);
app.use("/auth", authRouter);
2022-08-17 14:27:37 +02:00
// catch 404 and forward to error handler
2022-08-28 09:48:24 +02:00
app.use(function (req, res, next) {
2022-08-17 14:27:37 +02:00
next(createError(404));
});
// error handler
2022-08-28 09:48:24 +02:00
app.use(function (err, req, res, next) {
2022-08-17 14:27:37 +02:00
// 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;