Last active
November 26, 2015 04:21
-
-
Save Gattermeier/dde9d7c49cfe8607e3b3 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
// Cluster | |
import cluster from 'cluster'; | |
const numCPUs = require('os').cpus().length; | |
// Hapi | |
import Hapi from 'hapi'; | |
import hapiReactViews from 'hapi-react-views'; | |
// Load our module configuration | |
import hapiModuleConfig from './hapi.modules'; | |
module.exports = function() { | |
if (cluster.isMaster) { | |
// For each available CPU we spawn a process | |
for (let i = 0; i < numCPUs; i++) { | |
cluster.fork(); | |
} | |
cluster.on('online', (worker) => { | |
console.log('Worker ' + worker.process.pid + ' is online, expecting: ' + numCPUs + ' workers.'); | |
}); | |
// Get a new worker if one dies | |
cluster.on('exit', (worker, code, signal) => { | |
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal); | |
console.log('Starting a new worker'); | |
cluster.fork(); | |
}); | |
} else if (cluster.isWorker) { | |
// Server connection for this worker | |
const serverConfig = { | |
host: '127.0.0.1', | |
port: 3000 | |
} | |
const server = new Hapi.Server(); | |
server.connection(serverConfig); | |
// Register Hapi Modules | |
// We are passing in the cluster object as we might want to have access to the worker information | |
server.register(hapiModuleConfig(cluster), (err) => { | |
if (err) throw err; | |
// Hapi Views with React as Engine | |
server.views({ | |
engines: { | |
jsx: hapiReactViews | |
}, | |
relativeTo: __dirname, | |
path: 'views' | |
}); | |
// Example for a catch-all route returning a reply | |
// & rendered with React template 'default' in views folder | |
server.route({ | |
method: 'GET', | |
path: '/{path*}', | |
handler: (request, reply) => { | |
return reply.view('default',{ | |
title: 'Node Hapi React Cluster in ES6', | |
message: 'Get the Repo on Github', | |
link: 'https://github.com/Gattermeier/Node-Hapi-React-Cluster-in-ES6' | |
}) | |
} | |
}) | |
}); | |
// Finally, let's fire up the server for this worker | |
server.start((err) => { | |
if (err) throw err; | |
console.log('HAPI server running as cluster worker with ID ' + cluster.worker.id + ' on:', serverConfig.host, ':', serverConfig.port); | |
}); | |
} | |
} |
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
'use strict'; | |
// wrapper to enable es6 nice-a-nice in node app | |
require('babel-core/register')({ | |
presets: ['react', 'es2015', 'stage-1'] | |
}); | |
const app = require('./app.es6.js'); | |
app(); |
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
import React from 'react'; | |
class Component extends React.Component { | |
static propTypes = { | |
title: React.PropTypes.string, | |
message: React.PropTypes.string, | |
link: React.PropTypes.string | |
} | |
static defaultProps = { | |
title: '', | |
message: '', | |
link: '' | |
} | |
constructor() { | |
super() | |
} | |
render() { | |
return ( | |
<div> | |
<p>{this.props.title}</p> | |
<a href={this.props.link}>{this.props.message}</a> | |
</div> | |
) | |
} | |
} | |
export default Component |
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
import inert from 'inert'; | |
import vision from 'vision'; | |
import Good from 'good'; | |
import goodConsole from 'good-console'; | |
import goodFile from 'good-file'; | |
export default (cluster) => { | |
const hapiModuleConfig = [ | |
inert, | |
vision, { | |
register: Good, | |
options: { | |
reporters: [{ | |
reporter: goodConsole, | |
events: { | |
response: '*', | |
log: '*' | |
} | |
}, { | |
reporter: goodFile, | |
events: { | |
ops: '*' | |
}, | |
config: { | |
path: './log/', | |
prefix: 'Ops-WorkerID-' + cluster.worker.id + '-' | |
} | |
}, { | |
reporter: goodFile, | |
events: { | |
error: '*' | |
}, | |
config: { | |
path: './log/', | |
prefix: 'Err-WorkerID-' + cluster.worker.id + '-' | |
} | |
}] | |
} | |
} | |
] | |
return hapiModuleConfig | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment