11import { join } from 'path' ;
22import { traverseAppRoutes } from '../routerPlugins/traverseAppRoutesPlugin' ;
33import { scullyConfig } from './config' ;
4- import { log , logError , yellow } from './log' ;
4+ import { log , logError , red , yellow } from './log' ;
5+ import { ssl , sslCert , sslKey } from '../utils/cli-options' ;
6+ import { readFileSync } from 'fs' ;
7+
58const express = require ( 'express' ) ;
9+ const https = require ( 'https' ) ;
10+ const selfsigned = require ( 'selfsigned' ) ;
611
712let angularServerInstance : { close : ( ) => void } ;
813let scullyServerInstance : { close : ( ) => void } ;
14+ let httpsServer ;
915
1016export async function staticServer ( port ?: number ) {
1117 try {
@@ -29,13 +35,56 @@ export async function staticServer(port?: number) {
2935 scullyServer . use ( express . static ( scullyConfig . outDir , options ) ) ;
3036 scullyServer . get ( '/' , ( req , res ) => res . sendFile ( join ( distFolder , '/index.html' ) ) ) ;
3137
32- scullyServerInstance = scullyServer . listen ( port , scullyConfig . hostName , x => {
33- log (
34- `Scully static server started on "${ yellow (
35- `http://${ scullyConfig . hostName } :${ scullyConfig . staticport } /`
36- ) } "`
38+ if ( ! ssl ) {
39+ scullyServerInstance = scullyServer . listen ( port , scullyConfig . hostName , x => {
40+ log (
41+ `Scully static server started on "${ yellow (
42+ `http://${ scullyConfig . hostName } :${ scullyConfig . staticport } /`
43+ ) } "`
44+ ) ;
45+ } ) ;
46+ } else {
47+ let pems = {
48+ private : '' ,
49+ cert : '' ,
50+ } ;
51+ if ( sslCert && sslKey ) {
52+ try {
53+ pems . private = readFileSync ( sslKey ) . toString ( ) ;
54+ pems . cert = readFileSync ( sslCert ) . toString ( ) ;
55+ } catch ( e ) {
56+ logError ( `Could not read the file: ${ e . path } ` ) ;
57+ log ( `${ yellow ( `Please check the path for the certificate.` ) } ` ) ;
58+ process . exit ( 0 ) ;
59+ }
60+ } else {
61+ const attrs = [
62+ {
63+ name : 'scully' ,
64+ value : `${ scullyConfig . hostName } :${ scullyConfig . staticport } ` ,
65+ type : 'RSAPublicKey' ,
66+ } ,
67+ ] ;
68+ pems = selfsigned . generate ( attrs , { days : 365 } ) ;
69+ console . log ( pems ) ;
70+ }
71+ // serve the API with signed certificate on 443 (SSL/HTTPS) port
72+ httpsServer = https . createServer (
73+ {
74+ key : pems . private ,
75+ cert : pems . cert ,
76+ } ,
77+ scullyServer
3778 ) ;
38- } ) ;
79+
80+ httpsServer . listen ( port , ( ) => {
81+ log (
82+ `Scully static server started on "${ yellow (
83+ `https://${ scullyConfig . hostName } :${ scullyConfig . staticport } /`
84+ ) } "`
85+ ) ;
86+ } ) ;
87+ }
3988
4089 const angularDistServer = express ( ) ;
4190 angularDistServer . get ( '/_pong' , ( req , res ) => {
@@ -79,4 +128,7 @@ export function closeExpress() {
79128 if ( angularServerInstance && angularServerInstance . close ) {
80129 angularServerInstance . close ( ) ;
81130 }
131+ if ( httpsServer ) {
132+ httpsServer . close ( ) ;
133+ }
82134}
0 commit comments