This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
125 lines (106 loc) · 3.16 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var express = require('express'),
connect = require('connect'),
faye = require('faye')
var HOSTED_ON_JOYENT = /\/home\/node\/node\-service\/releases\/[^\/]*\/server.js/.test(__filename)
,WEBSERVER_PORT = HOSTED_ON_JOYENT ? 80 : 8080
// Utilities
Utils = function() {
var BLURB_SIZE = HOSTED_ON_JOYENT ? 7 : 1
,BLURB_CHARS = "abcdefghijklmnopqrstuvxywz"
this.baseUrl = function(req) {
return req.headers['host'].split(":")[0];
}
this.generateBlurb = function() {
var blurb = '';
for (i=0;i<BLURB_SIZE;i++){
blurb += BLURB_CHARS[Math.floor(Math.random()*BLURB_CHARS.length)]
}
return blurb;
}
this.getPubSubServerURL = function(req) {
return "http://"+this.baseUrl(req)+":8000/pubsubhub"
}
}
utils = new Utils();
// PubSub Server
server = new Faye.NodeAdapter({mount: '/pubsubhub'});
server.listen(8000);
console.log("PubSub running at 8000")
// Express App
channels = {};
// Express setup
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(connect.bodyDecoder());
app.use(connect.methodOverride());
app.use(connect.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(app.router);
app.use(connect.staticProvider(__dirname + '/public'));
});
app.configure('development', function(){
app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(connect.errorHandler());
});
// Routes
//
// app.get('/', function(req, res){
// res.render('index.html.haml', {
// locals: {
// title: 'Umeboshi Fireteam FTW'
// }
// });
// });
app.get('/channels/new', function(req, res) {
var baseURL = utils.baseUrl(req);
var blurb = utils.generateBlurb();
pubsubURL = utils.getPubSubServerURL(req);
var channelURL = pubsubURL+"/"+blurb;
channels[blurb] = channelURL;
jsFile = pubsubURL+".js"
if (req.query.fmt == 'json') {
res.writeHead(200, { "Content-Type": "application/json" })
// res.writeHead(200, { "Content-Type": "text/plain" })
var output = JSON.stringify({ 'channel_key': blurb });
if (req.query.callback) output = req.query.callback + '('+output+')';//JSONP
res.end(output);
} else{
res.render('channels/new_channel.html.haml', {
locals: {
channelURL: channelURL
,pubsubJSFile: jsFile
,channelName: blurb
,pubsubURL: pubsubURL
}
});
}
});
app.get('/channels/info/:id', function(req, res) {
channelID = req.params.id;
channelURL = channels[channelID];
res.send(channelURL, { 'Content-Type': 'text/plain' }, 201);
});
app.get('/channels', function(req, res) {
res.render('channels/channels.html.haml', {
locals: {
channelList: channels
}
});
});
app.get('/demo/publisher/:id', function(req, res){
channelID = req.params.id;
channelURL = channels[channelID];
pubsubURL = utils.getPubSubServerURL(req);
jsFile = pubsubURL+".js"
res.render('demo_widget/demo_publisher.html.haml', {
locals: {
pubsubJSFile: jsFile
, pubsubURL: pubsubURL
, channel: channelID
}
})
});
if (!module.parent) app.listen(WEBSERVER_PORT);
console.log("Express application running at "+WEBSERVER_PORT);