Skip to content

Commit 5b92f28

Browse files
authored
Backend. Add caching #175 (#88)
* Backend. Add caching for static images #175 Visual-Regression-Tracker/Visual-Regression-Tracker#175 * cache-manager added
1 parent fce3639 commit 5b92f28

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

package-lock.json

Lines changed: 29 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test:e2e": "jest --config ./test/jest-e2e.json"
2222
},
2323
"dependencies": {
24-
"@nestjs/common": "^7.4.2",
24+
"@nestjs/common": "^7.6.5",
2525
"@nestjs/config": "^0.5.0",
2626
"@nestjs/core": "^7.4.2",
2727
"@nestjs/jwt": "^7.1.0",
@@ -32,6 +32,7 @@
3232
"@nestjs/websockets": "^7.4.2",
3333
"@prisma/client": "2.12.1",
3434
"bcryptjs": "^2.4.3",
35+
"cache-manager": "^3.4.0",
3536
"class-transformer": "^0.3.1",
3637
"class-validator": "^0.12.2",
3738
"fs-extra": "^9.0.1",
@@ -54,6 +55,7 @@
5455
"@nestjs/testing": "^7.4.2",
5556
"@prisma/cli": "2.12.1",
5657
"@types/bcryptjs": "^2.4.2",
58+
"@types/cache-manager": "^2.10.3",
5759
"@types/express": "^4.17.7",
5860
"@types/jest": "26.0.14",
5961
"@types/node": "^14.0.27",

src/app.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module } from '@nestjs/common';
1+
import { CacheInterceptor, CacheModule, Module } from '@nestjs/common';
22
import { AuthModule } from './auth/auth.module';
33
import { UsersModule } from './users/users.module';
44
import { BuildsModule } from './builds/builds.module';
@@ -7,12 +7,13 @@ import { TestRunsModule } from './test-runs/test-runs.module';
77
import { TestVariationsModule } from './test-variations/test-variations.module';
88
import { PrismaService } from './prisma/prisma.service';
99
import { ConfigModule } from '@nestjs/config';
10-
import { APP_FILTER } from '@nestjs/core';
10+
import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';
1111
import { HttpExceptionFilter } from './http-exception.filter';
1212

1313
@Module({
1414
imports: [
1515
ConfigModule.forRoot({ isGlobal: true }),
16+
CacheModule.register(),
1617
AuthModule,
1718
UsersModule,
1819
BuildsModule,
@@ -26,6 +27,10 @@ import { HttpExceptionFilter } from './http-exception.filter';
2627
provide: APP_FILTER,
2728
useClass: HttpExceptionFilter,
2829
},
30+
{
31+
provide: APP_INTERCEPTOR,
32+
useClass: CacheInterceptor,
33+
},
2934
],
3035
})
3136
export class AppModule {}

src/main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { join } from 'path';
77
import * as bodyParser from 'body-parser';
88
import { readFileSync, existsSync } from 'fs';
99
import { HttpsOptions } from '@nestjs/common/interfaces/external/https-options.interface';
10+
import { IMAGE_PATH } from './shared/static/static.service';
11+
import { NestExpressApplication } from '@nestjs/platform-express';
1012

1113
function getHttpsOptions(): HttpsOptions | null {
1214
const keyPath = './secrets/ssl.key';
@@ -22,7 +24,7 @@ function getHttpsOptions(): HttpsOptions | null {
2224
}
2325

2426
async function bootstrap() {
25-
const app = await NestFactory.create(AppModule, {
27+
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
2628
cors: true,
2729
httpsOptions: getHttpsOptions(),
2830
});
@@ -34,7 +36,9 @@ async function bootstrap() {
3436
}
3537

3638
// serve images
37-
app.use(express.static(join(process.cwd(), 'imageUploads/')));
39+
app.useStaticAssets(join(process.cwd(), IMAGE_PATH), {
40+
maxAge: 31536000
41+
});
3842

3943
await app.listen(process.env.APP_PORT || 3000);
4044
}

src/shared/static/static.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from 'path';
33
import { writeFileSync, readFileSync, unlink, mkdirSync, existsSync } from 'fs';
44
import { PNG, PNGWithMetadata } from 'pngjs';
55

6+
export const IMAGE_PATH = 'imageUploads/'
7+
68
@Injectable()
79
export class StaticService {
810
saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): string {
@@ -35,9 +37,8 @@ export class StaticService {
3537
}
3638

3739
private getImagePath(imageName: string): string {
38-
const dir = 'imageUploads/';
39-
this.ensureDirectoryExistence(dir);
40-
return path.resolve(dir, imageName);
40+
this.ensureDirectoryExistence(IMAGE_PATH);
41+
return path.resolve(IMAGE_PATH, imageName);
4142
}
4243

4344
private ensureDirectoryExistence(dir: string) {

0 commit comments

Comments
 (0)