-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
175 lines (155 loc) · 6.71 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Module dependencies.
*/
var express = require('express');
//var routes = require('./routes');
var user = require('./routes/user');
var model = require('./domain/model.js');
var notifications = require('./util/notifications.js');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
var enforce = require('express-sslify');
var app = express();
if (process.env.ENV !== 'dev') {
app.use(enforce.HTTPS({ trustProtoHeader: true }));
}
// all environments
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
next();
});
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon("public/images/favicon.ico"));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
//app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//Initialize Mongoose
//mongodb://af_brew-o-matic-lautaromail:mngn0k588adkt5er4h758tp1im@ds047948.mongolab.com:47948/af_brew-o-matic-lautaromail
mongoose.connect(process.env.MONGOLAB_URI);
//mongoose.connect('mongodb://app:lac713@ds047948.mongolab.com:47948/af_brew-o-matic-lautaromail');
function filterAdmin(req,res,next){
console.log("checking admin session");
var s = req.session;
if (s.user_isAdmin ) {
console.log("sigue");
next();
} else {
console.log("null");
res.send(500,{error:'No tiene permisos para acceder a estos datos'});
}
}
function filter (req,res,next){
console.log("checking session");
var s = req.session;
console.log("user: " + s.user_id);
console.log("google_id: " + req.query.google_id);
if (s.user_id ) {
console.log("sigue");
next();
} else if ( req.query.google_id ) {
model.User.findOne({'google_id':req.query.google_id}).exec(function(err,user) {
//console.log(user);
if (user) {
var s = req.session;
console.log("Set User_Id in session: " + user._id);
//console.log(req);
s.user_id = user._id;
s.user_name = user.name;
s.user_isAdmin = user.isAdmin;
next();
} else {
console.log("null");
res.send(500,{error:'Hubo un problema con la operacion, presion F5 y reintente'});
}
});
} else {
console.log("null");
res.send(500,{error:'Hubo un problema con la operacion, presion F5 y reintente'});
}
}
var recipe = require("./routes/recipe.js");
var tilt = require("./routes/tilt.js");
var data = require("./routes/data.js");
var rating = require("./routes/rating.js");
var stats = require("./routes/stats.js");
// hack for avoid timeout looking for socket.io
app.get('/socket.io', function(req, res, next) {
res.status(204).send();
});
app.get('/user/accessCode/:accessCode', user.getByAccessCode);
app.get('/user/google_:google_id', user.getByGoogleId);
app.post('/user', user.add);
app.get('/user/id_:id', user.get);
app.put('/user/favorite_add',filter,user.addToFavorites);
app.put('/user/favorite_drop',filter,user.removeFromFavorites);
app.get('/user',filter,data.User.findAll);
app.put('/user/settings',filter,user.updateSettings);
app.get('/stats/last', filter, stats.lastActions);
app.get('/stats',filter,stats.all);
app.get('/recipe/export/:id',filter,recipe.exportRecipe);
app.get('/recipe/export',filter,recipe.exportAll);
app.get('/recipe/public',filter,recipe.findPublic);
app.get('/recipe/public_count',filter,recipe.countPublic);
app.get('/recipe/public_styles',filter,recipe.publicStyles);
app.get('/recipe/collaborated',filter,recipe.findCollaborated);
app.put('/recipe/comment',filter,recipe.addComment);
app.get('/recipe/comment:id',recipe.getComments);
app.post('/recipe/publish_:id',filter,recipe.publish);
app.post('/recipe/state_:id',filter,recipe.updateState);
app.put('/recipe/remove_comment',filter,recipe.deleteComment);
app.get('/recipe/my_count',filter,recipe.countAll);
app.get('/recipe/my_tags',filter,recipe.myTags);
app.get('/recipe',filter,recipe.findAll);
app.get('/recipe/stats',filter,recipe.stats);
app.get('/recipe/by_user_:id',filter,recipe.findByUser);
app.get('/recipe/:id',recipe.get);
app.get('/recipe/:id/xml',recipe.getBeerXML);
app.post('/recipe/:id',filter,recipe.save);
app.post('/recipe',filter,recipe.save);
app.delete('/recipe/:id',filter,recipe.remove);
app.get('/notification',filter,notifications.findAll);
app.get('/notification/news',filter,notifications.findNews);
app.post("/notification/:id",filter,notifications.update);
app.get('/rating/beers',rating.findBeers);
app.post('/tilt/:id',tilt.updateTilt);
var services = ['Style','Grain','Hop','Yeast','Misc','Bottle','Tag','WaterReport','TempDevice','TempDeviceReport'];
for (s in services ) {
app.get('/' + services[s].toLowerCase(),data[services[s]].findAll);
app.get('/' + services[s].toLowerCase()+ "/:id", data[services[s]].findById);
app.post('/' + services[s].toLowerCase() + "/:id",data[services[s]].save);
app.post('/' + services[s].toLowerCase(),data[services[s]].save);
app.delete('/' + services[s].toLowerCase()+ "/:id",data[services[s]].remove);
}
var admin = ['Recipe','User','Action'];
for (s in admin ) {
app.get('/admin/' + admin[s].toLowerCase()+ "/count",[filter,filterAdmin],data[admin[s]].count);
app.get('/admin/' + admin[s].toLowerCase(),[filter,filterAdmin],data[admin[s]].findAll);
app.post('/admin/' + admin[s].toLowerCase() + "/:id",[filter,filterAdmin],data[admin[s]].save);
app.post('/admin/' + admin[s].toLowerCase(),[filter,filterAdmin],data[admin[s]].save);
app.delete('/admin/' + admin[s].toLowerCase()+ "/:id",[filter,filterAdmin],data[admin[s]].remove);
}
var scheduler = require("./util/scheduler");
scheduler.runEveryDay(notifications.removeOld,true);
scheduler.runEveryDay(require("./routes/actions.js").removeOld,true);
scheduler.runEveryDay(recipe.fireFermentationNotification,true);
// scheduler.runEverySecond(recipe.fireFermentationNotification,true);
var server = http.createServer(app).listen(process.env.VCAP_APP_PORT || app.get('port'), function(){
console.log('Express server listening on port ' + (process.env.VCAP_APP_PORT || app.get('port')));
});
var push = require("./routes/push.js");
push.initOn(server);
//mongoose.disconnect();