Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Feature/fastify examples session #26

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions servers/fastify/session/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ Each time you hit the url the response will tell you how many times you requeste
"hello": "route requested 6 times in this session"
}
```
Note the use of **await** when registering **fastify-redis** as we want the redis client available when we give the
fastify session its options.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@ const fastifySession = require('fastify-session')
const fastifyCookie = require('fastify-cookie')
const fs = require('fs')
const isDocker = require('is-docker')
const redis = require('redis')
const RedisStore = require('connect-redis')(fastifySession)
const RedisStore = require('connect-redis')(fastifySession);

const APP_PORT = process.env.PORT || 3000
const REDIS_PORT = 6379
const APP_PORT = 3000

// the docker compose service is called redis
let host = 'localhost'
if (isDocker()) {
host = 'redis'
}

const redisClient = redis.createClient({
host,
port: REDIS_PORT
})

const getCertificates = () => {
const cert = fs.readFileSync(path.join(__dirname, './certificates/selfsigned.crt'), 'utf-8')
const key = fs.readFileSync(path.join(__dirname, './certificates/selfsigned.key'), 'utf-8')
Expand All @@ -41,29 +34,38 @@ const fastify = Fastify({
logger: true
})

fastify.register(fastifyCookie)
const initialization = async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you pass in the fastify app as a parameter, you can export this function and make this easily testable. I would also move the listen outside.

Suggested change
const initialization = async () => {
const initialization = async (app) => {

fastify.register(fastifyCookie)
await fastify.register(require('fastify-redis'), { host })
const { redis } = fastify;
const sessionOptions = {
secret: 'a secret with minimum length of 32 characters',
cookieName: 'example-redis-session',
cookie: {
secure: true,
maxAge: 1000 * 60 * 3
},
store: new RedisStore({ client: redis })
}
fastify.register(fastifySession, sessionOptions)

const sessionOptions = {
secret: 'a secret with minimum length of 32 characters',
cookieName: 'example-redis-session',
cookie: {
secure: true,
maxAge: 1000 * 60 * 3
},
store: new RedisStore({ client: redisClient })
}
fastify.register(fastifySession, sessionOptions)
fastify.get('/', (request, reply) => {
if (request.session) {
request.session.count = request.session.count ? request.session.count + 1 : 1
}
return { hello: `route requested ${request.session.count} times in this session` }
})

fastify.get('/', async (request, reply) => {
if (request.session) {
request.session.count = request.session.count ? request.session.count + 1 : 1
}
return { hello: `route requested ${request.session.count} times in this session` }
})
fastify.listen(APP_PORT, '0.0.0.0', (err) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
}

fastify.listen(APP_PORT, '0.0.0.0', (err) => {
initialization().catch((err) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"connect-redis": "^5.0.0",
"fastify": "^3.2.0",
"fastify-cookie": "^4.0.2",
"fastify-redis": "^4.0.3",
"fastify-session": "^5.0.0",
"is-docker": "^2.1.1",
"redis": "^3.0.2"
"is-docker": "^2.1.1"
}
}