-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
56 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,14 @@ | ||
const expressSession = require("express-session"); | ||
const redis = require("redis"); | ||
const MongoStore = require("connect-mongo"); | ||
const RedisStore = require("connect-redis")(expressSession); | ||
const { | ||
redis: redisUrl, | ||
mongo: mongoUrl, | ||
session: sessionSecret, | ||
} = require("../../loadenv"); | ||
const logger = require("../modules/logger"); | ||
|
||
// 환경변수 REDIS_PATH 유무에 따라 session 저장 방식이 변경됩니다. | ||
let sessionStore = null; | ||
if (redisUrl) { | ||
const client = redis.createClient({ | ||
url: redisUrl, | ||
legacyMode: true, | ||
}); | ||
client.connect().catch(logger.error); | ||
sessionStore = new RedisStore({ client }); | ||
} else { | ||
sessionStore = MongoStore.create({ mongoUrl }); | ||
} | ||
const { nodeEnv, session: sessionConfig } = require("../../loadenv"); | ||
const sessionStore = require("../modules/stores/sessionStore"); | ||
|
||
module.exports = expressSession({ | ||
secret: sessionSecret, | ||
secret: sessionConfig.secret, | ||
resave: false, | ||
saveUninitialized: false, | ||
store: sessionStore, | ||
cookie: { | ||
maxAge: sessionConfig.expiry, | ||
secure: nodeEnv === "production", | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const expressSession = require("express-session"); | ||
const redis = require("redis"); | ||
const MongoStore = require("connect-mongo"); | ||
const RedisStore = require("connect-redis")(expressSession); | ||
const { | ||
redis: redisUrl, | ||
mongo: mongoUrl, | ||
session: sessionConfig, | ||
} = require("../../../loadenv"); | ||
const logger = require("../logger"); | ||
|
||
const getSessionStore = (redisUrl) => { | ||
// 환경변수 REDIS_PATH 유무에 따라 session 저장 방식이 변경됩니다. | ||
if (redisUrl) { | ||
const client = redis.createClient({ | ||
url: redisUrl, | ||
legacyMode: true, | ||
}); | ||
|
||
// redis client 연결 성공 시 로그를 출력합니다. | ||
client.on("ready", () => { | ||
logger.info("Redis session store is connected!"); | ||
}); | ||
|
||
// redis client 에러 발생 시 1초에 두 번 재연결을 시도합니다. | ||
client.on("error", (err) => { | ||
logger.error(err); | ||
}); | ||
|
||
client.connect().catch(logger.error); | ||
return new RedisStore({ client, ttl: sessionConfig.expiry }); | ||
} else { | ||
return MongoStore.create({ mongoUrl }); | ||
} | ||
}; | ||
|
||
module.exports = getSessionStore(redisUrl); |