-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 4247_create_application_versioning_part_3
- Loading branch information
Showing
68 changed files
with
2,368 additions
and
595 deletions.
There are no files selected for viewing
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 was deleted.
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
45 changes: 45 additions & 0 deletions
45
sources/packages/backend/apps/api/src/middlewares/access-logger.middleware.ts
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,45 @@ | ||
import { Injectable, LoggerService, NestMiddleware } from "@nestjs/common"; | ||
import { JwtService } from "@nestjs/jwt"; | ||
import { InjectLogger } from "@sims/utilities/logger"; | ||
import { IUserToken } from "../auth"; | ||
import { getClientIPFromRequest } from "../utilities"; | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
@Injectable() | ||
export class AccessLoggerMiddleware implements NestMiddleware { | ||
constructor(private readonly jwtService: JwtService) {} | ||
/** | ||
* Logs access information of every request. | ||
* @param request http request. | ||
* @param _response http response. | ||
* @param next next function. | ||
*/ | ||
use(request: Request, _response: Response, next: NextFunction) { | ||
const { headers, originalUrl, method } = request; | ||
const clientIP = getClientIPFromRequest(request); | ||
const user = this.getUserFromBearerToken(request.headers.authorization); | ||
const userGUID = user ? user.userName : "User GUID not found"; | ||
const userAgent = headers["user-agent"] ?? "User agent not found"; | ||
const userAccessLog = `Request - ${method} ${originalUrl} From ${clientIP} | User GUID: ${userGUID} | User Agent: ${userAgent}`; | ||
this.logger.log(userAccessLog); | ||
next(); | ||
} | ||
|
||
/** | ||
* Get decoded user information from bearer token. | ||
* @param bearerToken token to be decoded. | ||
* @returns decoded user information. | ||
*/ | ||
private getUserFromBearerToken(bearerToken: string): IUserToken | null { | ||
if (!bearerToken) { | ||
return null; | ||
} | ||
const user = this.jwtService.decode<IUserToken>( | ||
bearerToken.replace("Bearer ", ""), | ||
); | ||
return user; | ||
} | ||
|
||
@InjectLogger() | ||
logger: LoggerService; | ||
} |
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 @@ | ||
export * from "./access-logger.middleware"; |
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
14 changes: 6 additions & 8 deletions
14
...ckend/apps/api/src/app.controller.spec.ts → ...rs/health-check/health.controller.spec.ts
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 |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { AppController } from "./app.controller"; | ||
import { AppService } from "./app.service"; | ||
import { HealthController } from "./health.controller"; | ||
import { DatabaseModule } from "@sims/sims-db"; | ||
|
||
// TODO: must mock DB dependencies. | ||
describe.skip("AppController", () => { | ||
let appController: AppController; | ||
describe.skip("HealthController", () => { | ||
let healthController: HealthController; | ||
|
||
beforeEach(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
imports: [DatabaseModule], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
controllers: [HealthController], | ||
}).compile(); | ||
|
||
appController = app.get<AppController>(AppController); | ||
healthController = app.get<HealthController>(HealthController); | ||
}); | ||
|
||
describe("root", () => { | ||
it("should return Hello world string with db connection status and version", () => { | ||
const expected = `Hello World! The database dataSource is true and version: ${ | ||
process.env.VERSION ?? "-1" | ||
}`; | ||
expect(appController.getHello()).toBe(expected); | ||
expect(healthController.getHello()).toBe(expected); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
sources/packages/backend/apps/api/src/route-controllers/health-check/health.controller.ts
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,32 @@ | ||
import { Controller, Get } from "@nestjs/common"; | ||
import { Public } from "../../auth/decorators"; | ||
import { DataSource } from "typeorm"; | ||
|
||
@Controller("health") | ||
export class HealthController { | ||
constructor(private readonly dataSource: DataSource) {} | ||
|
||
/** | ||
* Returns a health check status. | ||
* @returns health check status. | ||
*/ | ||
@Get() | ||
@Public() | ||
getHello(): string { | ||
return this.getHeathCheckStatus(); | ||
} | ||
|
||
/** | ||
* Get health check status. | ||
* @returns health check status. | ||
*/ | ||
private getHeathCheckStatus(): string { | ||
try { | ||
return `Hello World! The database dataSource is ${ | ||
this.dataSource.isInitialized | ||
} and version: ${process.env.VERSION ?? "-1"}`; | ||
} catch (error: unknown) { | ||
return `Hello world! Fail with error: ${error}`; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.