From a0c07b9e3cc2eec7e88e49085f1e66242fa1ec50 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Sat, 26 Dec 2020 23:33:50 +0800 Subject: [PATCH] feat: support https config for web/koa/express (#742) --- packages/web-express/src/framework.ts | 35 +++++++++++++++++++++- packages/web-express/src/interface.ts | 15 ++++++++++ packages/web-koa/src/framework.ts | 35 +++++++++++++++++++++- packages/web-koa/src/interface.ts | 15 ++++++++++ packages/web/src/devFramework.ts | 42 +++++++++++++++++++++++++-- 5 files changed, 138 insertions(+), 4 deletions(-) diff --git a/packages/web-express/src/framework.ts b/packages/web-express/src/framework.ts index 2d0e2527b472..46cadf36a7b4 100644 --- a/packages/web-express/src/framework.ts +++ b/packages/web-express/src/framework.ts @@ -35,6 +35,8 @@ import { } from './interface'; import type { IRouter, IRouterHandler, RequestHandler } from 'express'; import * as express from 'express'; +import { readFileSync } from 'fs'; +import { Server } from 'net'; export class MidwayExpressFramework extends BaseFramework< IMidwayExpressApplication, @@ -47,6 +49,7 @@ export class MidwayExpressFramework extends BaseFramework< router: IRouter; prefix: string; }> = []; + private server: Server; public configure( options: IMidwayExpressConfigurationOptions @@ -87,9 +90,35 @@ export class MidwayExpressFramework extends BaseFramework< } public async run(): Promise { + // https config + if (this.configurationOptions.key && this.configurationOptions.cert) { + this.configurationOptions.key = + typeof this.configurationOptions.key === 'string' + ? readFileSync(this.configurationOptions.key as string) + : this.configurationOptions.key; + + this.configurationOptions.cert = + typeof this.configurationOptions.cert === 'string' + ? readFileSync(this.configurationOptions.cert as string) + : this.configurationOptions.cert; + + this.configurationOptions.ca = + this.configurationOptions.ca && + (typeof this.configurationOptions.ca === 'string' + ? readFileSync(this.configurationOptions.ca) + : this.configurationOptions.ca); + + this.server = require('https').createServer( + this.configurationOptions, + this.app + ); + } else { + this.server = require('http').createServer(this.app); + } + if (this.configurationOptions.port) { new Promise(resolve => { - this.app.listen(this.configurationOptions.port, () => { + this.server.listen(this.configurationOptions.port, () => { resolve(); }); }); @@ -313,4 +342,8 @@ export class MidwayExpressFramework extends BaseFramework< } } } + + public getServer() { + return this.server; + } } diff --git a/packages/web-express/src/interface.ts b/packages/web-express/src/interface.ts index 02d3a03ba576..b369474fe508 100644 --- a/packages/web-express/src/interface.ts +++ b/packages/web-express/src/interface.ts @@ -27,7 +27,22 @@ export type IMidwayExpressApplication = IMidwayApplication & Application & { }; export interface IMidwayExpressConfigurationOptions { + /** + * application http port + */ port?: number; + /** + * https key + */ + key?: string | Buffer | Array; + /** + * https cert + */ + cert?: string | Buffer | Array; + /** + * https ca + */ + ca?: string | Buffer | Array; } export type MiddlewareParamArray = RequestHandler[]; diff --git a/packages/web-koa/src/framework.ts b/packages/web-koa/src/framework.ts index e13df4bca8e7..2babe91c9543 100644 --- a/packages/web-koa/src/framework.ts +++ b/packages/web-koa/src/framework.ts @@ -37,6 +37,8 @@ import { import * as Router from 'koa-router'; import type { DefaultState, Middleware } from 'koa'; import * as koa from 'koa'; +import { readFileSync } from 'fs'; +import { Server } from 'net'; export abstract class MidwayKoaBaseFramework< T, @@ -274,6 +276,8 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework< IMidwayKoaApplication, IMidwayKoaContext > { + private server: Server; + public configure( options: IMidwayKoaConfigurationOptions ): MidwayKoaFramework { @@ -313,9 +317,34 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework< } public async run(): Promise { + // https config + if (this.configurationOptions.key && this.configurationOptions.cert) { + this.configurationOptions.key = + typeof this.configurationOptions.key === 'string' + ? readFileSync(this.configurationOptions.key as string) + : this.configurationOptions.key; + + this.configurationOptions.cert = + typeof this.configurationOptions.cert === 'string' + ? readFileSync(this.configurationOptions.cert as string) + : this.configurationOptions.cert; + + this.configurationOptions.ca = + this.configurationOptions.ca && + (typeof this.configurationOptions.ca === 'string' + ? readFileSync(this.configurationOptions.ca) + : this.configurationOptions.ca); + + this.server = require('https').createServer( + this.configurationOptions, + this.app.callback() + ); + } else { + this.server = require('http').createServer(this.app.callback()); + } if (this.configurationOptions.port) { new Promise(resolve => { - this.app.listen(this.configurationOptions.port, () => { + this.server.listen(this.configurationOptions.port, () => { resolve(); }); }); @@ -325,4 +354,8 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework< public getFrameworkType(): MidwayFrameworkType { return MidwayFrameworkType.WEB_KOA; } + + public getServer() { + return this.server; + } } diff --git a/packages/web-koa/src/interface.ts b/packages/web-koa/src/interface.ts index ce3988f222f1..90d0aec4623c 100644 --- a/packages/web-koa/src/interface.ts +++ b/packages/web-koa/src/interface.ts @@ -20,7 +20,22 @@ export interface IMidwayKoaApplicationPlus { } export interface IMidwayKoaConfigurationOptions { + /** + * application http port + */ port?: number; + /** + * https key + */ + key?: string | Buffer | Array; + /** + * https cert + */ + cert?: string | Buffer | Array; + /** + * https ca + */ + ca?: string | Buffer | Array; } export type MiddlewareParamArray = Array>; diff --git a/packages/web/src/devFramework.ts b/packages/web/src/devFramework.ts index 06adfac178f0..eb23bcbdb9ca 100644 --- a/packages/web/src/devFramework.ts +++ b/packages/web/src/devFramework.ts @@ -7,12 +7,15 @@ import { import { IMidwayWebConfigurationOptions } from './interface'; import { Application } from 'egg'; import { resolve } from 'path'; +import { readFileSync } from 'fs'; +import { Server } from 'net'; export class MidwayDevFramework implements IMidwayFramework { public app: Application; public configurationOptions: IMidwayWebConfigurationOptions; - isTsMode: boolean; + private isTsMode: boolean; + private server: Server; public getApplication(): Application { return this.app; @@ -23,9 +26,40 @@ export class MidwayDevFramework } public async run(): Promise { + // https config + if (this.configurationOptions.key && this.configurationOptions.cert) { + this.configurationOptions.key = + typeof this.configurationOptions.key === 'string' + ? readFileSync(this.configurationOptions.key as string) + : this.configurationOptions.key; + + this.configurationOptions.cert = + typeof this.configurationOptions.cert === 'string' + ? readFileSync(this.configurationOptions.cert as string) + : this.configurationOptions.cert; + + this.configurationOptions.ca = + this.configurationOptions.ca && + (typeof this.configurationOptions.ca === 'string' + ? readFileSync(this.configurationOptions.ca) + : this.configurationOptions.ca); + + this.server = require('https').createServer( + this.configurationOptions, + this.app.callback() + ); + } else { + this.server = require('http').createServer(this.app.callback()); + } + + // emit `server` event in app + this.app.emit('server', this.server); + // trigger server didReady + this.app.messenger.emit('egg-ready'); + if (this.configurationOptions.port) { new Promise(resolve => { - this.app.listen(this.configurationOptions.port, () => { + this.server.listen(this.configurationOptions.port, () => { resolve(); }); }); @@ -64,4 +98,8 @@ export class MidwayDevFramework async stop(): Promise { await this.app.close(); } + + public getServer() { + return this.server; + } }