add health route (#19)
This commit is contained in:
parent
d3918ad9da
commit
1cd18f5a14
@ -26,5 +26,7 @@ services:
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data/pgdata
|
||||
# ports:
|
||||
# - "5432:5432"
|
||||
volumes:
|
||||
postgres_data:
|
@ -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 {
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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')
|
||||
|
31
src/health/health.controller.spec.ts
Normal file
31
src/health/health.controller.spec.ts
Normal 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');
|
||||
});
|
||||
});
|
21
src/health/health.controller.ts
Normal file
21
src/health/health.controller.ts
Normal 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';
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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',
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user