use redis for sessions

This commit is contained in:
b1ek 2023-02-19 12:25:01 +10:00
parent f6176f74cc
commit cda3d85748
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 46 additions and 3 deletions

View File

@ -1,4 +1,13 @@
APP_PORT=8000 APP_PORT=8000
APP_DEBUG=true APP_DEBUG=true
# a 32-bit base64 encryption key # 32-byte base64 string
APP_KEY=TKe8lE2IdkgGBUrB4nxdq7mGMf8PK29xqOnGa3vU0PBmNXADJrVA5LKd8pg6g/YO5aFG/ESzUleo/9Hve3SAe4rvwLBejD/SKOmDR4gbaMv4PuiNi8S2sYL30aVyi1OeaSTyYsfjteumkFxFVwrsxhDCX94xvNEuTEfS4repfLo= APP_KEY=
DB_PORT=5432
DB_HOSTNAME=db
DB_PASSWORD=homepage
DB_USERNAME=homepage
DB_DATABASE=homepage
REDIS_HOST=redis
REDIS_PORT=6379

View File

@ -12,3 +12,27 @@ services:
env_file: ./.env env_file: ./.env
environment: environment:
APP_PORT: '${APP_PORT}' APP_PORT: '${APP_PORT}'
networks:
- homepage
redis:
image: redis:alpine
ports:
- '6379:6379'
networks:
- homepage
db:
image: postgres:alpine
ports:
- '5432:5432'
environment:
POSTGRES_PASSWORD: '${DB_PASSWORD}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_DB: '${DB_DATABASE}'
volumes:
- './data/db:/var/lib/postgresql'
networks:
- homepage
networks:
homepage:
driver: bridge

View File

@ -7,6 +7,9 @@ const app = express();
const session = require('express-session'); const session = require('express-session');
const cookie_parse = require('cookie-parser'); const cookie_parse = require('cookie-parser');
const cookie_encrypt = require('cookie-encrypter'); const cookie_encrypt = require('cookie-encrypter');
const Redis = require("ioredis");
let redisClient = new Redis(process.env.REDIS_PORT, process.env.REDIS_HOST);
let RedisStore = require("connect-redis")(session)
const { APP_PORT, APP_KEY } = process.env; const { APP_PORT, APP_KEY } = process.env;
@ -17,8 +20,12 @@ app.use(cookie_parse(APP_KEY))
app.use(cookie_encrypt(APP_KEY)); app.use(cookie_encrypt(APP_KEY));
app.use(session({ app.use(session({
secret: APP_KEY, secret: APP_KEY,
cookie: { secure: true } resave: false,
saveUninitialized: true,
cookie: { secure: true },
store: new RedisStore({ client: redisClient })
})); }));
app.use(require('./middleware'));
const server = app.listen(APP_PORT, () => { const server = app.listen(APP_PORT, () => {
console.log("Listening on port " + APP_PORT); console.log("Listening on port " + APP_PORT);

View File

@ -12,6 +12,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"connect-redis": "^6.1.3",
"cookie-encrypter": "^1.0.1", "cookie-encrypter": "^1.0.1",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"crc-32": "^1.2.2", "crc-32": "^1.2.2",
@ -20,9 +21,11 @@
"express-session": "^1.17.3", "express-session": "^1.17.3",
"glob": "^8.1.0", "glob": "^8.1.0",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"ioredis": "^5.3.1",
"js-base64": "^3.7.5", "js-base64": "^3.7.5",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"pug": "^3.0.2", "pug": "^3.0.2",
"redis": "^4.6.4",
"unit.js": "^2.1.1" "unit.js": "^2.1.1"
} }
} }