Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added code to see application data as an admin

  • Loading branch information...
commit 49e133e71b3ca8bb484afee1b2a766a91539c995 1 parent fac6e19
@jettro authored
View
12 app-uniform.js
@@ -4,6 +4,7 @@ function AppUniform() {
var properties = require('./properties');
var Site = require('./site');
var Blog = require('./blog');
+var Data = require('./data');
AppUniform.prototype.start = function(chat) {
var pub = __dirname + '/public';
@@ -11,7 +12,8 @@ AppUniform.prototype.start = function(chat) {
var express = require('express')
, app = express.createServer()
, blog = new Blog()
- , site = new Site(properties);
+ , site = new Site(properties)
+ , data = new Data(properties);
app.configure(function() {
app.set('view engine', 'jade');
@@ -36,9 +38,15 @@ AppUniform.prototype.start = function(chat) {
app.get('/blog/new', blog.blogShowForm);
app.post('/blog/new', blog.blogPostForm);
+// chat
app.get('/chat', chat.index);
- app.listen(process.env.VCAP_APP_PORT || 8008);
+// data
+ app.get('/data',data.index);
+ app.get('/data/messages',data.messages);
+ app.get('/data/warnings',data.warnings);
+
+ app.listen(process.env.VCAP_APP_PORT || 8008);
console.log('Express server started on port %s', app.address().port);
chat.init(app);
View
52 data.js
@@ -0,0 +1,52 @@
+var twitterAccount;
+var Redis = require('./redis');
+var redis = new Redis();
+
+function Data(properties) {
+ twitterAccount = properties.admin.twitter;
+}
+
+Data.prototype.index = function(req,res) {
+ if (!checkAdminUser(req,res)) return;
+
+ res.render('data/index', {locals: {loginName:req.session.user.name}});
+};
+
+Data.prototype.messages = function(req,res) {
+ if (!checkAdminUser(req,res)) return;
+ var messages = [];
+ redis.obtainMessages(function(replies) {
+ replies.forEach(function (reply, i) {
+ messages.push(JSON.parse(reply));
+ });
+
+ res.render('data/messages', {locals: {loginName:req.session.user.name, messages:messages}});
+ });
+};
+
+Data.prototype.warnings = function(req,res) {
+ if (!checkAdminUser(req,res)) return;
+ var warnings = [];
+ redis.obtainWarnings(function(replies) {
+ replies.forEach(function (reply, i) {
+ console.log("warning: %s", reply);
+ warnings.push(JSON.parse(reply));
+ });
+
+ res.render('data/warnings', {locals: {loginName:req.session.user.name, warnings:warnings}});
+ });
+};
+
+function checkAdminUser(req,res) {
+ var loginName = '';
+ if (req.session.oauth && req.session.user) {
+ loginName = req.session.user.name;
+ if (req.session.user.name == twitterAccount) {
+ return true;
+ }
+ }
+ res.render('404',{locals: {loginName:loginName}});
+ return false;
+}
+
+module.exports = Data;
View
14 redis.js
@@ -38,7 +38,19 @@ Redis.prototype.writeWarning = function(message) {
client.lpush("warnings", message);
client.quit();
}
-}
+};
+
+Redis.prototype.obtainWarnings = function(callback) {
+ if (redisEnabled) {
+ initClient();
+ client.lrange("warnings", 0, -1, function(err, replies){
+ callback(replies);
+ });
+ client.quit();
+ } else {
+ callback([]);
+ }
+};
function initClient() {
client = redis.createClient();
View
1  views/404.jade
@@ -0,0 +1 @@
+p Oops, I do not have that page
View
7 views/data/index.jade
@@ -0,0 +1,7 @@
+p About to enter the data zone
+p where to go from here?
+ul
+ li
+ a(href="/data/messages") messages
+ li
+ a(href="/data/warnings") warnings
View
12 views/data/messages.jade
@@ -0,0 +1,12 @@
+p
+ a(href="/data") Back
+
+p show all messages from the application
+
+table
+ - each message in messages
+ tr
+ td
+ #{message.chat[0]}
+ td
+ #{message.chat[1]}
View
12 views/data/warnings.jade
@@ -0,0 +1,12 @@
+p
+ a(href="/data") Back
+
+p show all warnings from the application
+
+table
+ - each warning in warnings
+ tr
+ td
+ #{warning.hack.date}
+ td
+ #{warning.hack.new}
Please sign in to comment.
Something went wrong with that request. Please try again.