Skip to content

Commit

Permalink
wip: use pino http logger and cloud logging (test with BlaiseAPI)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian4res committed Oct 29, 2024
1 parent 3e16abf commit 600ed11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
20 changes: 12 additions & 8 deletions server/BlaiseAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { CustomConfig } from "../interfaces/server";
import { Auth } from "blaise-login-react/blaise-login-react-server";
import BlaiseApiClient from "blaise-api-node-client";
import createLogger from "../pino";
import AuditLogger from "../AuditLogger";

const pinoLogger = createLogger();

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable pinoLogger.

export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaiseApiClient: BlaiseApiClient): Router {
export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaiseApiClient: BlaiseApiClient, auditLogger: AuditLogger): Router {
const router = express.Router();
router.get("/api/roles", auth.Middleware, async function (req: Request, res: Response) {
res.status(200).json(await blaiseApiClient.getUserRoles());
});

router.get("/api/users", auth.Middleware, async function (req: Request, res: Response) {
auditLogger.info(req.log, `USER: ${req.headers.user} has successfully fetched all users`);
res.status(200).json(await blaiseApiClient.getUsers());
});

Expand Down Expand Up @@ -40,13 +42,13 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise
await blaiseApiClient.changeUserRole(user, role);
await blaiseApiClient.changeUserServerParks(user, newServerParks, newDefaultServerPark);
const successMessage = `Successfully updated user role and permissions to ${role} for ${user}`;
pinoLogger.logger.info(successMessage + ` at ${(new Date()).toLocaleTimeString("en-UK")} ${(new Date()).toLocaleDateString("en-UK")}`);
auditLogger.info(req.log, successMessage + ` at ${(new Date()).toLocaleTimeString("en-UK")} ${(new Date()).toLocaleDateString("en-UK")}`);
return res.status(200).json({
message: successMessage + " today at " + (new Date()).toLocaleTimeString("en-UK")
});
} catch (error) {
const errorMessage = `Error whilst trying to update user role and permissions to ${role} for ${req.params.user}: ${error}`;
pinoLogger.logger.error(errorMessage);
auditLogger.error(req.log, errorMessage + ` at ${(new Date()).toLocaleTimeString("en-UK")} ${(new Date()).toLocaleDateString("en-UK")}`);
return res.status(500).json({
message: errorMessage
});
Expand All @@ -61,14 +63,14 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise
try {
const user = await blaiseApiClient.getUser(req.params.user);
const successMessage = `Successfully fetched user details for ${req.params.user}`;
pinoLogger.logger.info(successMessage);
auditLogger.info(req.log, successMessage);
return res.status(200).json({
message: successMessage,
data: user
});
} catch (error) {
const errorMessage = `Error whilst trying to retrieve user ${req.params.user}: ${error}`;
pinoLogger.logger.error(errorMessage);
auditLogger.error(req.log, errorMessage);
return res.status(500).json({
message: errorMessage,
error: error
Expand All @@ -88,10 +90,10 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise
}

blaiseApiClient.changePassword(req.params.user, password).then(() => {
pinoLogger.logger.info(`Successfully changed password for ${req.params.user}`);
auditLogger.info(req.log, `Successfully changed password for ${req.params.user}`);
return res.status(204).json(null);
}).catch((error: unknown) => {
pinoLogger.logger.error(error);
auditLogger.error(req.log, `Error whilst trying to change password for ${req.params.user}: ${error}`);
return res.status(500).json(error);
});
});
Expand All @@ -104,8 +106,10 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise
}

if (!user) {
auditLogger.error(req.log, "No user provided for deletion");
return res.status(400).json();
}
auditLogger.info(req.log, `Successfully deleted user: ${user}`);
return res.status(204).json(await blaiseApiClient.deleteUser(user));
});

Expand All @@ -124,7 +128,7 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise
data.defaultServerPark = defaultServerPark[0];
}
const currentUser = req.headers.user;
pinoLogger.logger.info(`${currentUser} has created user: ${data.username}`);
auditLogger.info(req.log, `${currentUser} has created user: ${data.username}`);
return res.status(200).json(await blaiseApiClient.createUser(data));
});

Expand Down
19 changes: 9 additions & 10 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import BlaiseAPIRouter from "./BlaiseAPI";
import multer from "multer";
import * as profiler from "@google-cloud/profiler";
import { newLoginHandler, Auth } from "blaise-login-react/blaise-login-react-server";
import pino from "pino";
import { CustomConfig } from "./interfaces/server";
import BlaiseApi from "blaise-api-node-client";
import { Express } from "express";
import fs from "fs";
import AuditLogger from "./AuditLogger";

export default function GetNodeServer(config: CustomConfig, blaiseApi: BlaiseApi, auth: Auth): Express
{
Expand All @@ -21,32 +21,29 @@ export default function GetNodeServer(config: CustomConfig, blaiseApi: BlaiseApi
});

const upload = multer();

const server = express();
const logger = createLogger();

server.use(upload.any());

axios.defaults.timeout = 10000;

const logger = createLogger();
server.use(logger);
logger.logger.info("Server started");
axios.defaults.timeout = 10000;

// where ever the react built package is
const buildFolder = "../build";

const auditLogger = new AuditLogger(config.ProjectId);
const loginHandler = newLoginHandler(auth, blaiseApi);

// Health Check endpoint
server.get("/bam-ui/:version/health", async function (req: Request, res: Response) {
pinoLogger.logger.info("Heath Check endpoint called");
auditLogger.info(req.log, "Heath Check endpoint called");
req.log.info("Heath Check endpoint called");
res.status(200).json({ healthy: true });
});

server.use("/", loginHandler);

// All Endpoints calling the Blaise API
server.use("/", BlaiseAPIRouter(config, auth, blaiseApi));
server.use("/", BlaiseAPIRouter(config, auth, blaiseApi, auditLogger));

// treat the index.html as a template and substitute the values at runtime
server.set("views", path.join(__dirname, "/views"));
Expand All @@ -67,8 +64,10 @@ export default function GetNodeServer(config: CustomConfig, blaiseApi: BlaiseApi

server.use(function (err, _req, res, _next) {
if (err && err.stack) {
auditLogger.error(res, err.stack);
console.error(err.stack);
} else {
auditLogger.error(res, "An undefined error occurred");
console.error("An undefined error occurred");
}
res.render("../views/500.html", {});
Expand Down

0 comments on commit 600ed11

Please sign in to comment.