-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support sessions via session middleware
- Loading branch information
1 parent
3c79c94
commit 5182378
Showing
5 changed files
with
159 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const RedisStore = require("../component/connector/redis"); | ||
const createObjectStore = require("../component/registry"); | ||
|
||
const registry = createObjectStore(); | ||
|
||
module.exports = class Connections { | ||
static get(key, options) { | ||
key = key.toLowerCase(); | ||
|
||
if(!registry.contains(key)) { | ||
switch(key) { | ||
case "redis": | ||
registry.add("redis", Connections.#connectToRedis(options)); | ||
break; | ||
} | ||
} | ||
|
||
return registry.get(key); | ||
} | ||
|
||
static #connectToRedis(redisCreds) { | ||
try { | ||
const redisStore = new RedisStore(redisCreds); | ||
const redisClient = redisStore.getClient(); | ||
const log = Connections.#log.bind(Connections); | ||
|
||
// TO DO: Use a proper log service for this. | ||
redisClient.on("error", (e) => log("Redis error", require("node:util").inspect(e))); | ||
redisClient.on("connect", () => log("Redis connection established")); | ||
|
||
setTimeout(async function() { | ||
if(!redisStore.connecting() && !redisStore.connected()) { | ||
await redisStore.connect(); | ||
} | ||
}, 1000); | ||
|
||
return redisClient; | ||
} catch(e) { | ||
// TO DO: Use a proper log service for this. | ||
log("Redis error", require("node:util").inspect(e)); | ||
} | ||
} | ||
|
||
static #log(message) { | ||
console.log(message); | ||
} | ||
}; |
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