@@ -18,7 +18,7 @@ export class HttpServer implements Service {
18
18
private static PUBLIC_DIR = DEFAULT_STATIC_DIR ;
19
19
private static SERVE_STATIC = true ;
20
20
private servers : ServerAndPort [ ] = [ ] ;
21
- private app ?: Express ;
21
+ private mainApp ?: Express ;
22
22
23
23
protected constructor ( ) {
24
24
// nothing here
@@ -58,13 +58,13 @@ export class HttpServer implements Service {
58
58
}
59
59
60
60
public start ( ) : void {
61
- this . app = express ( ) ;
61
+ this . mainApp = express ( ) ;
62
62
if ( HttpServer . SERVE_STATIC && HttpServer . PUBLIC_DIR ) {
63
- this . app . use ( express . static ( HttpServer . PUBLIC_DIR ) ) ;
63
+ this . mainApp . use ( express . static ( HttpServer . PUBLIC_DIR ) ) ;
64
64
}
65
65
const config = Config . getInstance ( ) ;
66
66
config . getServers ( ) . forEach ( ( serverItem ) => {
67
- const { secure, port } = serverItem ;
67
+ const { secure, port, redirectToSecure } = serverItem ;
68
68
let proto : string ;
69
69
let server : http . Server | https . Server ;
70
70
if ( secure ) {
@@ -86,12 +86,37 @@ export class HttpServer implements Service {
86
86
cert = config . readFile ( certPath ) ;
87
87
}
88
88
const options = { ...serverItem . options , cert, key } ;
89
- server = https . createServer ( options , this . app ) ;
89
+ server = https . createServer ( options , this . mainApp ) ;
90
90
proto = 'https' ;
91
91
} else {
92
92
const options = serverItem . options ? { ...serverItem . options } : { } ;
93
- server = http . createServer ( options , this . app ) ;
94
93
proto = 'http' ;
94
+ let currentApp = this . mainApp ;
95
+ let host = '' ;
96
+ let port = 443 ;
97
+ let doRedirect = false ;
98
+ if ( redirectToSecure === true ) {
99
+ doRedirect = true ;
100
+ } else if ( typeof redirectToSecure === 'object' ) {
101
+ doRedirect = true ;
102
+ if ( typeof redirectToSecure . port === 'number' ) {
103
+ port = redirectToSecure . port ;
104
+ }
105
+ if ( typeof redirectToSecure . host === 'string' ) {
106
+ host = redirectToSecure . host ;
107
+ }
108
+ }
109
+ if ( doRedirect ) {
110
+ currentApp = express ( ) ;
111
+ currentApp . use ( function ( req , res ) {
112
+ const url = new URL ( `https://${ host ? host : req . headers . host } ${ req . url } ` ) ;
113
+ if ( port && port !== 443 ) {
114
+ url . port = port . toString ( ) ;
115
+ }
116
+ return res . redirect ( 301 , url . toString ( ) ) ;
117
+ } ) ;
118
+ }
119
+ server = http . createServer ( options , currentApp ) ;
95
120
}
96
121
this . servers . push ( { server, port } ) ;
97
122
server . listen ( port , ( ) => {
0 commit comments