Commit b57a9afc authored by nanahira's avatar nanahira

direct koa

parent f6ba250a
...@@ -38,29 +38,7 @@ const koishiContextProvider: Provider<Context> = { ...@@ -38,29 +38,7 @@ const koishiContextProvider: Provider<Context> = {
@Module({ @Module({
imports: [DiscoveryModule], imports: [DiscoveryModule],
providers: [ providers: [
{ KoishiService,
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;
},
},
KoishiLoggerService, KoishiLoggerService,
KoishiMetascanService, KoishiMetascanService,
koishiContextProvider, koishiContextProvider,
......
import { App } from 'koishi'; import { App } from 'koishi';
import { import {
Inject,
Injectable, Injectable,
OnApplicationBootstrap, OnApplicationBootstrap,
OnModuleDestroy, OnModuleDestroy,
...@@ -12,14 +13,18 @@ import KoaRouter from '@koa/router'; ...@@ -12,14 +13,18 @@ import KoaRouter from '@koa/router';
import KoaBodyParser from 'koa-bodyparser'; import KoaBodyParser from 'koa-bodyparser';
import { KoishiMetascanService } from './providers/koishi-metascan.service'; import { KoishiMetascanService } from './providers/koishi-metascan.service';
import { applySelector } from './utility/koishi.utility'; import { applySelector } from './utility/koishi.utility';
import { KOISHI_MODULE_OPTIONS } from './utility/koishi.constants';
import { KoishiLoggerService } from './providers/koishi-logger.service';
@Injectable() @Injectable()
export class KoishiService export class KoishiService
extends App extends App
implements OnModuleInit, OnApplicationBootstrap, OnModuleDestroy { implements OnModuleInit, OnApplicationBootstrap, OnModuleDestroy {
constructor( constructor(
@Inject(KOISHI_MODULE_OPTIONS)
private readonly koishiModuleOptions: KoishiModuleOptions, private readonly koishiModuleOptions: KoishiModuleOptions,
private readonly metascan: KoishiMetascanService, private readonly metascan: KoishiMetascanService,
private readonly koishiLogger: KoishiLoggerService,
) { ) {
super({ super({
...koishiModuleOptions, ...koishiModuleOptions,
...@@ -29,22 +34,20 @@ export class KoishiService ...@@ -29,22 +34,20 @@ export class KoishiService
this._nestKoaTmpInstance.use(this.router.routes()); this._nestKoaTmpInstance.use(this.router.routes());
this._nestKoaTmpInstance.use(this.router.allowedMethods()); this._nestKoaTmpInstance.use(this.router.allowedMethods());
this._nestKoaTmpInstance.use(KoaBodyParser()); this._nestKoaTmpInstance.use(KoaBodyParser());
this.options.port = 1;
} }
_nestKoaTmpInstance = new Koa(); readonly _nestKoaTmpInstance = new Koa();
_nestKoaTmpServer: Server;
_nestKoaTmpServerPort: number;
private async setHttpServer() { private async setHttpServer() {
const httpAdapter = this.metascan.getHttpAdapter(); const httpAdapter = this.metascan.getHttpAdapter();
if (httpAdapter) { if (!httpAdapter) {
const httpServer: Server = httpAdapter.getHttpServer(); return;
if (httpServer instanceof Server) { }
this.logger('app').info('App using Nest HTTP Server.'); const httpServer: Server = httpAdapter.getHttpServer();
this._httpServer = httpServer; if (httpServer instanceof Server) {
} this.logger('app').info('App using Nest HTTP Server.');
} else { this._httpServer = httpServer;
this._httpServer = this._nestKoaTmpServer;
} }
} }
...@@ -66,8 +69,5 @@ export class KoishiService ...@@ -66,8 +69,5 @@ export class KoishiService
async onModuleDestroy() { async onModuleDestroy() {
await this.stop(); await this.stop();
if (this._nestKoaTmpServer) {
this._nestKoaTmpServer.close();
}
} }
} }
import { Injectable, NestMiddleware, OnModuleInit } from '@nestjs/common'; import { Injectable, NestMiddleware, OnModuleInit } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express'; import { NextFunction, Request, Response } from 'express';
import { KoishiService } from '../koishi.service'; import { KoishiService } from '../koishi.service';
import { createProxyMiddleware, RequestHandler } from 'http-proxy-middleware'; import { IncomingMessage, ServerResponse } from 'http';
import { Http2ServerRequest, Http2ServerResponse } from 'http2';
@Injectable() @Injectable()
export class KoishiMiddleware export class KoishiMiddleware implements NestMiddleware<Request, Response> {
implements NestMiddleware<Request, Response>, OnModuleInit { private readonly koaCallback: (
constructor(private koishi: KoishiService) {} req: IncomingMessage | Http2ServerRequest,
res: ServerResponse | Http2ServerResponse,
private proxyMiddleware: RequestHandler; ) => void;
constructor(private koishi: KoishiService) {
async onModuleInit() { this.koaCallback = this.koishi._nestKoaTmpInstance.callback();
this.proxyMiddleware = createProxyMiddleware({
target: `http://localhost:${this.koishi._nestKoaTmpServerPort}`,
ws: false,
logLevel: 'silent',
});
} }
use(req: Request, res: Response, next: NextFunction) { 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) { if (!match.route) {
return next(); 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