Skip to content

Commit

Permalink
support ssl/https mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdesl committed Mar 7, 2016
1 parent b26bd9a commit 0761529
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ budo src/index.js:static/bundle.js
# pass some options to browserify
budo index.js --live -- -t babelify

# set the header to use CORS (`Access-Control-Allow-Origin: *`)
budo index.js --live --cors
# use HTTPS and enable CORS headers
budo index.js --ssl --cors --cert=cert.pem --key=key.pem
```

Then open [http://localhost:9966/](http://localhost:9966/) to see the content in action.
Expand Down Expand Up @@ -91,6 +91,9 @@ Options:
--poll=N use polling for file watch, with optional interval N
--title optional title for default index.html
--css optional stylesheet href for default index.html
--ssl, -S create an HTTPS server instead of HTTP
--cert, -C the cert for SSL (default cert.pem)
--key, -K the key for SSL (default key.pem)
--cors set header to use CORS (Access-Control-Allow-Origin: *)
--ndjson print ndjson instead of pretty-printed logs
--verbose, -v also include debug messages
Expand Down
4 changes: 4 additions & 0 deletions bin/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Options:
--poll=N use polling for file watch, with optional interval N
--title optional title for default index.html
--css optional stylesheet href for default index.html
--ssl, -S create an HTTPS server instead of HTTP
--cert, -C the cert for SSL (default cert.pem)
--key, -K the key for SSL (default key.pem)
--cors set header to use CORS (Access-Control-Allow-Origin: *)
--ndjson print ndjson instead of pretty-printed logs
--verbose, -v also include debug messages
--no-stream do not print messages to stdout
Expand Down
6 changes: 6 additions & 0 deletions docs/api-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ All settings are optional.
will trigger LiveReload events
- `cors` (Boolean)
- Set the header to use CORS (`Access-Control-Allow-Origin: *`)
- `ssl` (Boolean)
- Creates an HTTPS server instead of HTTP
- `cert` (String)
- The SSL public certificate file path (default `'cert.pem'`)
- `key` (String)
- The SSL private key file path (default `'key.pem'`)
- `watchGlob` (Array|String)
- a glob string or array of glob strings to use as the default when `opts.live` is specified, or when `live()` is called without arguments
- defaults to `'**/*.{html,css}'`
Expand Down
3 changes: 2 additions & 1 deletion lib/budo.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ function createBudo (entries, opts) {
opts.host = getHostAddress(opts.host)

var port = opts.port
var uri = 'http://' + opts.host + ':' + port + '/'
var protocol = opts.ssl ? 'https' : 'http'
var uri = protocol + '://' + opts.host + ':' + port + '/'

log.info({ message: 'Server running at', url: uri, type: 'connect' })

Expand Down
10 changes: 8 additions & 2 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function parseArgs (args, opt) {
'portfind',
'ndjson',
'verbose',
'cors'
'cors',
'ssl'
],
string: [
'host',
Expand All @@ -21,11 +22,16 @@ function parseArgs (args, opt) {
'onupdate',
'serve',
'title',
'watchGlob'
'watchGlob',
'cert',
'key'
],
default: module.exports.defaults,
alias: {
port: 'p',
ssl: 'S',
cert: 'C',
key: 'K',
verbose: 'v',
help: 'h',
host: 'H',
Expand Down
12 changes: 10 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
// TODO: https support
var http = require('http')
var https = require('https')
var createMiddleware = require('./middleware')
var fs = require('fs')

module.exports = function createServer (entryMiddleware, opts) {
var httpsOpts = opts.ssl ? {
cert: fs.readFileSync(opts.cert || 'cert.pem'),
key: fs.readFileSync(opts.key || 'key.pem')
} : undefined

var handler = createMiddleware(entryMiddleware, opts)
var server = http.createServer(handler)
var server = httpsOpts
? https.createServer(httpsOpts, handler)
: http.createServer(handler)
server.setLiveOptions = handler.setLiveOptions
return server
}

1 comment on commit 0761529

@veggiemonk
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh Sweet! Nice job! 😂

Please sign in to comment.