From 13f37a14e83b0b1f80ecc3aedba8eec6fe1490f9 Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Mon, 29 Aug 2022 14:05:34 +0200 Subject: [PATCH] Fix express and session for production --- .gitignore | 5 ++++- app.js | 12 +++-------- controllers/api.js | 43 ++++++++++++++++++++++++--------------- controllers/cache.js | 13 +----------- controllers/quizz.js | 4 ++-- docker-compose.yml | 4 +++- docker/express/Dockerfile | 4 ++-- locales/de.json | 2 +- package-lock.json | 14 +++++++++++++ package.json | 1 + redis.js | 17 ++++++++++++++++ 11 files changed, 75 insertions(+), 44 deletions(-) create mode 100644 redis.js diff --git a/.gitignore b/.gitignore index 939490e..cf4569f 100644 --- a/.gitignore +++ b/.gitignore @@ -65,4 +65,7 @@ typings/ .ideas data/* -!/data/.gitkeep \ No newline at end of file +!/data/.gitkeep + +push.sh +.rsyncignore \ No newline at end of file diff --git a/app.js b/app.js index 8ed5c7a..d5c8422 100644 --- a/app.js +++ b/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 }); diff --git a/controllers/api.js b/controllers/api.js index ce6c0b0..9b60172 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -32,23 +32,34 @@ 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) { - debug("Correct answer"); - result = { - correct: true, - message: req.i18n.__('Correct!'), - answer: correctAnswer - }; - } else { - debug("Wrong answer"); - result = { - correct: false, - message: req.i18n.__('Wrong!'), - answer: correctAnswer - }; + 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: correct + }; + } else { + debug("Wrong answer"); + result = { + correct: false, + message: req.i18n.__('Wrong!'), + answer: correct + }; + } + } catch (error) { + console.error(error); } res.json(result); } diff --git a/controllers/cache.js b/controllers/cache.js index 02044d3..9bef8bc 100644 --- a/controllers/cache.js +++ b/controllers/cache.js @@ -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); diff --git a/controllers/quizz.js b/controllers/quizz.js index 54bc34b..ff00003 100644 --- a/controllers/quizz.js +++ b/controllers/quizz.js @@ -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) { diff --git a/docker-compose.yml b/docker-compose.yml index a752226..1dc2e9d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/docker/express/Dockerfile b/docker/express/Dockerfile index 0246193..dee876f 100644 --- a/docker/express/Dockerfile +++ b/docker/express/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/locales/de.json b/locales/de.json index c5cfbd9..44781af 100644 --- a/locales/de.json +++ b/locales/de.json @@ -1,4 +1,4 @@ { - "Game": "Game", + "Game": "Spiel", "About": "About" } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 31ab81c..e61150a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 1d55bf7..da30fb8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/redis.js b/redis.js new file mode 100644 index 0000000..b1f142d --- /dev/null +++ b/redis.js @@ -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 +} \ No newline at end of file