UNPKG

giraffe-web

Version:

Giraffe dashboard for Graphite wrapped in a webserver

206 lines (145 loc) 4.85 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bluebox Source: giraffe-web.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.cerulean.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <a class="brand" href="index.html">Bluebox</a> <ul class="nav"> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="GiraffeWeb.html">GiraffeWeb</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: giraffe-web.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">'use strict'; var path = require('path'); var fs = require('fs'); var url = require('url'); var connect = require('connect'); var proxy = require('proxy-middleware'); /** * @constructor * @name GiraffeWeb * @param {hash} options * @param {integer} options.port Port to start the webserver on * @param {string} options.graphiteUrl Graphite URL to connect to * @param {string} options.configFile Path for the dashboardsfile to read * @param {boolean} options.proxyGraphite Flag if we need to proxy Graphite requests via this webserver */ var GiraffeWeb = function(options) { var self = this; self.settings = options; self.config = self.loadConfig(options.configFile); }; module.exports = GiraffeWeb; /** * Method to read the config dashboard and replace the graphite according to settings * If we proxy it replaces the graphiteURL to point to this webserver/graphite * * @name loadConfig * @memberof GiraffeWeb * @public * @param {string} filename Filename to read the dashboard config from * @returns {string} The correct dashboard file with the options specified */ GiraffeWeb.prototype.loadConfig = function(filename) { var self = this; var settings = self.settings; var fileConfig = fs.readFileSync(filename, 'utf-8'); var graphiteRegEx = /^var.*graphite_url.*\n/; var graphiteUrl; if (settings.proxyGraphite) { graphiteUrl = 'http://localhost:' + settings.port + '/graphite'; } else { graphiteUrl = settings.graphiteUrl; } var config = fileConfig.replace(graphiteRegEx , 'var graphite_url = "'+ graphiteUrl + '";\n'); return config; }; /** * Method to launch the webserver * * - If we proxyGraphite, we will proxy the request to another Graphite HTTP server * - We override the dashboards.js file provided by plain Giraffe by our own dashboard file specified * * @name start * @memberof GiraffeWeb * @public */ GiraffeWeb.prototype.start = function() { var self = this; var settings = self.settings; var app = connect(); app.use(connect.logger('dev')); if (settings.proxyGraphite) { console.log('proxying Graphite to ' + settings.graphiteUrl); app.use('/graphite', proxy(url.parse(settings.graphiteUrl))); } // Server dashboards.js from the config file app.use(function logItHandle(req, res, next) { if (req.url === '/dashboards.js') { res.writeHead(200, {'Content-Type': 'application/javascript'}); res.end(self.config); } else { next(); } }); app.use(connect.static(path.join(__dirname , '..', 'vendor', 'giraffe'))); connect.createServer(app).listen(settings.port); }; </pre> </article> </section> </div> <div class="clearfix"></div> <footer> </div> <br clear="both"> </div> </div> <script src="scripts/sunlight.js"></script> <script src="scripts/sunlight.javascript.js"></script> <script src="scripts/sunlight-plugin.doclinks.js"></script> <script src="scripts/sunlight-plugin.linenumbers.js"></script> <script src="scripts/sunlight-plugin.menu.js"></script> <script src="scripts/jquery.min.js"></script> <script src="scripts/jquery.scrollTo.js"></script> <script src="scripts/jquery.localScroll.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script> <script> $( function () { $( "#toc" ).toc( { selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : 60 } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); } ); </script> </body> </html>