1 | {join} = require 'path'
|
2 | fs = require 'fs'
|
3 |
|
4 | http = require 'http'
|
5 | https = require 'https'
|
6 |
|
7 | read = (file) -> fs.readFileSync file, 'utf8'
|
8 |
|
9 | config =
|
10 | port: 4000
|
11 | ssl: false
|
12 |
|
13 | module.exports = (port, app, done) ->
|
14 | app ?= ->
|
15 | done ?= ->
|
16 |
|
17 | if config.ssl
|
18 |
|
19 |
|
20 | ca = config.ssl.ca || []
|
21 | options =
|
22 | key: read config.ssl.key
|
23 | cert: read config.ssl.cert
|
24 | ca: ca.map read
|
25 |
|
26 |
|
27 | server = https.createServer(options, app).listen port, done
|
28 |
|
29 |
|
30 | if (port is config.port) and config.ssl.redirectFrom?
|
31 | redirect = (req, res) ->
|
32 | redirectTarget = "https://#{req.headers.host}#{req.url}"
|
33 | res.writeHead 301, {
|
34 | "Location": redirectTarget
|
35 | }
|
36 | res.end()
|
37 | redirectServer = http.createServer(redirect).listen config.ssl.redirectFrom
|
38 |
|
39 | return server
|
40 |
|
41 | else
|
42 | http.createServer(app).listen port, done
|