Skip to content

Commit

Permalink
chore(logs): add more logs (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cali93 authored May 31, 2023
1 parent 50b5a7d commit 3ebffee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
30 changes: 18 additions & 12 deletions src/handlers/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,25 @@ export const verifyAndSignIn = async (req: Request, res: Response) => {
req.session.siwe = undefined;
req.session.nonce = undefined;
console.error(e);
switch (e) {
case ErrorTypes.EXPIRED_MESSAGE: {
req.session.save(() => res.status(440).json({ message: e.message }));
break;
}
case ErrorTypes.INVALID_SIGNATURE: {
req.session.save(() => res.status(422).json({ message: e.message }));
break;
}
default: {
req.session.save(() => res.status(500).json({ message: e.message }));
break;
try {
switch (e) {
case ErrorTypes.EXPIRED_MESSAGE: {
req.session.save(() => res.status(440).json({ message: e.message }));
break;
}
case ErrorTypes.INVALID_SIGNATURE: {
req.session.save(() => res.status(422).json({ message: e.message }));
break;
}
default: {
req.session.save(() => res.status(500).json({ message: e.message }));
break;
}
}
} catch (sessionError) {
console.error(`Failed to save session, ${JSON.stringify(sessionError)}`);
}

return;
}
};
36 changes: 16 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ const corsOptions: CorsOptions = {
};
app.use(cors(corsOptions));

const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 30, // Limit each IP to 30 requests per `window` (here, per 10 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});

// Apply the rate limiting middleware to all requests
app.use(limiter);

app.use(
Session({
name: COOKIE_NAME,
Expand All @@ -78,6 +68,16 @@ app.use(
})
);

const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 200, // Limit each IP to 200 requests per `window` (here, per 10 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});

// Apply the rate limiting middleware to all requests
app.use(limiter);

app.get("/health", async function (req, res) {
return res.status(200).json({ status: "OK" });
});
Expand Down Expand Up @@ -107,22 +107,18 @@ const server = app.listen(PORT, () => {
// Create a function to close the server and exit the process
const exitProcess = () => {
console.log("Closing server and exiting process...");
server.close(() => {
return server.close(() => {
console.log("Server closed.");
process.exit(1);
return process.exit(1);
});
};

// Gracefully handle SIGINT (Ctrl+C) and SIGTERM (docker stop)
process.on("SIGINT", exitProcess);
process.on("SIGTERM", exitProcess);

// Gracefully handle uncaught exceptions and rejections
process.on("uncaughtException", (err) => {
console.error("Uncaught exception:", err);
exitProcess();
console.log(`Uncaught exception: ${JSON.stringify(err)}`);
return exitProcess();
});
process.on("unhandledRejection", (err) => {
console.error("Unhandled rejection:", err);
exitProcess();
console.log(`Unhandled rejection: ${JSON.stringify(err)}`);
return exitProcess();
});

0 comments on commit 3ebffee

Please sign in to comment.