add health route (#19)

This commit is contained in:
JannikStreek 2024-03-20 10:48:31 +01:00 committed by GitHub
parent d3918ad9da
commit 1cd18f5a14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 74 additions and 3 deletions

View File

@ -26,5 +26,7 @@ services:
POSTGRES_USER: ${POSTGRES_USER}
volumes:
- postgres_data:/var/lib/postgresql/data/pgdata
# ports:
# - "5432:5432"
volumes:
postgres_data:

View File

@ -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 {

View File

@ -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();

View File

@ -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')

View File

@ -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');
});
});

View File

@ -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';
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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);
}
}
@ -41,4 +45,5 @@ export enum StorageNamespace {
SCENES = 'SCENES',
ROOMS = 'ROOMS',
FILES = 'FILES',
SETTINGS = 'SETTINGS',
}