Skip to content

Commit 8f5e50d

Browse files
committed
fix(serve): make the local enforce https provide http and https server.
- move the run server method from app to server class. - make the local enforce https provide http and https server. - update app test cases.
1 parent 0e5c669 commit 8f5e50d

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

packages/integration-testing/src/example1/buildAndServe.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ it('Example1: Build and serve should work', async () => {
5959
const builder = new VulcanBuilder(projectConfig);
6060
await builder.build();
6161
server = new VulcanServer(projectConfig);
62-
const httpServer = await server.start(3000);
62+
const httpServer = (await server.start(3000))['http'];
6363

6464
const agent = supertest(httpServer);
6565
const result = await agent.get(

packages/serve/src/lib/server.ts

+38-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isEmpty } from 'lodash';
12
import * as fs from 'fs';
23
import * as http from 'http';
34
import * as https from 'https';
@@ -13,15 +14,21 @@ import { getEnforceHttpsOptions } from './middleware';
1314
export class VulcanServer {
1415
private config: ServeConfig;
1516
private container: Container;
16-
private server?: http.Server;
17-
17+
private servers?: {
18+
http: http.Server;
19+
https?: https.Server;
20+
};
1821
constructor(config: ServeConfig) {
1922
this.config = config;
2023
this.container = new Container();
2124
}
22-
23-
public async start(port = 3000) {
24-
if (this.server)
25+
/**
26+
* Start the vulcan server
27+
* @param port the http port for server start, default is 3000
28+
* @param httpsPort the https port for https server start when you set "type" = LOCAL in "enforce-https" middleware and provide ssl file, default port is 3001
29+
*/
30+
public async start(port = 3000, httpsPort = 3001) {
31+
if (!isEmpty(this.servers))
2532
throw new Error('Server has created, please close it first.');
2633

2734
// Load container
@@ -47,24 +54,40 @@ export class VulcanServer {
4754
await app.useMiddleware();
4855
await app.buildRoutes(schemas, this.config['types']);
4956
// Run server
50-
this.server = this.runServer(app, port);
51-
return this.server;
57+
this.servers = this.runServer(app, port, httpsPort);
58+
return this.servers;
59+
}
60+
public async close() {
61+
if (this.servers) {
62+
if (this.servers['http']) this.servers['http'].close();
63+
if (this.servers['https']) this.servers['https'].close();
64+
this.servers = undefined;
65+
}
66+
this.container.unload();
5267
}
5368

54-
public runServer(app: VulcanApplication, port: number) {
69+
/**
70+
* Run server for https when config has setup ssl and middleware 'enforce-https' enabled with LOCAL type, or keep http
71+
*/
72+
private runServer(app: VulcanApplication, port: number, httpsPort: number) {
5573
const options = getEnforceHttpsOptions(this.config['enforce-https']);
5674
if (options && this.config.ssl) {
5775
const options = {
5876
key: fs.readFileSync(this.config.ssl.keyFile),
5977
cert: fs.readFileSync(this.config.ssl.certFile),
6078
};
61-
return https.createServer(options, app.getHandler()).listen(port);
62-
}
63-
return http.createServer(app.getHandler()).listen(port);
64-
}
6579

66-
public async close() {
67-
if (this.server) this.server.close();
68-
this.container.unload();
80+
const httpServer = http.createServer(app.getHandler()).listen(port);
81+
const httpsServer = https
82+
.createServer(options, app.getHandler())
83+
.listen(httpsPort);
84+
return {
85+
http: httpServer,
86+
https: httpsServer,
87+
};
88+
}
89+
return {
90+
http: http.createServer(app.getHandler()).listen(port),
91+
};
6992
}
7093
}

0 commit comments

Comments
 (0)