1
- import { readFileSync } from 'fs ' ;
1
+ import express from 'express ' ;
2
2
import { join } from 'path' ;
3
3
import { traverseAppRoutes } from '../routerPlugins/traverseAppRoutesPlugin' ;
4
- import { ssl , sslCert , sslKey , tds } from '../utils/cli-options' ;
4
+ import { ssl , tds } from '../utils/cli-options' ;
5
+ import { addSSL } from './addSSL' ;
5
6
import { scullyConfig } from './config' ;
6
- import { log , logError , yellow } from './log' ;
7
7
import { startDataServer } from './dataServer' ;
8
+ import { log , logError , yellow } from './log' ;
8
9
import { proxyAdd } from './proxyAdd' ;
9
10
10
- const express = require ( 'express' ) ;
11
- const https = require ( 'https' ) ;
12
- const selfsigned = require ( 'selfsigned' ) ;
13
-
14
11
let angularServerInstance : { close : ( ) => void } ;
15
12
let scullyServerInstance : { close : ( ) => void } ;
16
13
let dataServerInstance : { close : ( ) => void } ;
17
- let httpsServer ;
18
14
19
15
export async function staticServer ( port ?: number ) {
20
16
try {
21
17
port = port || scullyConfig . staticport ;
18
+ const hostName = scullyConfig . hostName ;
22
19
const routes = await traverseAppRoutes ( ) ;
23
20
const scullyServer = express ( ) ;
24
21
const distFolder = join ( scullyConfig . homeFolder , scullyConfig . distFolder ) ;
@@ -44,56 +41,9 @@ export async function staticServer(port?: number) {
44
41
scullyServer . use ( express . static ( scullyConfig . outDir , options ) ) ;
45
42
scullyServer . get ( '/' , ( req , res ) => res . sendFile ( join ( distFolder , '/index.html' ) ) ) ;
46
43
47
- if ( ! ssl ) {
48
- scullyServerInstance = scullyServer . listen ( port , scullyConfig . hostName , x => {
49
- log (
50
- `Scully static server started on "${ yellow (
51
- `http://${ scullyConfig . hostName } :${ scullyConfig . staticport } /`
52
- ) } "`
53
- ) ;
54
- } ) ;
55
- } else {
56
- let pems = {
57
- private : '' ,
58
- cert : '' ,
59
- } ;
60
- if ( sslCert && sslKey ) {
61
- try {
62
- pems . private = readFileSync ( sslKey ) . toString ( ) ;
63
- pems . cert = readFileSync ( sslCert ) . toString ( ) ;
64
- } catch ( e ) {
65
- logError ( `Could not read the file: ${ e . path } ` ) ;
66
- log ( `${ yellow ( `Please check the path for the certificate.` ) } ` ) ;
67
- process . exit ( 0 ) ;
68
- }
69
- } else {
70
- const attrs = [
71
- {
72
- name : 'scully' ,
73
- value : `${ scullyConfig . hostName } :${ scullyConfig . staticport } ` ,
74
- type : 'RSAPublicKey' ,
75
- } ,
76
- ] ;
77
- pems = selfsigned . generate ( attrs , { days : 365 } ) ;
78
- console . log ( pems ) ;
79
- }
80
- // serve the API with signed certificate on 443 (SSL/HTTPS) port
81
- httpsServer = https . createServer (
82
- {
83
- key : pems . private ,
84
- cert : pems . cert ,
85
- } ,
86
- scullyServer
87
- ) ;
88
-
89
- httpsServer . listen ( port , ( ) => {
90
- log (
91
- `Scully static server started on "${ yellow (
92
- `https://${ scullyConfig . hostName } :${ scullyConfig . staticport } /`
93
- ) } "`
94
- ) ;
95
- } ) ;
96
- }
44
+ scullyServerInstance = addSSL ( scullyServer , hostName , port ) . listen ( port , hostName , x => {
45
+ log ( `Scully static server started on "${ yellow ( `http${ ssl ? 's' : '' } ://${ hostName } :${ port } /` ) } "` ) ;
46
+ } ) ;
97
47
98
48
const angularDistServer = express ( ) ;
99
49
proxyAdd ( angularDistServer ) ;
@@ -119,13 +69,17 @@ export async function staticServer(port?: number) {
119
69
* // angularDistServer.get('/*', (req, res) => res.sendFile(join(scullyConfig.outDir, '/index.html')));
120
70
* we are already serving all known routes an index.html. at this point a 404 is indeed just a 404, don't substitute.
121
71
*/
122
- angularServerInstance = angularDistServer . listen ( scullyConfig . appPort , scullyConfig . hostName , x => {
123
- log (
124
- `Angular distribution server started on "${ yellow (
125
- `http://${ scullyConfig . hostName } :${ scullyConfig . appPort } /`
126
- ) } " `
127
- ) ;
128
- } ) ;
72
+ angularServerInstance = addSSL ( angularDistServer , hostName , scullyConfig . appPort ) . listen (
73
+ scullyConfig . appPort ,
74
+ hostName ,
75
+ x => {
76
+ log (
77
+ `Angular distribution server started on "${ yellow (
78
+ `http${ ssl ? 's' : '' } ://${ hostName } :${ scullyConfig . appPort } /`
79
+ ) } " `
80
+ ) ;
81
+ }
82
+ ) ;
129
83
} catch ( e ) {
130
84
logError ( `Could not start Scully serve` , e ) ;
131
85
}
@@ -138,9 +92,6 @@ export function closeExpress() {
138
92
if ( angularServerInstance && angularServerInstance . close ) {
139
93
angularServerInstance . close ( ) ;
140
94
}
141
- if ( httpsServer ) {
142
- httpsServer . close ( ) ;
143
- }
144
95
if ( dataServerInstance && dataServerInstance . close ) {
145
96
dataServerInstance . close ( ) ;
146
97
}
0 commit comments