Skip to content

Commit 91329be

Browse files
committed
Create controller for status monitor
1 parent 5012bbb commit 91329be

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

examples/test-status-monitor/src/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Module } from '@nestjs/common';
22
import { AppController } from './app.controller';
33
import { AppService } from './app.service';
4+
import { StatusMonitorModule } from '../../../dist/status.monitor.module';
45

56
@Module({
6-
imports: [],
7+
imports: [StatusMonitorModule],
78
controllers: [AppController],
89
providers: [AppService],
910
})
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { NestFactory } from '@nestjs/core';
22
import { AppModule } from './app.module';
3-
import { StatusMonitorModule } from '../../../dist/status.monitor.module';
43

54
async function bootstrap() {
65
const app = await NestFactory.create(AppModule);
7-
8-
StatusMonitorModule.setup('/monitor', app);
96
await app.listen(3000);
107
}
118
bootstrap();

src/status.monitor.controller.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Get, Controller, HttpCode } from '@nestjs/common';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
import Handlebars from 'handlebars';
5+
6+
const controllerPath = 'monitor';
7+
8+
@Controller(controllerPath)
9+
export class StatusMonitorController {
10+
@Get()
11+
@HttpCode(200)
12+
root() {
13+
const data = {
14+
script: fs.readFileSync(
15+
path.join(__dirname, '/public/javascripts/app.js'),
16+
),
17+
style: fs.readFileSync(
18+
path.join(__dirname, '/public/stylesheets/style.css'),
19+
),
20+
};
21+
22+
const htmlTmpl = fs
23+
.readFileSync(path.join(__dirname, '/public/index.html'))
24+
.toString();
25+
26+
const render = Handlebars.compile(htmlTmpl);
27+
return render(data);
28+
}
29+
}

src/status.monitor.module.ts

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,7 @@
1-
import { INestApplication } from '@nestjs/common';
2-
import { FastifyAdapter } from '@nestjs/core';
3-
import { loadPackage } from '@nestjs/common/utils/load-package.util';
4-
import * as fs from 'fs';
5-
import * as path from 'path';
6-
import Handlebars from 'handlebars';
1+
import { Module } from '@nestjs/common';
2+
import { StatusMonitorController } from './status.monitor.controller';
73

8-
export class StatusMonitorModule {
9-
public static setup(endpointPath: string, app: INestApplication) {
10-
const validatePath = (path): string =>
11-
path.charAt(0) !== '/' ? '/' + path : path;
12-
13-
const httpServer = app.getHttpServer();
14-
if (httpServer instanceof FastifyAdapter) {
15-
return this.setupFastify(endpointPath, httpServer);
16-
}
17-
18-
const finalPath = validatePath(endpointPath);
19-
20-
const data = {
21-
script: fs.readFileSync(
22-
path.join(__dirname, '/public/javascripts/app.js'),
23-
),
24-
style: fs.readFileSync(
25-
path.join(__dirname, '/public/stylesheets/style.css'),
26-
),
27-
};
28-
29-
const htmlTmpl = fs
30-
.readFileSync(path.join(__dirname, '/public/index.html'))
31-
.toString();
32-
33-
const render = Handlebars.compile(htmlTmpl);
34-
35-
app.use(finalPath, (req, res) => res.send(render(data)));
36-
}
37-
38-
private static setupFastify(path: string, httpServer: FastifyAdapter) {
39-
httpServer.register(loadPackage('fastify-swagger', 'StatusMonitorModule'), {
40-
exposeRoute: true,
41-
routePrefix: path,
42-
mode: 'static',
43-
});
44-
}
45-
}
4+
@Module({
5+
controllers: [StatusMonitorController],
6+
})
7+
export class StatusMonitorModule {}

0 commit comments

Comments
 (0)