Skip to content

Commit

Permalink
add health route (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek authored Mar 20, 2024
1 parent d3918ad commit 1cd18f5
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ services:
POSTGRES_USER: ${POSTGRES_USER}
volumes:
- postgres_data:/var/lib/postgresql/data/pgdata
# ports:
# - "5432:5432"
volumes:
postgres_data:
8 changes: 7 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/files/files.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/files/files.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
31 changes: 31 additions & 0 deletions src/health/health.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(StorageService);
controller = module.get<HealthController>(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');
});
});
21 changes: 21 additions & 0 deletions src/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
const timestamp = new Date().getTime().toString();
await this.storageService.set(
'last-health-check',
timestamp,
this.namespace,
);
return 'healthy';
}
}
2 changes: 2 additions & 0 deletions src/rooms/rooms.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
2 changes: 2 additions & 0 deletions src/scenes/scenes.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
7 changes: 6 additions & 1 deletion src/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export class StorageService {
async has(key: string, namespace: StorageNamespace): Promise<boolean> {
return !!(await this.storagesMap.get(namespace).get(key));
}
set(key: string, value: Buffer, namespace: StorageNamespace): Promise<true> {
set(
key: string,
value: Buffer | string,
namespace: StorageNamespace,
): Promise<true> {
return this.storagesMap.get(namespace).set(key, value);
}
}
Expand All @@ -41,4 +45,5 @@ export enum StorageNamespace {
SCENES = 'SCENES',
ROOMS = 'ROOMS',
FILES = 'FILES',
SETTINGS = 'SETTINGS',
}

0 comments on commit 1cd18f5

Please sign in to comment.