Created
February 26, 2016 07:18
-
-
Save syuilo/515fee99e837a4194404 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////// | |
// WEB SERVER | |
////////////////////////////////////////////////// | |
import * as cluster from 'cluster'; | |
import * as fs from 'fs'; | |
import * as http from 'http'; | |
import * as https from 'https'; | |
import * as koa from 'koa'; | |
import config from './config'; | |
const worker = cluster.worker; | |
console.log(`Initializing ${worker.id} server...`); | |
////////////////////////////////////////////////// | |
// INIT SERVER PHASE | |
const app = new koa(); | |
app.use(async (ctx) => { | |
ctx.body = 'Hello world'; | |
}); | |
////////////////////////////////////////////////// | |
// LISTEN PHASE | |
let server: http.Server | https.Server; | |
let port: number; | |
if (config.https.enable) { | |
port = config.port.https; | |
server = https.createServer({ | |
key: fs.readFileSync(config.https.keyPath), | |
cert: fs.readFileSync(config.https.certPath) | |
}, app.callback()); | |
} else { | |
port = config.port.http; | |
server = http.createServer(app.callback()); | |
} | |
server.listen(port, () => { | |
const host = server.address().address; | |
const port = server.address().port; | |
console.log( | |
`\u001b[1;32m${worker.id} is now listening at ${host}:${port}\u001b[0m`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment