From 9a46c30a93fa95cb7fa79c2c2cb10500814ac640 Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Fri, 9 Feb 2024 10:50:32 +0100 Subject: [PATCH] add caching --- .../src/common/types/http/httpHeader.ts | 2 ++ .../src/common/types/http/httpResponse.ts | 5 +---- apps/backend/src/core/httpRouter.ts | 16 +++++++------- .../resourceHttpController.ts | 21 +++++++++++-------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/apps/backend/src/common/types/http/httpHeader.ts b/apps/backend/src/common/types/http/httpHeader.ts index 654b75d..6985ad3 100644 --- a/apps/backend/src/common/types/http/httpHeader.ts +++ b/apps/backend/src/common/types/http/httpHeader.ts @@ -2,4 +2,6 @@ export enum HttpHeader { authorization = 'Authorization', contentType = 'Content-Type', accept = 'Accept', + cacheControl = 'Cache-Control', + contentDisposition = 'Content-Disposition', } diff --git a/apps/backend/src/common/types/http/httpResponse.ts b/apps/backend/src/common/types/http/httpResponse.ts index 2c40aeb..e7dc108 100644 --- a/apps/backend/src/common/types/http/httpResponse.ts +++ b/apps/backend/src/common/types/http/httpResponse.ts @@ -3,10 +3,7 @@ import { type HttpStatusCode } from './httpStatusCode.js'; export interface HttpResponse { readonly statusCode: HttpStatusCode; readonly body: Body; - readonly file?: { - readonly name: string; - readonly contentType: string; - }; + readonly headers?: Record; } export interface HttpOkResponse extends HttpResponse { diff --git a/apps/backend/src/core/httpRouter.ts b/apps/backend/src/core/httpRouter.ts index 63d1ffa..a4ade1d 100644 --- a/apps/backend/src/core/httpRouter.ts +++ b/apps/backend/src/core/httpRouter.ts @@ -70,7 +70,7 @@ export class HttpRouter { const requestDate = new Date(); try { - this.loggerService.info({ + this.loggerService.debug({ message: 'Received an HTTP request.', source: HttpRouter.name, path: fastifyRequest.url, @@ -84,7 +84,7 @@ export class HttpRouter { const { statusCode, body: responseBody, - file, + headers, } = await httpRoute.handler({ body: fastifyRequest.body, pathParams: fastifyRequest.params, @@ -94,12 +94,12 @@ export class HttpRouter { fastifyReply.status(statusCode); - if (file) { - fastifyReply.header(HttpHeader.contentType, `${file.contentType}`); + fastifyReply.header(HttpHeader.contentType, 'application/json'); - fastifyReply.header('Content-Disposition', `attachment; filename=${file.name}`); - } else { - fastifyReply.header(HttpHeader.contentType, 'application/json'); + if (headers) { + Object.entries(headers).forEach(([headerName, headerValue]) => { + fastifyReply.header(headerName, headerValue); + }); } if (responseBody) { @@ -114,8 +114,6 @@ export class HttpRouter { path: fastifyRequest.url, method, statusCode, - file, - time: new Date().getTime() - requestDate.getTime(), }); return; diff --git a/apps/backend/src/modules/resourceModule/api/httpControllers/resourceHttpController/resourceHttpController.ts b/apps/backend/src/modules/resourceModule/api/httpControllers/resourceHttpController/resourceHttpController.ts index 136ab8d..8290c5f 100644 --- a/apps/backend/src/modules/resourceModule/api/httpControllers/resourceHttpController/resourceHttpController.ts +++ b/apps/backend/src/modules/resourceModule/api/httpControllers/resourceHttpController/resourceHttpController.ts @@ -33,6 +33,7 @@ import { } from './schemas/findResourcesSchema.js'; import { type ResourceMetadataDTO } from './schemas/resourceMetadataDTO.js'; import { type HttpController } from '../../../../../common/types/http/httpController.js'; +import { HttpHeader } from '../../../../../common/types/http/httpHeader.js'; import { HttpMethodName } from '../../../../../common/types/http/httpMethodName.js'; import { type HttpRequest } from '../../../../../common/types/http/httpRequest.js'; import { type HttpOkResponse, type HttpNoContentResponse } from '../../../../../common/types/http/httpResponse.js'; @@ -253,9 +254,9 @@ export class ResourceHttpController implements HttpController { return { statusCode: HttpStatusCode.ok, body: resourcesData, - file: { - name: 'resources.zip', - contentType: 'application/zip', + headers: { + [HttpHeader.contentDisposition]: 'attachment; filename=resources.zip', + [HttpHeader.contentType]: 'application/zip', }, }; } @@ -278,9 +279,10 @@ export class ResourceHttpController implements HttpController { return { statusCode: HttpStatusCode.ok, body: resource.data, - file: { - name: resource.name, - contentType: resource.contentType, + headers: { + [HttpHeader.cacheControl]: 'max-age=2592000', + [HttpHeader.contentDisposition]: `attachment; filename=${resource.name}`, + [HttpHeader.contentType]: resource.contentType, }, }; } @@ -305,9 +307,10 @@ export class ResourceHttpController implements HttpController { return { statusCode: HttpStatusCode.ok, body: resource.data, - file: { - name: resource.name, - contentType: resource.contentType, + headers: { + [HttpHeader.cacheControl]: 'max-age=2592000', + [HttpHeader.contentDisposition]: `attachment; filename=${resource.name}`, + [HttpHeader.contentType]: resource.contentType, }, }; }