Skip to content

Commit

Permalink
Merge pull request #5 from cieslarmichal/feature/cache
Browse files Browse the repository at this point in the history
add caching
  • Loading branch information
cieslarmichal authored Feb 9, 2024
2 parents 5448fef + 9a46c30 commit 80606b7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 2 additions & 0 deletions apps/backend/src/common/types/http/httpHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export enum HttpHeader {
authorization = 'Authorization',
contentType = 'Content-Type',
accept = 'Accept',
cacheControl = 'Cache-Control',
contentDisposition = 'Content-Disposition',
}
5 changes: 1 addition & 4 deletions apps/backend/src/common/types/http/httpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { type HttpStatusCode } from './httpStatusCode.js';
export interface HttpResponse<Body = unknown> {
readonly statusCode: HttpStatusCode;
readonly body: Body;
readonly file?: {
readonly name: string;
readonly contentType: string;
};
readonly headers?: Record<string, string>;
}

export interface HttpOkResponse<Body = unknown> extends HttpResponse<Body> {
Expand Down
16 changes: 7 additions & 9 deletions apps/backend/src/core/httpRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -84,7 +84,7 @@ export class HttpRouter {
const {
statusCode,
body: responseBody,
file,
headers,
} = await httpRoute.handler({
body: fastifyRequest.body,
pathParams: fastifyRequest.params,
Expand All @@ -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) {
Expand All @@ -114,8 +114,6 @@ export class HttpRouter {
path: fastifyRequest.url,
method,
statusCode,
file,
time: new Date().getTime() - requestDate.getTime(),
});

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
},
};
}
Expand All @@ -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,
},
};
}
Expand All @@ -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,
},
};
}
Expand Down

0 comments on commit 80606b7

Please sign in to comment.