forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (72 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* eslint-disable no-console */
/* eslint-disable import/no-nodejs-modules */
require( '@babel/polyfill' );
/**
* External dependencies.
*/
const boot = require( 'boot' );
const chalk = require( 'chalk' );
const fs = require( 'fs' );
/**
* Internal dependencies
*/
const pkg = require( './package.json' );
const config = require( 'config' );
const start = Date.now();
const protocol = process.env.PROTOCOL || config( 'protocol' );
const port = process.env.PORT || config( 'port' );
const host = process.env.HOST || config( 'hostname' );
const app = boot();
function sendBootStatus( status ) {
// don't send anything if we're not running in a fork
if ( ! process.send ) {
return;
}
process.send( { boot: status } );
}
console.log(
chalk.yellow( '%s booted in %dms - %s://%s:%s' ),
pkg.name,
Date.now() - start,
protocol,
host,
port
);
function loadSslCert() {
const { execSync } = require( 'child_process' );
const execOptions = { encoding: 'utf-8', windowsHide: true };
let key = './config/server/key.pem';
let certificate = './config/server/certificate.pem';
if ( ! fs.existsSync( key ) || ! fs.existsSync( certificate ) ) {
try {
execSync( 'openssl version', execOptions );
execSync(
`openssl req -x509 -newkey rsa:2048 -keyout ./config/server/key.tmp.pem -out ${ certificate } -days 365 -nodes -subj "/C=US/ST=Foo/L=Bar/O=Baz/CN=calypso.localhost"`,
execOptions
);
execSync( `openssl rsa -in ./config/server/key.tmp.pem -out ${ key }`, execOptions );
execSync( 'rm ./config/server/key.tmp.pem', execOptions );
} catch ( error ) {
key = './config/server/key.default.pem';
certificate = './config/server/certificate.default.pem';
console.error( error );
}
}
return {
key: fs.readFileSync( key ),
cert: fs.readFileSync( certificate ),
};
}
// Start a development HTTPS server.
function createServer() {
if ( protocol === 'https' ) {
return require( 'https' ).createServer( loadSslCert(), app );
}
return require( 'http' ).createServer( app );
}
const server = createServer();
// The desktop app runs Calypso in a fork. Let non-forks listen on any host.
server.listen( { port, host: process.env.CALYPSO_IS_FORK ? host : null }, function() {
// Tell the parent process that Calypso has booted.
sendBootStatus( 'ready' );
} );