Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ttl setting #161

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MOCKBIN_PORT=8080
MOCKBIN_QUIET=false
MOCKBIN_REDIS=redis://127.0.0.1:6379
MOCKBIN_REDIS_EXPIRE_SECONDS=1000000000
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ services:
build: .
environment:
MOCKBIN_REDIS: "//redis:6379"
MOCKBIN_REDIS_EXPIRE_SECONDS: 1000000000
links:
- redis
ports:
Expand Down
7 changes: 6 additions & 1 deletion lib/routes/bins/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ module.exports = (client) => (req, res, next) => {
.then(function () {
const id = uuid.v4();

client.set(`bin:${id}`, JSON.stringify(mock));
client.set(
`bin:${id}`,
JSON.stringify(mock),
"EX",
process.env.MOCKBIN_REDIS_EXPIRE_SECONDS,
);

res.view = "redirect";
res.status(201).location(util.format("/bin/%s", id)).body = id;
Expand Down
7 changes: 6 additions & 1 deletion lib/routes/bins/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ module.exports = (client) => (req, res, next) => {
validate
.response(mock)
.then(function () {
client.set(`bin:${compoundId}`, JSON.stringify(mock));
client.set(
`bin:${compoundId}`,
JSON.stringify(mock),
"EX",
process.env.MOCKBIN_REDIS_EXPIRE_SECONDS,
);

res.view = "redirect";
res.status(200).location(`/bin/${compoundId}`).body = id;
Expand Down
8 changes: 7 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ const options = {
port: process.env.MOCKBIN_PORT,
quiet: process.env.MOCKBIN_QUIET,
redis: process.env.MOCKBIN_REDIS,
redisExpiry: process.env.MOCKBIN_REDIS_EXPIRE_SECONDS,
};

app(options, () => {
console.info("starting server", options.port, options.redis);
console.info(
"starting server",
options.port,
options.redis,
options.redisExpiry,
);
if (!options.port || !options.redis) {
console.warn(`
------------------------
Expand Down
25 changes: 15 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,21 @@ module.exports = (options, done) => {
// magic starts here
app.use("/", router(options));
app.use("/healthcheck", (req, res) => {
res.set({ 'Content-Type': 'application/json; charset=utf-8' })
res.send(JSON.stringify({
name: process.env.npm_package_name,
version: process.env.npm_package_version,
uptimeSeconds: process.uptime(),
memory: process.memoryUsage(),
pid: process.pid,
versions: process.versions,
}, null, 2));

res.set({ "Content-Type": "application/json; charset=utf-8" });
res.send(
JSON.stringify(
{
name: process.env.npm_package_name,
version: process.env.npm_package_version,
uptimeSeconds: process.uptime(),
memory: process.memoryUsage(),
pid: process.pid,
versions: process.versions,
},
null,
2,
),
);
});

app.listen(options.port);
Expand Down
Loading