Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HTTPS option for dev server #735

Merged
merged 7 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions dist/server/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/usr/bin/env node
'use strict';

var _server;

var _express = require('express');

var _express2 = _interopRequireDefault(_express);

var _https = require('https');

var _https2 = _interopRequireDefault(_https);

var _serveFavicon = require('serve-favicon');

var _serveFavicon2 = _interopRequireDefault(_serveFavicon);
Expand Down Expand Up @@ -47,7 +53,7 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var logger = console;

_commander2.default.version(_package2.default.version).option('-p, --port [number]', 'Port to run Storybook (Required)', parseInt).option('-h, --host [string]', 'Host to run Storybook').option('-s, --static-dir <dir-names>', 'Directory where to load static files from').option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from').option('--dont-track', 'Do not send anonymous usage stats.').option('-d, --db-path [db-file]', 'DEPRECATED!').option('--enable-db', 'DEPRECATED!').parse(process.argv);
_commander2.default.version(_package2.default.version).option('-p, --port [number]', 'Port to run Storybook (Required)', parseInt).option('-h, --host [string]', 'Host to run Storybook').option('-s, --static-dir <dir-names>', 'Directory where to load static files from').option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from').option('--dont-track', 'Do not send anonymous usage stats.').option('--https', 'Serve Storybook over HTTPS. Note: You must provide your own certificate information.').option('--ssl-ca <ca>', 'Provide an SSL certificate authority. (Required with --https)').option('--ssl-cert <cert>', 'Provide an SSL certificate. (Required with --https)').option('--ssl-key <key>', 'Provide an SSL key. (Required with --https)').option('-d, --db-path [db-file]', 'DEPRECATED!').option('--enable-db', 'DEPRECATED!').parse(process.argv);

logger.info(_chalk2.default.bold(_package2.default.name + ' v' + _package2.default.version + '\n'));

Expand Down Expand Up @@ -84,6 +90,30 @@ if (_commander2.default.host) {
}

var app = (0, _express2.default)();
var server = app;

if (_commander2.default.https) {
if (!_commander2.default.sslCa) {
logger.error('Error: --ssl-ca is required with --https');
process.exit(-1);
}
if (!_commander2.default.sslCert) {
logger.error('Error: --ssl-cert is required with --https');
process.exit(-1);
}
if (!_commander2.default.sslKey) {
logger.error('Error: --ssl-key is required with --https');
process.exit(-1);
}

var sslOptions = {
ca: _fs2.default.readFileSync(_commander2.default.sslCa, 'utf-8'),
cert: _fs2.default.readFileSync(_commander2.default.sslCert, 'utf-8'),
key: _fs2.default.readFileSync(_commander2.default.sslKey, 'utf-8')
};

server = _https2.default.createServer(sslOptions, app);
}

var hasCustomFavicon = false;

Expand Down Expand Up @@ -126,11 +156,11 @@ process.env.STORYBOOK_GIT_BRANCH = process.env.STORYBOOK_GIT_BRANCH || exec('git
// `getBaseConfig` function which is called inside the middleware
app.use((0, _middleware2.default)(configDir));

app.listen.apply(app, listenAddr.concat([function (error) {
(_server = server).listen.apply(_server, listenAddr.concat([function (error) {
if (error) {
throw error;
} else {
var address = 'http://' + (_commander2.default.host || 'localhost') + ':' + _commander2.default.port + '/';
var address = 'http' + (_commander2.default.https ? 's' : '') + '://' + (_commander2.default.host || 'localhost') + ':' + _commander2.default.port + '/';
logger.info('\nReact Storybook started on => ' + _chalk2.default.cyan(address) + '\n');
(0, _track_usage.track)();
}
Expand Down
33 changes: 31 additions & 2 deletions src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import express from 'express';
import https from 'https';
import favicon from 'serve-favicon';
import program from 'commander';
import path from 'path';
Expand All @@ -23,6 +24,10 @@ program
.option('-s, --static-dir <dir-names>', 'Directory where to load static files from')
.option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from')
.option('--dont-track', 'Do not send anonymous usage stats.')
.option('--https', 'Serve Storybook over HTTPS. Note: You must provide your own certificate information.')
.option('--ssl-ca <ca>', 'Provide an SSL certificate authority. (Required with --https)')
.option('--ssl-cert <cert>', 'Provide an SSL certificate. (Required with --https)')
.option('--ssl-key <key>', 'Provide an SSL key. (Required with --https)')
.option('-d, --db-path [db-file]', 'DEPRECATED!')
.option('--enable-db', 'DEPRECATED!')
.parse(process.argv);
Expand Down Expand Up @@ -66,6 +71,30 @@ if (program.host) {
}

const app = express();
let server = app;

if (program.https) {
if (!program.sslCa) {
logger.error('Error: --ssl-ca is required with --https');
process.exit(-1);
}
if (!program.sslCert) {
logger.error('Error: --ssl-cert is required with --https');
process.exit(-1);
}
if (!program.sslKey) {
logger.error('Error: --ssl-key is required with --https');
process.exit(-1);
}

const sslOptions = {
ca: fs.readFileSync(program.sslCa, 'utf-8'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doctyper looking at the docs, it seems like ca should be an array, and also is only used for self-signed certificates?

https://nodejs.org/api/https.html
https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener

If it's straightforward can you provide simple instructions for me to test this?

cert: fs.readFileSync(program.sslCert, 'utf-8'),
key: fs.readFileSync(program.sslKey, 'utf-8'),
};

server = https.createServer(sslOptions, app);
}

let hasCustomFavicon = false;

Expand Down Expand Up @@ -106,11 +135,11 @@ process.env.STORYBOOK_GIT_BRANCH = process.env.STORYBOOK_GIT_BRANCH || exec('git
// `getBaseConfig` function which is called inside the middleware
app.use(storybook(configDir));

app.listen(...listenAddr, function (error) {
server.listen(...listenAddr, function (error) {
if (error) {
throw error;
} else {
const address = `http://${program.host || 'localhost'}:${program.port}/`;
const address = `http${program.https ? 's' : ''}://${program.host || 'localhost'}:${program.port}/`;
logger.info(`\nReact Storybook started on => ${chalk.cyan(address)}\n`);
track();
}
Expand Down