Created
June 20, 2012 21:25
-
-
Save marfalkov/2962326 to your computer and use it in GitHub Desktop.
Using winston-mongodb with a scalable nodejs application on OpenShift
This file contains hidden or 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
#!/bin/env node | |
// OpenShift sample Node application | |
var winston = require('winston'); | |
var MongoDB = require('winston-mongodb').MongoDB; | |
winston.add(MongoDB, { | |
db: process.env.OPENSHIFT_APP_NAME | |
, host: process.env.OPENSHIFT_NOSQL_DB_HOST | |
, port: parseInt(process.env.OPENSHIFT_NOSQL_DB_PORT, 10) | |
, safe: true | |
, level: 'info' | |
, silent: false | |
, username: process.env.OPENSHIFT_NOSQL_DB_USERNAME | |
, password: process.env.OPENSHIFT_NOSQL_DB_PASSWORD | |
}); | |
winston.remove(winston.transports.Console); | |
/* Use this function to log the OPENSHIFT_GEAR_NAME into the meta. See: lineno: 44 */ | |
var slogger = function(level, logmessage, meta) { | |
var meta = meta || {}; | |
meta.OPENSHIFT_GEAR_NAME = process.env.OPENSHIFT_GEAR_NAME; | |
winston.log(level, logmessage, meta); | |
} | |
var express = require('express'); | |
var fs = require('fs'); | |
// Local cache for static content [fixed and loaded at startup] | |
var zcache = { 'index.html': '' }; | |
zcache['index.html'] = fs.readFileSync('./index.html'); // Cache index.html | |
// Create "express" server. | |
var app = express.createServer(); | |
/* ===================================================================== */ | |
/* Setup route handlers. */ | |
/* ===================================================================== */ | |
// Handler for GET /logtest | |
app.get('/logtest', function(req, res){ | |
slogger('info', 'hello from logger', {foo:'bar'}); | |
res.send('testing'); | |
}); | |
// Handler for GET /health | |
app.get('/health', function(req, res){ | |
res.send('1'); | |
}); | |
// Handler for GET /asciimo | |
app.get('/asciimo', function(req, res){ | |
var link="https://a248.e.akamai.net/assets.github.com/img/d84f00f173afcf3bc81b4fad855e39838b23d8ff/687474703a2f2f696d6775722e636f6d2f6b6d626a422e706e67"; | |
res.send("<html><body><img src='" + link + "'></body></html>"); | |
}); | |
// Handler for GET / | |
app.get('/', function(req, res){ | |
res.send(zcache['index.html'], {'Content-Type': 'text/html'}); | |
}); | |
// Get the environment variables we need. | |
var ipaddr = process.env.OPENSHIFT_INTERNAL_IP; | |
var port = process.env.OPENSHIFT_INTERNAL_PORT || 8080; | |
if (typeof ipaddr === "undefined") { | |
console.warn('No OPENSHIFT_INTERNAL_IP environment variable'); | |
} | |
// terminator === the termination handler. | |
function terminator(sig) { | |
if (typeof sig === "string") { | |
console.log('%s: Received %s - terminating Node server ...', | |
Date(Date.now()), sig); | |
process.exit(1); | |
} | |
console.log('%s: Node server stopped.', Date(Date.now()) ); | |
} | |
// Process on exit and signals. | |
process.on('exit', function() { terminator(); }); | |
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS', | |
'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGPIPE', 'SIGTERM' | |
].forEach(function(element, index, array) { | |
process.on(element, function() { terminator(element); }); | |
}); | |
// And start the app on that interface (and port). | |
app.listen(port, ipaddr, function() { | |
console.log('%s: Node server started on %s:%d ...', Date(Date.now() ), | |
ipaddr, port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment