Skip to content

Commit

Permalink
improve some logs and fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-trajanovski committed Mar 25, 2024
1 parent 4c31079 commit a3ced51
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { router as downloadRouter } from "./routes/download";
import { router as indexRouter } from "./routes/index";
import { router as uploadRouter } from "./routes/upload";
import { logger } from "@user-office-software/duo-logger";
import { configureLogger } from "./common/configureLogger";

const app = express();
app.set("views", path.join(__dirname, "views"));
Expand Down Expand Up @@ -43,6 +44,13 @@ app.use("/zip_in_place", zipInPlaceRouter);
app.use("/download", downloadRouter);
app.use("/upload", uploadRouter);

configureLogger(
config.graylogEnabled,
config.graylogServer,
config.graylogPort,
config.environment
);

// Delete all zip files in config.path_to_zipped_files older than one hour.
const deleteZipFiles = () => {
try {
Expand Down
9 changes: 5 additions & 4 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import { config } from "./common/config";
import jwtLib from "jsonwebtoken";
import * as fs from "fs";
import { logger } from "@user-office-software/duo-logger";

export const hasFileAccess = (
req: express.Request,
Expand All @@ -21,10 +22,7 @@ export const hasFileAccess = (
let jwtDecoded: Global.JWT;
const jwtToken = req.body.jwt || req.cookies.jwt || req.query.jwt;
try {
jwtDecoded = jwtLib.verify(
jwtToken,
jwtSecret
) as Global.JWT;
jwtDecoded = jwtLib.verify(jwtToken, jwtSecret) as Global.JWT;
} catch (e) {
return {
hasAccess: false,
Expand All @@ -34,6 +32,9 @@ export const hasFileAccess = (
fileNames: [],
};
}

logger.logInfo("Request user: ", { user: jwtDecoded.username });

const authRequest: Global.AuthRequest = {
jwt: jwtDecoded,
endpoint: req.originalUrl,
Expand Down
8 changes: 0 additions & 8 deletions src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs = require("fs");
import path = require("path");
import { configureLogger } from "./configureLogger";

const configFilePath = path.resolve(__dirname, "../../config/config.json");
const configBuffer: string = fs.readFileSync(configFilePath, {
Expand All @@ -9,10 +8,3 @@ const configBuffer: string = fs.readFileSync(configFilePath, {
});

export const config: Record<string, any> = JSON.parse(configBuffer);

configureLogger(
config.graylogEnabled,
config.graylogServer,
config.graylogPort,
config.environment
);
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ server.listen(port);
server.on("error", onError);
server.on("listening", onListening);

process.on("uncaughtException", (error) => {
logger.logException("Unhandled NODE exception", error);
});

/**
* Event listener for HTTP server "error" event.
*/

function onError(error: Record<string, unknown>) {
if (error.syscall !== "listen") {
logger.logError("Error received ", error);

throw error;
}
const bind = "Port " + port;
Expand Down
8 changes: 7 additions & 1 deletion src/routes/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ export const router = express.Router();
* Request zipping of files. Require directory:string and files:string[] in the request body
*/
router.post("/", (req, res) => {
logger.logInfo("Request has been submitted", {
directory: req.body.directory,
fileNames: req.body.files,
});

const { hasAccess, statusCode, error, directory, fileNames } = hasFileAccess(
req,
req.body.directory,
req.body.files
);

if (!hasAccess) {
logger.logError("Error : ", { statusCode, error });
logger.logError("Error: ", { statusCode, error });

return res.render("error", { statusCode, error });
}
try {
Expand Down

0 comments on commit a3ced51

Please sign in to comment.