diff --git a/docker-compose.yml b/docker-compose.yml index 7ff2c31..0582947 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,5 +26,7 @@ services: POSTGRES_USER: ${POSTGRES_USER} volumes: - postgres_data:/var/lib/postgresql/data/pgdata + # ports: + # - "5432:5432" volumes: postgres_data: \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index f5f4421..821d544 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -4,10 +4,16 @@ import { ScenesController } from './scenes/scenes.controller'; import { StorageService } from './storage/storage.service'; import { RoomsController } from './rooms/rooms.controller'; import { FilesController } from './files/files.controller'; +import { HealthController } from './health/health.controller'; @Module({ imports: [], - controllers: [ScenesController, RoomsController, FilesController], + controllers: [ + ScenesController, + RoomsController, + FilesController, + HealthController, + ], providers: [StorageService], }) export class AppModule { diff --git a/src/files/files.controller.spec.ts b/src/files/files.controller.spec.ts index 0358f0a..0c7e824 100644 --- a/src/files/files.controller.spec.ts +++ b/src/files/files.controller.spec.ts @@ -1,11 +1,13 @@ import { Test, TestingModule } from '@nestjs/testing'; import { FilesController } from './files.controller'; +import { StorageService } from '../storage/storage.service'; describe('FilesController', () => { let controller: FilesController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ + providers: [StorageService], controllers: [FilesController], }).compile(); diff --git a/src/files/files.controller.ts b/src/files/files.controller.ts index cc8c5aa..085149e 100644 --- a/src/files/files.controller.ts +++ b/src/files/files.controller.ts @@ -10,7 +10,7 @@ import { Res, } from '@nestjs/common'; import { Response } from 'express'; -import { StorageNamespace, StorageService } from 'src/storage/storage.service'; +import { StorageNamespace, StorageService } from '../storage/storage.service'; import { Readable } from 'stream'; @Controller('files') diff --git a/src/health/health.controller.spec.ts b/src/health/health.controller.spec.ts new file mode 100644 index 0000000..d7e491a --- /dev/null +++ b/src/health/health.controller.spec.ts @@ -0,0 +1,31 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { HealthController } from './health.controller'; +import { StorageService } from '../storage/storage.service'; + +describe('HealthController', () => { + let controller: HealthController; + let storageService: StorageService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [StorageService], + controllers: [HealthController], + }).compile(); + + storageService = module.get(StorageService); + controller = module.get(HealthController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); + + it('returns healthy', async () => { + jest.spyOn(storageService, 'set').mockImplementation(() => { + return new Promise((resolve) => { + return resolve(true); + }); + }); + expect(await controller.health()).toBe('healthy'); + }); +}); diff --git a/src/health/health.controller.ts b/src/health/health.controller.ts new file mode 100644 index 0000000..64ecbc8 --- /dev/null +++ b/src/health/health.controller.ts @@ -0,0 +1,21 @@ +import { Controller, Get, Logger } from '@nestjs/common'; +import { StorageNamespace, StorageService } from '../storage/storage.service'; + +@Controller('health') +export class HealthController { + private readonly logger = new Logger(HealthController.name); + namespace = StorageNamespace.SETTINGS; + + constructor(private storageService: StorageService) {} + + @Get() + async health(): Promise { + const timestamp = new Date().getTime().toString(); + await this.storageService.set( + 'last-health-check', + timestamp, + this.namespace, + ); + return 'healthy'; + } +} diff --git a/src/rooms/rooms.controller.spec.ts b/src/rooms/rooms.controller.spec.ts index 0402040..dc8ad2b 100644 --- a/src/rooms/rooms.controller.spec.ts +++ b/src/rooms/rooms.controller.spec.ts @@ -1,11 +1,13 @@ import { Test, TestingModule } from '@nestjs/testing'; import { RoomsController } from './rooms.controller'; +import { StorageService } from '../storage/storage.service'; describe('RoomsController', () => { let controller: RoomsController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ + providers: [StorageService], controllers: [RoomsController], }).compile(); diff --git a/src/scenes/scenes.controller.spec.ts b/src/scenes/scenes.controller.spec.ts index 513c92d..8ca5970 100644 --- a/src/scenes/scenes.controller.spec.ts +++ b/src/scenes/scenes.controller.spec.ts @@ -1,11 +1,13 @@ import { Test, TestingModule } from '@nestjs/testing'; import { ScenesController } from './scenes.controller'; +import { StorageService } from '../storage/storage.service'; describe('ScenesController', () => { let controller: ScenesController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ + providers: [StorageService], controllers: [ScenesController], }).compile(); diff --git a/src/storage/storage.service.ts b/src/storage/storage.service.ts index e0e8b8a..4117b56 100644 --- a/src/storage/storage.service.ts +++ b/src/storage/storage.service.ts @@ -32,7 +32,11 @@ export class StorageService { async has(key: string, namespace: StorageNamespace): Promise { return !!(await this.storagesMap.get(namespace).get(key)); } - set(key: string, value: Buffer, namespace: StorageNamespace): Promise { + set( + key: string, + value: Buffer | string, + namespace: StorageNamespace, + ): Promise { return this.storagesMap.get(namespace).set(key, value); } } @@ -41,4 +45,5 @@ export enum StorageNamespace { SCENES = 'SCENES', ROOMS = 'ROOMS', FILES = 'FILES', + SETTINGS = 'SETTINGS', }