-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
325 lines (296 loc) · 9.96 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// # MakeHub server
//
// New Relic Metrics import
require('newrelic');
// Core imports
var http = require('http');
var https = require('https');
var path = require('path');
// Third party modules import
var async = require('async');
var express = require('express');
var I18n = require('i18n-2');
var util = require('util');
var _ = require('underscore');
var restler = require('restler')
// MakeHub imports
var github = require('./auth/github');
var projectParser = require('./project-parser');
var MAKEHUB_PROJECT_FLAG = "(¯`·._.·[ MakeHub Project ]·._.·´¯)";
var pagedown = require("pagedown");
var converter = pagedown.getSanitizingConverter();
console.log('Running application with GITHUB_CLIENT_ID = ' + github.GITHUB_CLIENT_ID);
console.log('Running application with GITHUB_CLIENT_SECRET = ' + github.GITHUB_CLIENT_SECRET);
console.log('Running application on ' + github.HOSTNAME);
//
// Creates a new instance of SimpleServer with the following options:
// * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
//
var app = express();
// configure Express
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({
secret: 'keyboard cat'
}));
I18n.expressBind(app, {
locales:['en','cn'],
cookieName: 'locale'
});
// This is how you'd set a locale from req.cookies.
// Don't forget to set the cookie either on the client or in your Express app.
app.use(function(req, res, next) {
//this changes it to chinese: document.cookie = "locale=cn";
req.i18n.setLocaleFromCookie(req);
next();
});
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(github.passport.initialize());
app.use(github.passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/client'));
});
app.get('/', function (req, res) {
res.render('index', {
user: req.user,
hostname: github.HOSTNAME,
});
});
// GET /auth/github
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in GitHub authentication will involve redirecting
// the user to github.com. After authorization, GitHubwill redirect the user
// back to this application at /auth/github/callback
app.get('/auth/github',
github.passport.authenticate('github'),
function (req, res) {
// The request will be redirected to GitHub for authentication, so this
// function will not be called.
});
// GET /auth/github/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/github/callback',
github.passport.authenticate('github', {
failureRedirect: '/login'
}),
function (req, res) {
res.redirect('/#/create');
});
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
app.get('/cn', function (req, res) {
res.cookie('locale', '"cn"', { maxAge: 90000, path: '/' });
res.redirect('/');
});
app.get('/en', function (req, res) {
res.clearCookie('locale');
res.redirect('/');
});
app.post('/create', function (req, res) {
if (!req.isAuthenticated()) {
res.send({
'error': 'Login required'
});
return;
}
github.conn(req).gists.create({
description: req.body.project.title,
public: "true",
files: {
'makehub': {
"content": projectParser.encode(req.body.project)
},
'makehub.json': {
"content": JSON.stringify(req.body.project)
}
}
}, function (err, gist) {
if (err) {
res.send({
'error': JSON.parse(err.message).message
})
} else if (!gist.files['makehub']) {
res.send({
'error': 'This is not a makehub project.'
})
} else {
var project = projectParser.parse(gist);
res.send(project);
}
});
});
app.post('/project/:projectId', function (req, res) {
if (!req.isAuthenticated()) {
res.send({
'error': 'Login required'
});
return;
}
var options = {
id: req.params.projectId,
description: req.body.project.title,
files: {
'makehub': {
"content": projectParser.encode(req.body.project)
},
'makehub.json': {
"content": JSON.stringify(req.body.project)
}
}
};
github.conn(req).gists.edit(
options, function (err, gist) {
if (err) {
res.send({
'error': JSON.parse(err.message).message
})
} else if (!gist.files['makehub']) {
res.send({
'error': 'This is not a makehub project.'
})
} else {
var project = projectParser.parse(gist);
res.send(project);
}
});
});
app.get('/project/:projectId', function (req, res) {
console.log([req.params.userId, req.params.projectId, 'raw'].join('/'));
var currentlyLoggedInUser = req.user ? req.user._json.login : null;
// https://api.github.com/gists/4224228
github.conn(req).gists.get({
id: req.params.projectId
},
function (err, gist) {
//console.log(err)
//console.log(gist)
if (err) {
res.send({
'error': JSON.parse(err.message).message
})
} else if (!gist.files['makehub']) {
res.send({
'error': 'This is not a makehub project.'
})
} else {
var project = projectParser.parse(gist);
project.ownedByMe = currentlyLoggedInUser == gist.owner.login;
var opts = {
urls: project.urls,
maxWidth: 450
};
project['urls'].forEach(function(media, index) {
var replaceValue = "<img src='" + media.replace("http:","https:") + "'>";
project['content'] = project['content'].replace("{{" + index + "}}", replaceValue);
});
res.send(project);
}
}
);
});
app.post('/project/fork/:projectId', function (req, res) {
if (!req.isAuthenticated()) {
res.send({
'error': 'Login required'
});
return;
}
console.log("FORKING project " + req.params.projectId);
github.conn(req).gists.fork({
id: req.params.projectId
},
function (err, gist) {
console.log(err)
console.log(gist)
if (err) {
res.send({
'error': JSON.parse(err.message).message
})
} else {
res.send({
id: gist.id
});
}
}
);
});
app.post('/project/delete/:projectId', function (req, res) {
if (!req.isAuthenticated()) {
res.send({
'error': 'Login required'
});
return;
}
console.log("DELETING project " + req.params.projectId);
github.conn(req).gists.delete({
id: req.params.projectId
},
function (err, gist) {
if (err) {
console.log(err)
res.send({
'error': JSON.parse(err.message).message
})
} else {
res.send({
id: gist.id
});
}
}
);
});
app.post('/my_projects', function (req, res) {
github.conn(req).gists.getFromUser({
user: req.user._json.login
},
function (err, res2) {
res.contentType('json');
var makeHubProjects = [];
res2.forEach(function (gist, index) {
if (gist.files['makehub']) {
console.log(gist)
var project = projectParser.parse(gist);
makeHubProjects.push(project);
}
});
res.send({
projects: makeHubProjects
});
}
);
});
app.post('/upload_picture', function (req, res) {
var file = req.files.file
restler.post("http://deviantsart.com", {
multipart: true,
data: {
"filename": restler.file(file.path, file.name,
file.size, null, "image/jpg")
}
}).on("complete", function(data) {
res.send(data);
});
});
app.listen(process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3000, process.env.OPENSHIFT_NODEJS_IP || process.env.IP || "0.0.0.0");
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login')
}