Skip to content

Instantly share code, notes, and snippets.

@syuilo
Created February 26, 2016 07:18
Show Gist options
  • Save syuilo/515fee99e837a4194404 to your computer and use it in GitHub Desktop.
Save syuilo/515fee99e837a4194404 to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////
// 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