Skip to content

Commit

Permalink
feat: support https config for web/koa/express (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Dec 26, 2020
1 parent 568379f commit a0c07b9
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 4 deletions.
35 changes: 34 additions & 1 deletion packages/web-express/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,6 +49,7 @@ export class MidwayExpressFramework extends BaseFramework<
router: IRouter;
prefix: string;
}> = [];
private server: Server;

public configure(
options: IMidwayExpressConfigurationOptions
Expand Down Expand Up @@ -87,9 +90,35 @@ export class MidwayExpressFramework extends BaseFramework<
}

public async run(): Promise<void> {
// 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();
});
});
Expand Down Expand Up @@ -313,4 +342,8 @@ export class MidwayExpressFramework extends BaseFramework<
}
}
}

public getServer() {
return this.server;
}
}
15 changes: 15 additions & 0 deletions packages/web-express/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,22 @@ export type IMidwayExpressApplication = IMidwayApplication & Application & {
};

export interface IMidwayExpressConfigurationOptions {
/**
* application http port
*/
port?: number;
/**
* https key
*/
key?: string | Buffer | Array<Buffer | Object>;
/**
* https cert
*/
cert?: string | Buffer | Array<string | Buffer>;
/**
* https ca
*/
ca?: string | Buffer | Array<string | Buffer>;
}

export type MiddlewareParamArray = RequestHandler[];
Expand Down
35 changes: 34 additions & 1 deletion packages/web-koa/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -274,6 +276,8 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework<
IMidwayKoaApplication,
IMidwayKoaContext
> {
private server: Server;

public configure(
options: IMidwayKoaConfigurationOptions
): MidwayKoaFramework {
Expand Down Expand Up @@ -313,9 +317,34 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework<
}

public async run(): Promise<void> {
// 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();
});
});
Expand All @@ -325,4 +354,8 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework<
public getFrameworkType(): MidwayFrameworkType {
return MidwayFrameworkType.WEB_KOA;
}

public getServer() {
return this.server;
}
}
15 changes: 15 additions & 0 deletions packages/web-koa/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ export interface IMidwayKoaApplicationPlus {
}

export interface IMidwayKoaConfigurationOptions {
/**
* application http port
*/
port?: number;
/**
* https key
*/
key?: string | Buffer | Array<Buffer | Object>;
/**
* https cert
*/
cert?: string | Buffer | Array<string | Buffer>;
/**
* https ca
*/
ca?: string | Buffer | Array<string | Buffer>;
}

export type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;
Expand Down
42 changes: 40 additions & 2 deletions packages/web/src/devFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Application, IMidwayWebConfigurationOptions> {
public app: Application;
public configurationOptions: IMidwayWebConfigurationOptions;
isTsMode: boolean;
private isTsMode: boolean;
private server: Server;

public getApplication(): Application {
return this.app;
Expand All @@ -23,9 +26,40 @@ export class MidwayDevFramework
}

public async run(): Promise<void> {
// 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();
});
});
Expand Down Expand Up @@ -64,4 +98,8 @@ export class MidwayDevFramework
async stop(): Promise<void> {
await this.app.close();
}

public getServer() {
return this.server;
}
}

0 comments on commit a0c07b9

Please sign in to comment.