add health route (#19)
This commit is contained in:
parent
d3918ad9da
commit
1cd18f5a14
@ -26,5 +26,7 @@ services:
|
|||||||
POSTGRES_USER: ${POSTGRES_USER}
|
POSTGRES_USER: ${POSTGRES_USER}
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data/pgdata
|
- postgres_data:/var/lib/postgresql/data/pgdata
|
||||||
|
# ports:
|
||||||
|
# - "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
postgres_data:
|
postgres_data:
|
@ -4,10 +4,16 @@ import { ScenesController } from './scenes/scenes.controller';
|
|||||||
import { StorageService } from './storage/storage.service';
|
import { StorageService } from './storage/storage.service';
|
||||||
import { RoomsController } from './rooms/rooms.controller';
|
import { RoomsController } from './rooms/rooms.controller';
|
||||||
import { FilesController } from './files/files.controller';
|
import { FilesController } from './files/files.controller';
|
||||||
|
import { HealthController } from './health/health.controller';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [],
|
imports: [],
|
||||||
controllers: [ScenesController, RoomsController, FilesController],
|
controllers: [
|
||||||
|
ScenesController,
|
||||||
|
RoomsController,
|
||||||
|
FilesController,
|
||||||
|
HealthController,
|
||||||
|
],
|
||||||
providers: [StorageService],
|
providers: [StorageService],
|
||||||
})
|
})
|
||||||
export class AppModule {
|
export class AppModule {
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { FilesController } from './files.controller';
|
import { FilesController } from './files.controller';
|
||||||
|
import { StorageService } from '../storage/storage.service';
|
||||||
|
|
||||||
describe('FilesController', () => {
|
describe('FilesController', () => {
|
||||||
let controller: FilesController;
|
let controller: FilesController;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [StorageService],
|
||||||
controllers: [FilesController],
|
controllers: [FilesController],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import {
|
|||||||
Res,
|
Res,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
import { StorageNamespace, StorageService } from 'src/storage/storage.service';
|
import { StorageNamespace, StorageService } from '../storage/storage.service';
|
||||||
import { Readable } from 'stream';
|
import { Readable } from 'stream';
|
||||||
|
|
||||||
@Controller('files')
|
@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 { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { RoomsController } from './rooms.controller';
|
import { RoomsController } from './rooms.controller';
|
||||||
|
import { StorageService } from '../storage/storage.service';
|
||||||
|
|
||||||
describe('RoomsController', () => {
|
describe('RoomsController', () => {
|
||||||
let controller: RoomsController;
|
let controller: RoomsController;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [StorageService],
|
||||||
controllers: [RoomsController],
|
controllers: [RoomsController],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { ScenesController } from './scenes.controller';
|
import { ScenesController } from './scenes.controller';
|
||||||
|
import { StorageService } from '../storage/storage.service';
|
||||||
|
|
||||||
describe('ScenesController', () => {
|
describe('ScenesController', () => {
|
||||||
let controller: ScenesController;
|
let controller: ScenesController;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [StorageService],
|
||||||
controllers: [ScenesController],
|
controllers: [ScenesController],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
|
@ -32,7 +32,11 @@ export class StorageService {
|
|||||||
async has(key: string, namespace: StorageNamespace): Promise<boolean> {
|
async has(key: string, namespace: StorageNamespace): Promise<boolean> {
|
||||||
return !!(await this.storagesMap.get(namespace).get(key));
|
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);
|
return this.storagesMap.get(namespace).set(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,4 +45,5 @@ export enum StorageNamespace {
|
|||||||
SCENES = 'SCENES',
|
SCENES = 'SCENES',
|
||||||
ROOMS = 'ROOMS',
|
ROOMS = 'ROOMS',
|
||||||
FILES = 'FILES',
|
FILES = 'FILES',
|
||||||
|
SETTINGS = 'SETTINGS',
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user