Commit 87bb49ac authored by nanahira's avatar nanahira

Merge branch 'direct-koa'

parents f6ba250a 219fcfd3
This diff is collapsed.
......@@ -58,7 +58,6 @@
},
"dependencies": {
"@koa/router": "^10.1.1",
"http-proxy-middleware": "^2.0.1",
"koa": "^2.13.3",
"koa-bodyparser": "^4.3.0",
"lodash": "^4.17.21"
......
......@@ -38,29 +38,7 @@ const koishiContextProvider: Provider<Context> = {
@Module({
imports: [DiscoveryModule],
providers: [
{
provide: KoishiService,
inject: [
KOISHI_MODULE_OPTIONS,
KoishiMetascanService,
KoishiLoggerService,
],
useFactory: async (
options: KoishiModuleOptions,
metascan: KoishiMetascanService,
) => {
const koishi = new KoishiService(options, metascan);
koishi._nestKoaTmpServer = createServer(
koishi._nestKoaTmpInstance.callback(),
);
await new Promise<void>((resolve) => {
koishi._nestKoaTmpServer.listen(0, 'localhost', resolve);
});
koishi._nestKoaTmpServerPort = (koishi._nestKoaTmpServer.address() as AddressInfo).port;
koishi.options.port = koishi._nestKoaTmpServerPort;
return koishi;
},
},
KoishiService,
KoishiLoggerService,
KoishiMetascanService,
koishiContextProvider,
......
import { App } from 'koishi';
import {
Inject,
Injectable,
OnApplicationBootstrap,
OnModuleDestroy,
......@@ -12,14 +13,18 @@ import KoaRouter from '@koa/router';
import KoaBodyParser from 'koa-bodyparser';
import { KoishiMetascanService } from './providers/koishi-metascan.service';
import { applySelector } from './utility/koishi.utility';
import { KOISHI_MODULE_OPTIONS } from './utility/koishi.constants';
import { KoishiLoggerService } from './providers/koishi-logger.service';
@Injectable()
export class KoishiService
extends App
implements OnModuleInit, OnApplicationBootstrap, OnModuleDestroy {
constructor(
@Inject(KOISHI_MODULE_OPTIONS)
private readonly koishiModuleOptions: KoishiModuleOptions,
private readonly metascan: KoishiMetascanService,
private readonly koishiLogger: KoishiLoggerService,
) {
super({
...koishiModuleOptions,
......@@ -29,22 +34,20 @@ export class KoishiService
this._nestKoaTmpInstance.use(this.router.routes());
this._nestKoaTmpInstance.use(this.router.allowedMethods());
this._nestKoaTmpInstance.use(KoaBodyParser());
this.options.port = 1;
}
_nestKoaTmpInstance = new Koa();
_nestKoaTmpServer: Server;
_nestKoaTmpServerPort: number;
readonly _nestKoaTmpInstance = new Koa();
private async setHttpServer() {
const httpAdapter = this.metascan.getHttpAdapter();
if (httpAdapter) {
const httpServer: Server = httpAdapter.getHttpServer();
if (httpServer instanceof Server) {
this.logger('app').info('App using Nest HTTP Server.');
this._httpServer = httpServer;
}
} else {
this._httpServer = this._nestKoaTmpServer;
if (!httpAdapter) {
return;
}
const httpServer: Server = httpAdapter.getHttpServer();
if (httpServer instanceof Server) {
this.logger('app').info('App using Nest HTTP Server.');
this._httpServer = httpServer;
}
}
......@@ -66,8 +69,5 @@ export class KoishiService
async onModuleDestroy() {
await this.stop();
if (this._nestKoaTmpServer) {
this._nestKoaTmpServer.close();
}
}
}
import { Injectable, NestMiddleware, OnModuleInit } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express';
import { KoishiService } from '../koishi.service';
import { createProxyMiddleware, RequestHandler } from 'http-proxy-middleware';
import { IncomingMessage, ServerResponse } from 'http';
import { Http2ServerRequest, Http2ServerResponse } from 'http2';
@Injectable()
export class KoishiMiddleware
implements NestMiddleware<Request, Response>, OnModuleInit {
constructor(private koishi: KoishiService) {}
private proxyMiddleware: RequestHandler;
async onModuleInit() {
this.proxyMiddleware = createProxyMiddleware({
target: `http://localhost:${this.koishi._nestKoaTmpServerPort}`,
ws: false,
logLevel: 'silent',
});
export class KoishiMiddleware implements NestMiddleware<Request, Response> {
private readonly koaCallback: (
req: IncomingMessage | Http2ServerRequest,
res: ServerResponse | Http2ServerResponse,
) => void;
constructor(private koishi: KoishiService) {
this.koaCallback = this.koishi._nestKoaTmpInstance.callback();
}
use(req: Request, res: Response, next: NextFunction) {
const match = this.koishi.router.match(req.baseUrl, req.method);
const baseUrl = req.baseUrl || req.url;
const exactUrl = req.originalUrl || baseUrl;
const match = this.koishi.router.match(baseUrl, req.method);
if (!match.route) {
return next();
}
return this.proxyMiddleware(req, res, next);
req.url = exactUrl;
return this.koaCallback(req, res);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment