我正在建立一个旨在服务于移动应用程序的应用程序。除了为客户提供服务外,它还将具有一些后台功能。
我们正在使用swagger,我们希望能够访问后台端点的swagger文档。但是,我们不想公开公开所有端点。
假设所有端点都是公共的,那么我们正在考虑的一种解决方案是让服务器为两个端口提供服务,然后仅将一个端口公开。我们创建了一个小的示例存储库,该存储库在两个不同的端口上为一个客户端模块和一个后台模块提供服务。
该main.ts
如下所示:
import { NestFactory } from '@nestjs/core';
import { ClientModule } from './modules/client/client.module';
import * as express from 'express';
import * as http from 'http';
import {ExpressAdapter} from '@nestjs/platform-express';
import { BackOfficeModule } from './modules/backoffice/backoffice.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
async function bootstrap() {
const clientServer = express();
const clientApp = await NestFactory.create(
ClientModule,
new ExpressAdapter(clientServer),
);
const clientOptions = new DocumentBuilder()
.setTitle('ClientServer')
.setDescription('The client server API description')
.setVersion('1.0')
.addTag('client')
.build();
const clientDocument = SwaggerModule.createDocument(clientApp, clientOptions);
SwaggerModule.setup('api', clientApp, clientDocument);
await clientApp.init();
const backOfficeServer = express();
const backOfficeApp = await NestFactory.create(
BackOfficeModule,
new ExpressAdapter(backOfficeServer),
);
const backOfficeOptions = new DocumentBuilder()
.setTitle('BackOffice')
.setDescription('The back office API description')
.setVersion('1.0')
.addTag('backOffice')
.build();
const backOfficeDocument = SwaggerModule.createDocument(backOfficeApp, backOfficeOptions);
SwaggerModule.setup('api', backOfficeApp, backOfficeDocument);
await backOfficeApp.init();
http.createServer(clientServer).listen(3000); // The public port (Load balancer will route traffic to this port)
http.createServer(backOfficeServer).listen(4000); // The private port (Will be accessed through a bastian host or similar)
}
bootstrap();
另一个选择是在代码库和基础结构之间建立更大的隔离,但是由于这是一个非常早期的阶段,我们认为这是不必要的。
因此,我们向Nest社区提出的问题是,有人这样做吗?如果是这样,您的经验是什么?分离这样的后端代码有什么弊端?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句