This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
135 lines (114 loc) · 3.58 KB
/
app.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
126
127
128
129
130
131
132
133
134
135
// Below line was recommended by one tutorial, commented out for now
// require.paths.push('/home/chadvonnau/lib/node_modules');
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, StreamProvider = require('./streamprovider').StreamProvider;
var app = module.exports = express.createServer()
, appPort = 0
, dbPort = 0
, dbName = '';
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
appPort = 11581;
dbPort = 14633;
dbName = 'gusherdevdb';
});
app.configure('production', function(){
app.use(express.errorHandler());
appPort = 11231;
dbPort = 14633;
dbName = 'gusherdb';
});
// App code
var streamProvider = new StreamProvider('localhost', dbName, dbPort);
var formatDate = function(dateObj){
dateObj = dateObj || new Date();
var dayname = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),
monthname = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var dn = dayname[dateObj.getDay()],
d = dateObj.getDate(),
mn = monthname[dateObj.getMonth()],
y = dateObj.getYear();
if(y < 1000) y+=1900;
return dn + ', ' + d + ' ' + mn + ' ' + y; //equivalent to dateObj.strftime('%A, %e %B %Y');
}
// Routes
app.get('/stream/new', function(req, res) {
res.render('stream_new', { locals: {
title: 'new stream'
}
});
});
app.post('/stream/new', function(req, res) {
streamProvider.save({
name: req.param('name'),
title: req.param('title')
}, function(error, docs) {
res.redirect('/'+req.param('name'));
})
});
app.post('/stream/delete', function(req, res) {
streamProvider.removeById(req.param('_id'), function(error, docs) {
res.redirect('/');
})
});
app.post('/stream/addfeed', function(req, res) {
streamProvider.addFeed(req.param('targetstream'), {
type: req.param('type'),
user_name: req.param('user_name'),
user_id: req.param('user_id'),
created_at: new Date()
}, function(error, docs) {
var result = JSON.stringify({
params: req.param('type')+ 'Params',
user_name: req.param('user_name'),
user_id: req.param('user_id')
});
res.send(result);
//res.redirect('/' + req.param('targetstream'))
}
);
});
app.post('/stream/removefeed', function(req, res) {
streamProvider.removeFeed(req.param('targetstream'), +req.param('timestamp'), function(error, docs) {
res.send('success');
}
);
});
app.get('/:name', function(req, res) {
streamProvider.findByName(req.params.name, function(error, stream) {
stream.created_at = formatDate(new Date(stream.created_at));
res.render('stream_show', { locals: {
title: stream.name,
stream: stream
}
});
});
});
//app.get('/', routes.index);
app.get('/', function(req, res){
streamProvider.findAll(function(error, docs){
res.render('index', { locals: {
title: 'gusher',
streams: docs
}
});
});
});
app.listen(appPort);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);