Skip to content
Browse files

ported old code to new templates and such

  • Loading branch information...
1 parent d2133eb commit c148e74636b11c6f7cfcb77e7dbadd6eb50d254e @robrighter committed Aug 9, 2012
Showing with 345 additions and 14 deletions.
  1. +151 −5 server.js
  2. +130 −0 static/css/style.css
  3. +31 −6 static/js/script.js
  4. +25 −0 views/console.jade
  5. +8 −3 views/index.jade
View
156 server.js
@@ -4,6 +4,9 @@ var connect = require('connect')
, io = require('socket.io')
, port = (process.env.PORT || 8081);
+//channels
+var datastore = {};
+
//Setup Express
var server = express.createServer();
server.configure(function(){
@@ -42,32 +45,175 @@ var io = io.listen(server);
io.sockets.on('connection', function(socket){
console.log('Client Connected');
socket.on('message', function(data){
- socket.broadcast.emit('server_message',data);
- socket.emit('server_message',data);
+ switch(data.cmd){
+ case 'connect':
+ console.log('GOT A CONNECT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
+ //do something
+ connectChannel(data.channel, socket);
+ break;
+ case 'saveresponse':
+ updateResponseText(data.channel, data.responsetext)
+ break;
+ default:
+ //do something
+ }
});
socket.on('disconnect', function(){
console.log('Client Disconnected.');
});
});
+function connectChannel(thechannelname, client){
+ if(datastore.hasOwnProperty(thechannelname)){
+ //the channel already exists
+ console.log('Connected to Existing Channel: ' + thechannelname);
+ console.log('ResponseText for this channel is: ' + datastore[thechannelname].responseText);
+ datastore[thechannelname].clients.push(client);
+ }
+ else{
+ //the channel does not exist
+ console.log('Creating New Channel: ' + thechannelname);
+ datastore[thechannelname] = {
+ clients : [client],
+ responseText : ''
+ };
+ }
+ //set the callback to remove the client whenever it disconnects
+ client.on('disconnect', function(){
+ console.log('Client Disconnected from channel: ' + thechannelname);
+ var toremove = datastore[thechannelname].clients.indexOf(client);
+ console.log('going to delete item at index ' + toremove);
+ datastore[thechannelname].clients.splice(toremove,1);
+ });
+ return datastore[thechannelname];
+}
+
+function updateResponseText(thechannelname, responsetext){
+ if(datastore.hasOwnProperty(thechannelname)){
+ datastore[thechannelname].responseText = responsetext;
+ }
+}
+
///////////////////////////////////////////
// Routes //
///////////////////////////////////////////
/////// ADD ALL YOUR ROUTES HERE /////////
+//homepage
server.get('/', function(req,res){
res.render('index.jade', {
locals : {
- title : 'Your Page Title'
- ,description: 'Your Page Description'
- ,author: 'Your Name'
+ header: ''
+ ,footer: ''
+ ,title : 'HTTP Request Inspector'
+ ,description: 'A website for inspecting HTTP GETS and POSTS'
+ ,author: 'Rob Righter'
,analyticssiteid: 'XXXXXXX'
}
});
});
+server.get("/:channel/console", function (req, res) {
+ //render out the console template and nothing else. The Channel is created and handled on the web socket event
+ res.render('console.jade', {
+ locals : {
+ header: '',
+ footer: '',
+ title : (req.params.channel +' Console'),
+ description: '',
+ author: 'Rob Righter',
+ channelname: req.params.channel,
+ responsetext: getResponseText(req.params.channel),
+ analyticssiteid: 'XXXXXXX'
+ }
+ });
+
+});
+
+server.get("/:channel", function (req, res, match) {
+ //render reply with whatever they asked us to.
+ sendConsoleUpdateToClients(req.params.channel, formatConsoleEntry(req));
+ sendResponseText(req.params.channel,res);
+});
+
+server.post("/:channel", function (req, res, match) {
+ //render reply with whatever they asked us to.
+ sendConsoleUpdateToClients(req.params.channel, formatConsoleEntry(req));
+ sendResponseText(req.params.channel,res);
+});
+
+
+function getResponseText(thechannelname){
+ return (datastore.hasOwnProperty(thechannelname) ? datastore[thechannelname].responseText : '');
+}
+
+function sendResponseText(thechannelname,res){
+ if(datastore.hasOwnProperty(thechannelname)){
+ //the channel exists
+ res.send(datastore[thechannelname].responseText);
+ }
+ else{
+ //the channel does not exist so just send out an error
+ res.send('{"error" : "Sorry, this channel does not exist"}');
+ }
+}
+
+function sendConsoleUpdateToClients(channel, consolestring){
+ var message = {
+ cmd: 'consoleupdate',
+ value: consolestring
+ }
+ if(datastore.hasOwnProperty(channel)){
+ datastore[channel].clients.forEach(function(client){
+ client.send(JSON.stringify(message));
+ });
+ }
+}
+
+function formatConsoleEntry(req){
+
+ var toreturn = '<h5>'+req.method+' '+req.url+'</h5>';
+ if(req.hasOwnProperty('rawBody')){
+ toreturn += '<h5>Post Body:</h5>';
+ toreturn += ('<p>'+req.rawBody+'</p>');
+ }
+ toreturn += '<h5>Headers:</h5>';
+ objectMap(req.headers, function(key, value){
+ toreturn += ("<span class='key'>"+key+":</span> " + " <span class='value'>"+value+"</span><br>");
+ });
+
+ toreturn += '<hr>';
+
+ return toreturn;
+
+}
+
+//A Route for Creating a 500 Error (Useful to keep around)
+server.get('/500', function(req, res){
+ throw new Error('This is a 500 Error');
+});
+
+//The 404 Route (ALWAYS Keep this as the last route)
+server.get('/*', function(req, res){
+ throw new NotFound;
+});
+
+function NotFound(msg){
+ this.name = 'NotFound';
+ Error.call(this, msg);
+ Error.captureStackTrace(this, arguments.callee);
+}
+
+function objectMap(obj, callback){
+ var toreturn = [];
+ for(key in obj){
+ toreturn.push(callback(key, obj[key]));
+ }
+ return toreturn;
+}
+
//A Route for Creating a 500 Error (Useful to keep around)
server.get('/500', function(req, res){
View
130 static/css/style.css
@@ -106,6 +106,136 @@ textarea {
========================================================================== */
+ /* Primary Styles
+ Author: Rob Righter
+ */
+
+
+
+body{
+ background-color: #161616;
+}
+
+h1, h2 {
+ font-family: 'Yanone Kaffeesatz', arial, serif;
+ font-weight: bold;
+ color: #8B77FF;
+}
+
+h1 {
+ font-size: 50px;
+}
+
+
+h2 {
+ font-size: 30px;
+ color: #78B0FF;
+}
+
+h5 {
+ color: #78B0FF;
+ margin-top: 10px;
+}
+
+#main {
+ width: 950px;
+ margin-left: auto;
+ margin-right: auto;
+ background-color: #111111;
+ padding: 20px;
+}
+
+.instructions {
+ padding: 10px;
+ color: #938CC3;
+}
+
+.label {
+ color: #938CC3;
+}
+
+#console, #response form textarea {
+ background-color: #282634;
+ padding: 10px;
+ color: #938CC3;
+ border: 1px solid #382644;
+ height: 400px;
+ width: 928px;
+ margin-top: 10px;
+ margin-bottom: 55px;
+ font-family: 'Courier New','Droid Sans Mono', arial, serif;
+}
+
+#console {
+ overflow-y: scroll;
+}
+
+#response form button, #homesearch form button {
+ float: right;
+ background-color: #75AA3E;
+ color: #161A1D;
+ width: 200px;
+ height: 25px;
+ border: 1px solid #9ED258;
+ font-family: 'Yanone Kaffeesatz', arial, serif;
+ font-size: 18px;
+ margin-top: 10px;
+}
+
+#response form button:hover, #homesearch form button:hover {
+ background-color: #A7D765;
+}
+
+code {
+ font-family: 'Courier New','Droid Sans Mono', arial, serif;
+}
+
+#homesearch form #channelselector {
+ background-color: #282634;
+ padding: 5px;
+ color: #938CC3;
+ border: 1px solid #382644;
+ width: 490px;
+ font-size: 25px;
+ font-family: 'Courier New','Droid Sans Mono', arial, serif;
+}
+
+#homesearch {
+ width: 500px;
+ margin-left: auto;
+ margin-right: auto;
+ padding-bottom: 150px;
+}
+
+#homesearch h2, #homesearch .label {
+ float: left;
+}
+
+#homesearch h1 {
+ margin-bottom: 40px;
+}
+
+#homesearch .label {
+ margin-top: 17px;
+ margin-left: 10px;
+}
+
+.break {
+ clear: both;
+}
+
+#console .key {
+ color: #8C6DFF;
+}
+
+hr {
+ color:none;
+ border:none;
+ border-top:1px #290943 solid;
+ height:1px;
+ overflow:hidden;
+ line-height:1px;
+}
View
37 static/js/script.js
@@ -6,10 +6,35 @@ $(document).ready(function() {
var socket = io.connect();
$('#sender').bind('click', function() {
- socket.emit('message', 'Message Sent on ' + new Date());
- });
-
- socket.on('server_message', function(data){
- $('#receiver').append('<li>' + data + '</li>');
- });
+ socket.send("Message Sent on " + new Date());
+ });
+
+ socket.on('message', function(data){
+ var message = JSON.parse(data);
+
+ switch(message.cmd){
+ case 'consoleupdate':
+ //do something
+ $('#console').prepend(message.value);
+ break;
+ default:
+ //do something
+ break;
+ }
+ //$('#reciever').append('<li>' + data + '</li>');
+ });
+
+ $('#homesearch form').submit(function(){
+ window.location = '/' + $('#channelselector').val() + "/console";
+ return false;
+ });
+
+ $('#response form button').click(function(){
+ socket.send(JSON.stringify({
+ cmd : "saveresponse",
+ responsetext : $('#response form textarea').val(),
+ channel : channelname
+ }));
+ return false;
+ })
});
View
25 views/console.jade
@@ -0,0 +1,25 @@
+extends layout
+
+block content
+ h1='Request Inspector'
+ h2=channelname
+
+ div.instructions
+ p='To view the request to a POST or a GET on this console, direct your request to the following url:'
+ code='http://robrighter.no.de/'+channelname
+
+ h2='Console:'
+ #console=''
+
+ #response
+ form
+ button(type='button')='Save Response'
+ h2='Response Text (for both POST and GET):'
+ textarea=responsetext
+ script
+ | var channelname = '<%= channelname %>';
+ | socket.send(JSON.stringify({
+ | cmd : "connect",
+ | channel : channelname
+ | }));
+
View
11 views/index.jade
@@ -1,6 +1,11 @@
extends layout
block content
- div
- a(id='sender')='Send a Message'
- ul(id='receiver')
+ #homesearch
+ h1='Request Inspector'
+ form
+ h2='Choose Url'
+ div.label='(example: "tester123" will be http://robrighter.no.de/tester123)'
+ input(type='text',id='channelselector')
+ button(type='submit')='Start Console'
+ div.break

0 comments on commit c148e74

Please sign in to comment.
Something went wrong with that request. Please try again.