Skip to content

Commit

Permalink
feat: add http2 support (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Sep 5, 2021
1 parent 59d0ee7 commit e1eab02
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/web-express/package.json
Expand Up @@ -38,5 +38,8 @@
"type": "git",
"url": "http://github.com/midwayjs/midway.git"
},
"engines": {
"node": ">=10.10.0"
},
"gitHead": "a603d2348d6141f8f723901498f03a162a037708"
}
21 changes: 16 additions & 5 deletions packages/web-express/src/framework.ts
Expand Up @@ -72,12 +72,23 @@ export class MidwayExpressFramework extends BaseFramework<
this.configurationOptions.ca
);

this.server = require('https').createServer(
this.configurationOptions,
this.app
);
if (this.configurationOptions.http2) {
this.server = require('http2').createSecureServer(
this.configurationOptions,
this.app
);
} else {
this.server = require('https').createServer(
this.configurationOptions,
this.app
);
}
} else {
this.server = require('http').createServer(this.app);
if (this.configurationOptions.http2) {
this.server = require('http2').createServer(this.app);
} else {
this.server = require('http').createServer(this.app);
}
}
// register httpServer to applicationContext
this.applicationContext.registerObject(HTTP_SERVER_KEY, this.server);
Expand Down
4 changes: 4 additions & 0 deletions packages/web-express/src/interface.ts
Expand Up @@ -48,6 +48,10 @@ export interface IMidwayExpressConfigurationOptions extends IConfigurationOption
* https ca
*/
ca?: string | Buffer | Array<string | Buffer>;
/**
* http2 support
*/
http2?: boolean;
}

export type MiddlewareParamArray = RequestHandler[];
Expand Down
3 changes: 3 additions & 0 deletions packages/web-koa/package.json
Expand Up @@ -42,5 +42,8 @@
"type": "git",
"url": "http://github.com/midwayjs/midway.git"
},
"engines": {
"node": ">=10.10.0"
},
"gitHead": "a603d2348d6141f8f723901498f03a162a037708"
}
21 changes: 16 additions & 5 deletions packages/web-koa/src/framework.ts
Expand Up @@ -262,12 +262,23 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework<
this.configurationOptions.ca
);

this.server = require('https').createServer(
this.configurationOptions,
this.app.callback()
);
if (this.configurationOptions.http2) {
this.server = require('http2').createSecureServer(
this.configurationOptions,
this.app.callback()
);
} else {
this.server = require('https').createServer(
this.configurationOptions,
this.app.callback()
);
}
} else {
this.server = require('http').createServer(this.app.callback());
if (this.configurationOptions.http2) {
this.server = require('http2').createServer(this.app.callback());
} else {
this.server = require('http').createServer(this.app.callback());
}
}
// register httpServer to applicationContext
this.applicationContext.registerObject(HTTP_SERVER_KEY, this.server);
Expand Down
4 changes: 4 additions & 0 deletions packages/web-koa/src/interface.ts
Expand Up @@ -40,6 +40,10 @@ export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
* https ca
*/
ca?: string | Buffer | Array<string | Buffer>;
/**
* http2 support
*/
http2?: boolean;
}

export type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;
Expand Down
3 changes: 3 additions & 0 deletions packages/web/package.json
Expand Up @@ -59,5 +59,8 @@
"type": "git",
"url": "http://github.com/midwayjs/midway.git"
},
"engines": {
"node": ">=10.10.0"
},
"gitHead": "a603d2348d6141f8f723901498f03a162a037708"
}
21 changes: 16 additions & 5 deletions packages/web/src/framework/singleProcess.ts
Expand Up @@ -101,12 +101,23 @@ export class MidwayWebSingleProcessFramework
this.configurationOptions.ca
);

this.server = require('https').createServer(
this.configurationOptions,
this.app.callback()
);
if (this.configurationOptions.http2) {
this.server = require('http2').createSecureServer(
this.configurationOptions,
this.app.callback()
);
} else {
this.server = require('https').createServer(
this.configurationOptions,
this.app.callback()
);
}
} else {
this.server = require('http').createServer(this.app.callback());
if (this.configurationOptions.http2) {
this.server = require('http2').createServer(this.app.callback());
} else {
this.server = require('http').createServer(this.app.callback());
}
}

if (options.isMainFramework === undefined) {
Expand Down

0 comments on commit e1eab02

Please sign in to comment.