|
29 | 29 | */ |
30 | 30 |
|
31 | 31 | const fs = require('fs-extra'); |
| 32 | +const http = require('http'); |
| 33 | +const https = require('https'); |
32 | 34 | const path = require('path'); |
33 | 35 | const morgan = require('morgan'); |
34 | 36 | const express = require('express'); |
@@ -69,17 +71,21 @@ class Core extends CoreBase { |
69 | 71 |
|
70 | 72 | super(defaultConfiguration, deepmerge(cfg, argvConfig), options); |
71 | 73 |
|
72 | | - this.httpServer = null; |
73 | 74 | this.logger = consola.withTag('Internal'); |
74 | 75 | this.app = express(); |
75 | | - this.session = createSession(this.app, this.configuration); |
76 | | - this.ws = createWebsocket(this.app, this.configuration, this.session); |
77 | | - this.wss = this.ws.getWss(); |
78 | 76 |
|
79 | 77 | if (!this.configuration.public) { |
80 | 78 | throw new Error('The public option is required'); |
81 | 79 | } |
82 | 80 |
|
| 81 | + this.httpServer = this.config('https.enabled') |
| 82 | + ? https.createServer(this.config('https.options'), this.app) |
| 83 | + : http.createServer(this.app); |
| 84 | + |
| 85 | + this.session = createSession(this.app, this.configuration); |
| 86 | + this.ws = createWebsocket(this.app, this.configuration, this.session, this.httpServer); |
| 87 | + this.wss = this.ws.getWss(); |
| 88 | + |
83 | 89 | _instance = this; |
84 | 90 | } |
85 | 91 |
|
@@ -163,22 +169,27 @@ class Core extends CoreBase { |
163 | 169 | * Opens HTTP server |
164 | 170 | */ |
165 | 171 | listen() { |
166 | | - const wsp = this.configuration.ws.port ? this.configuration.ws.port : this.configuration.port; |
167 | | - const session = path.basename(path.dirname(this.configuration.session.store.module)); |
168 | | - const dist = this.configuration.public.replace(process.cwd(), ''); |
169 | | - |
170 | | - logger.info('Creating HTTP server'); |
171 | | - |
172 | | - const checkFile = path.join(this.configuration.public, this.configuration.index); |
| 172 | + const httpPort = this.config('port'); |
| 173 | + const wsPort = this.config('ws.port') || httpPort; |
| 174 | + const pub = this.config('public'); |
| 175 | + const session = path.basename(path.dirname(this.config('session.store.module'))); |
| 176 | + const dist = pub.replace(process.cwd(), ''); |
| 177 | + const secure = this.config('https.enabled', false); |
| 178 | + const proto = prefix => `${prefix}${secure ? 's' : ''}://`; |
| 179 | + const host = port => `${this.config('hostname')}:${port}`; |
| 180 | + |
| 181 | + logger.info('Opening server connection'); |
| 182 | + |
| 183 | + const checkFile = path.join(pub, this.configuration.index); |
173 | 184 | if (!fs.existsSync(checkFile)) { |
174 | 185 | logger.warn('Missing files in "dist/" directory. Did you forget to run "npm run build" ?'); |
175 | 186 | } |
176 | 187 |
|
177 | | - this.httpServer = this.app.listen(this.configuration.port, () => { |
178 | | - logger.success(`Server was started on ${this.configuration.hostname}:${this.configuration.port}`); |
179 | | - logger.success(`WebSocket is running on ${this.configuration.hostname}:${wsp}`); |
180 | | - logger.success(`Using ${session} sessions`); |
181 | | - logger.success(`Serving content from ${dist}`); |
| 188 | + this.httpServer.listen(httpPort, () => { |
| 189 | + logger.success(`Using '${session}' sessions`); |
| 190 | + logger.success(`Serving '${dist}'`); |
| 191 | + logger.success(`WebSocket listening on ${proto('ws')}${host(wsPort)}`); |
| 192 | + logger.success(`Server listening on ${proto('http')}${host(httpPort)}`); |
182 | 193 | }); |
183 | 194 | } |
184 | 195 |
|
|
0 commit comments