Fix express and session for production
This commit is contained in:
parent
9e8404db11
commit
13f37a14e8
3
.gitignore
vendored
3
.gitignore
vendored
@ -66,3 +66,6 @@ typings/
|
||||
|
||||
data/*
|
||||
!/data/.gitkeep
|
||||
|
||||
push.sh
|
||||
.rsyncignore
|
12
app.js
12
app.js
@ -2,6 +2,8 @@ require('dotenv').config();
|
||||
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');
|
||||
@ -35,6 +37,7 @@ const sess = {
|
||||
if (app.get('env') === 'production') {
|
||||
app.set('trust proxy', 1); // trust first proxy
|
||||
sess.cookie.secure = true; // serve secure cookies
|
||||
sess.store = new RedisStore({ client: redisClient });
|
||||
}
|
||||
|
||||
app.use(session(sess));
|
||||
@ -67,15 +70,6 @@ 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(function (req, res, next) {
|
||||
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
|
||||
res.header(
|
||||
'Access-Control-Allow-Headers',
|
||||
'Origin, X-Requested-With, Content-Type, Accept'
|
||||
);
|
||||
next();
|
||||
});
|
||||
|
||||
app.use('/api/0', apiRouter);
|
||||
|
||||
const csrfProtection = csrf({ cookie: true });
|
||||
|
@ -32,24 +32,35 @@ function quizz(req, res) {
|
||||
}
|
||||
|
||||
function check(req, res) {
|
||||
const answer = req.query.species;
|
||||
const correctAnswer = req.session.answer;
|
||||
let answer, correct;
|
||||
try {
|
||||
answer = req.query.species;
|
||||
correct = req.session.answer;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
let result = {};
|
||||
if (answer === correctAnswer.speciesCode) {
|
||||
try {
|
||||
if (correct === undefined) {
|
||||
console.error("No answer found in session");
|
||||
} else if (answer === correct.speciesCode) {
|
||||
debug("Correct answer");
|
||||
result = {
|
||||
correct: true,
|
||||
message: req.i18n.__('Correct!'),
|
||||
answer: correctAnswer
|
||||
answer: correct
|
||||
};
|
||||
} else {
|
||||
debug("Wrong answer");
|
||||
result = {
|
||||
correct: false,
|
||||
message: req.i18n.__('Wrong!'),
|
||||
answer: correctAnswer
|
||||
answer: correct
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
res.json(result);
|
||||
}
|
||||
|
||||
|
@ -1,16 +1,5 @@
|
||||
require('dotenv').config();
|
||||
const { redisClient } = require('../redis');
|
||||
const debug = require('debug')('soundbirder:cache');
|
||||
const redis = require('redis');
|
||||
const host = process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost';
|
||||
const port = process.env.REDIS_PORT ? process.env.REDIS_PORT : 6379;
|
||||
const url = `redis://${host}:${port}`;
|
||||
const redisClient = redis.createClient({
|
||||
url
|
||||
});
|
||||
|
||||
(async () => {
|
||||
redisClient.connect();
|
||||
})();0
|
||||
|
||||
function cacheResponse(request, response) {
|
||||
debug("Caching response", request);
|
||||
|
@ -21,11 +21,11 @@ async function generateQuizz(coordinates, locale, size) {
|
||||
answer = choice(speciesSelectionLocalized);
|
||||
quizz.answer = answer;
|
||||
quizz.audio = await getAudio(answer.sciName);
|
||||
if (quizz.audio == undefined) {
|
||||
if (quizz.audio === undefined) {
|
||||
debug("No audio found for species", answer.sciName);
|
||||
debug("Trying again...");
|
||||
}
|
||||
} while (quizz.audio == undefined);
|
||||
} while (quizz.audio === undefined);
|
||||
debug("Got answer", answer);
|
||||
debug("Got audio", quizz.audio);
|
||||
} catch (error) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: '3.9'
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
express:
|
||||
@ -10,8 +10,10 @@ services:
|
||||
- NODE_ENV=production
|
||||
- REDIS_HOST=${REDIS_HOST:-soundbirder_redis}
|
||||
- REDIS_PORT=${REDIS_PORT:-6379}
|
||||
- DEBUG=${DEBUG:-""}
|
||||
ports:
|
||||
- "${EXPRESS_PORT:-3000}:3000"
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- soundbirder_network
|
||||
depends_on:
|
||||
|
@ -2,12 +2,12 @@ FROM node:16.17.0
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
COPY package*.json .
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
RUN npm ci --only=production
|
||||
|
||||
COPY . .
|
||||
COPY . ./
|
||||
|
||||
EXPOSE 3000
|
||||
CMD [ "./bin/www" ]
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
"Game": "Game",
|
||||
"Game": "Spiel",
|
||||
"About": "About"
|
||||
}
|
14
package-lock.json
generated
14
package-lock.json
generated
@ -11,6 +11,7 @@
|
||||
"@unclesamulus/ebird-api": "^0.0.0",
|
||||
"@unclesamulus/xeno-canto-api": "^0.0.0",
|
||||
"axios": "^0.27.2",
|
||||
"connect-redis": "^6.1.3",
|
||||
"cookie-parser": "~1.4.4",
|
||||
"csurf": "^1.11.0",
|
||||
"debug": "~2.6.9",
|
||||
@ -282,6 +283,14 @@
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/connect-redis": {
|
||||
"version": "6.1.3",
|
||||
"resolved": "https://registry.npmjs.org/connect-redis/-/connect-redis-6.1.3.tgz",
|
||||
"integrity": "sha512-aaNluLlAn/3JPxRwdzw7lhvEoU6Enb+d83xnokUNhC9dktqBoawKWL+WuxinxvBLTz6q9vReTnUDnUslaz74aw==",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/constantinople": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-4.0.1.tgz",
|
||||
@ -1545,6 +1554,11 @@
|
||||
"delayed-stream": "~1.0.0"
|
||||
}
|
||||
},
|
||||
"connect-redis": {
|
||||
"version": "6.1.3",
|
||||
"resolved": "https://registry.npmjs.org/connect-redis/-/connect-redis-6.1.3.tgz",
|
||||
"integrity": "sha512-aaNluLlAn/3JPxRwdzw7lhvEoU6Enb+d83xnokUNhC9dktqBoawKWL+WuxinxvBLTz6q9vReTnUDnUslaz74aw=="
|
||||
},
|
||||
"constantinople": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/constantinople/-/constantinople-4.0.1.tgz",
|
||||
|
@ -9,6 +9,7 @@
|
||||
"@unclesamulus/ebird-api": "^0.0.0",
|
||||
"@unclesamulus/xeno-canto-api": "^0.0.0",
|
||||
"axios": "^0.27.2",
|
||||
"connect-redis": "^6.1.3",
|
||||
"cookie-parser": "~1.4.4",
|
||||
"csurf": "^1.11.0",
|
||||
"debug": "~2.6.9",
|
||||
|
17
redis.js
Normal file
17
redis.js
Normal file
@ -0,0 +1,17 @@
|
||||
const redis = require('redis');
|
||||
|
||||
const redisHost = process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost';
|
||||
const redisPort = process.env.REDIS_PORT ? process.env.REDIS_PORT : 6379;
|
||||
const url = `redis://${redisHost}:${redisPort}`;
|
||||
const redisClient = redis.createClient({
|
||||
url,
|
||||
legacyMode: true
|
||||
});
|
||||
|
||||
(async () => {
|
||||
redisClient.connect()
|
||||
})();
|
||||
|
||||
module.exports = {
|
||||
redisClient
|
||||
}
|
Loading…
Reference in New Issue
Block a user