-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
288 lines (210 loc) · 7.38 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
require('newrelic');
var express = require('express');
var app = express();
var mongo = require("mongodb").MongoClient;
var ObjectID = require('mongodb').ObjectID;
var bodyParser = require('body-parser');
var stories = require('./DBSamples/new_storeis.json');
var fs = require('fs');
var mdb = null;
var FACEBOOK_APP_ID = '543308622468738';
var gcloud = require('gcloud');
var uuid = require('node-uuid');
var busboy = require('connect-busboy');
var bucket;
var projectId = 'streets-city';
var bucketName = 'streets';
// TODO: need to inform if /gcloud.json isn't there...
// without it image uploade crash the server.... /:
// if(process.env.MONGO){
// bucket = gcloud.storage({
// projectId: projectId,
// }).bucket(bucketName);
// } else {
bucket = gcloud.storage({
projectId: projectId,
keyFilename: __dirname + '/gcloud.json',
}).bucket(bucketName);
// }
var mdb = null;
function generateOpenGraphTags(story) {
var opengraph =
'<meta name="og:title" content="A story at ' + story.location + ' by ' + story.authorName + '">'+
'<meta name="og:site_name" content="Streets.City">'+
//'<meta name="og:description" content="A short story taking place in ' + story.location + ' by ' + story.authorName + ' written on ' + story.storyCreateDate +'">'+
'<meta name="og:description" content="' + story.text.replace('"','"') + '">'+
'<meta name="og:image" content="'+story.imageUrl+'">'+
'<meta name="fb:app_id" content="'+FACEBOOK_APP_ID+'">';
return opengraph;
}
function renderAppHtml(opengraphTags, callback) {
fs.readFile(__dirname+'/public/app.html', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
data = data.replace('OPENGRAPH_TAGS_THIS_IS_REPLACED_DONT_CHANGE_IT_RANDOM_ID_FOLLOWS_AGAGAOEGASJDGAEGOEGOR',opengraphTags);
callback(data);
});
}
var OPENGRAPH_TAGS_MAIN_PAGE =
'<meta name="og:title" content="Streets.City - Welcome">'+
'<meta name="og:site_name" content="Streets.City">'+
'<meta name="og:description" content="There are so many stories to be told. Stories of kisses and parties and excitement and love. Stories of people. Real people. People that live in the streets of Tel Aviv. We\'re ready with the new platform. Ready to hear YOUR stories. Share it with us, so we can remember how awesome this city is. Do it now. ">'+
//'<meta name="og:image" content="I AM AN IMAGE - CHANGE MEEEEEE">'+
'<meta name="fb:app_id" content="'+FACEBOOK_APP_ID+'">';
app.set('port', (process.env.PORT || 5555));
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json());
app.use(busboy());
app.use('/public', express.static(__dirname + '/public'));
app.get('/', function(req, res){
var openGraphTags = OPENGRAPH_TAGS_MAIN_PAGE;
renderAppHtml(openGraphTags, function(page) {
res.status(200).send(page);
});
});
app.get('/about', function(req, res){
renderAppHtml('', function(page) {
res.status(200).send(page);
});
});
app.get('/add_story', function(req, res){
renderAppHtml('', function(page) {
res.status(200).send(page);
});
});
app.get('/streets_meet', function(req, res){
renderAppHtml('', function(page) {
res.status(200).send(page);
});
});
app.get('/story/:storyId', function(req, res){
// Get the story, create opengraph tags, set template and return
var col = mdb.collection('stories');
var storyId = req.params.storyId;
try {
var objectId = ObjectID.createFromHexString(storyId);
} catch(e){
res.status(500).send('Error : please provide a valid storyId');
return;
}
col.findOne(objectId, function(err, doc){
if(err){
console.error('Error find story by storyId : %s. Error : %s.',storyId, err);
} else {
var openGraphTags = generateOpenGraphTags(doc);
renderAppHtml(openGraphTags, function(page) {
res.status(200).send(page);
});
}
});
});
/**
* Fetch story by ID
*/
app.get('/api/story/:storyId', function(req,res){
var col = mdb.collection('stories');
var storyId = req.params.storyId;
try{
var objectId = ObjectID.createFromHexString(storyId);
}catch(e){
res.status(500).send('Error : please provide a valid storyId');
return;
}
console.log('Fetch story for story ID : ' + storyId);
col.findOne(objectId, function(err, doc){
if(err){
console.error('Error find story by storyId : %s. Error : %s.',storyId, err);
}else{
res.send(doc);
}
});
});
/*app.get('/add_story', function(req,res,next) {
res.sendFile(__dirname+'/public/add_story.html');
});*/
app.get('/terms', function(req,res,next) {
res.sendFile(__dirname+'/public/terms.html');
});
app.get('/api/story', function(req,res){
var col = mdb.collection('stories');
col.count(function(err, count){
var randNum = Math.round(Math.random() * (count - 1)) + 1;
col.find().limit(-1).skip(randNum - 1).nextObject(function(err,doc){
res.send(doc);
});
});
});
app.get('/image/:filename', function(req,res){
bucket.file(req.params.filename).createReadStream().pipe(res);
});
app.post('/image', function(req,res){
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype){
var newId = uuid.v4();
var filename = newId + "." + mimetype.split("/")[1];
var path = "/image/" + filename;
file.pipe(bucket.file(filename).createWriteStream())
.on('complete', function(){
res.send(path);
})
.on('error', function(e){
console.log("error",e);
res.status(401).send(e);
});
});
});
var FIRST_STORY_MAGIC_ID = 'first_story';
app.get('/api/firstStory', function(req,res){
var col = mdb.collection('stories');
col.findOne({'_id':FIRST_STORY_MAGIC_ID}, function(err,doc) {
if(err){
console.error('Error find story by storyId : %s. Error : %s.',FIRST_STORY_MAGIC_ID, err);
} else {
res.send(doc);
}
})
});
app.post('/api/story', function(req,res){
console.log('Adding new story');
var col = mdb.collection('stories');
var story = req.body;
story.storyCreateDate = new Date();
//TODO: add some validations!!!
col.insert(story, function(err, records){
if(err){
console.error('Error adding a story. Error : ', err);
res.status(500).send('Oops, seems like we couldn\'t adding a story.');
}else{
console.log('id', records[0]._id);
res.json(records[0]);
}
});
});
app.get('/cleandb', function(req,res){
mdb.collection('stories').drop();
res.send("db clean!");
});
var loadStories = function () {
var col = mdb.collection('stories');
stories.map(function(story){
col.insert(story, function(err,col){});
});
}
app.get('/loadStories', function(req,res){
loadStories();
res.send("loaded");
});
var mongoIP = process.env.MONGO ? process.env.MONGO : "127.0.0.1";
// // Force prod IP
// mongoIP = "146.148.6.242";
var mongoUrl = 'mongodb://' + mongoIP + ':27017/streets';
console.log("connecting to mongo:" + mongoUrl);
mongo.connect(mongoUrl, function(err, db){
if (err) throw err;
console.log("mongo connected!");
mdb = db;
app.listen(app.get('port'), function() {
console.log("running on localhost:" + app.get('port'));
});
})