diff --git a/packages/cli/config/index.ts b/packages/cli/config/index.ts index 0609bdae31..ede5a1cdce 100644 --- a/packages/cli/config/index.ts +++ b/packages/cli/config/index.ts @@ -175,6 +175,18 @@ const config = convict({ env: 'N8N_PROTOCOL', doc: 'HTTP Protocol via which n8n can be reached' }, + ssl_key: { + format: String, + default: 'server.key', + env: 'N8N_SSL_KEY', + doc: 'SSL Key for HTTPS Protocol' + }, + ssl_cert: { + format: String, + default: 'server.pem', + env: 'N8N_SSL_CERT', + doc: 'SSL Cert for HTTPS Protocol' + }, security: { basicAuth: { @@ -276,10 +288,10 @@ const config = convict({ // Overwrite default configuration with settings which got defined in // optional configuration files if (process.env.N8N_CONFIG_FILES !== undefined) { - const configFiles = process.env.N8N_CONFIG_FILES.split(','); - console.log(`\nLoading configuration overwrites from:\n - ${configFiles.join('\n - ')}\n`); + const configFiles = process.env.N8N_CONFIG_FILES.split(','); + console.log(`\nLoading configuration overwrites from:\n - ${configFiles.join('\n - ')}\n`); - config.loadFile(configFiles); + config.loadFile(configFiles); } config.validate({ diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 2f539c6f5c..302da164e4 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -97,6 +97,10 @@ class App { push: Push.Push; versions: IPackageVersions | undefined; + protocol: string; + ssl_key: string; + ssl_cert: string; + constructor() { this.app = express(); @@ -112,6 +116,10 @@ class App { this.push = Push.getInstance(); this.activeExecutionsInstance = ActiveExecutions.getInstance(); + + this.protocol = config.get('protocol'); + this.ssl_key = config.get('ssl_key'); + this.ssl_cert = config.get('ssl_cert'); } @@ -1254,8 +1262,23 @@ export async function start(): Promise { const app = new App(); await app.config(); + + // Differntiate HTTP and HTTPS server + const http = require('http'); + const https = require('https'); + const fs = require('fs'); + var server; - app.app.listen(PORT, async () => { + if(app.protocol === 'https'){ + const privateKey = fs.readFileSync(app.ssl_key,'utf8'); + const cert = fs.readFileSync(app.ssl_cert,'utf8'); + const credentials = {key: privateKey,cert: cert}; + server = https.createServer(credentials,app.app); + }else{ + server = http.createServer(app.app); + } + + server.listen(PORT, async () => { const versions = await GenericHelpers.getVersions(); console.log(`n8n ready on port ${PORT}`); console.log(`Version: ${versions.cli}`);