-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
116 lines (105 loc) · 3.03 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
'use strict';
// Config file
const config = require('./config.json');
// Pillars require
const project = require('pillars').configure({
debug: config.debug,
renderReload: config.debug,
});
// Extract global classes
const {Route} = global;
// i18n config
const i18n = project.i18n;
i18n.load('guilds', './languages');
i18n.languages = ['es_ES'];
// Setup default HTTP service
project.services.get('http').configure({port: config.port}).start();
// Mongo config
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
MongoClient.connect(config.mongoURL || 'mongodb://localhost:27017', function(e, client) {
if (e) {
console.log('Mongo connect Error.', e);
} else {
project.DB = client.db('guilds');
console.log('Mongo connect Succes.');
}
});
// Inject MongoDB sessions connector
require('./src/mongoSessions')(project);
// Load passport middleware (Github - Passport - MongoDB connector)
require('./src/mongoPassport')(project);
// API
project.routes.add(new Route({
path: '/api',
session: true, // active sessions
passport: true, // force automatic PassportGithub login
routes: [
new Route({
path: 'guilds/:guild/join',
method: 'GET',
}, function(gw) {
if (ObjectID.isValid(gw.params.guild)) {
project.DB.collection('guilds').update({
_id: (ObjectID.createFromHexString(gw.params.guild)),
guilders: {$ne: gw.user._id},
}, {
$push: {guilders: {_id: gw.user._id}},
}).then(function(query) {
if (query.result.nModified != 1) {
// gw.statusCode = 406;
// gw.json({error:gw.i18n('pillars.statusCodes',{code:gw.statusCode})});
gw.error(406);
} else {
// gw.json({result:1});
gw.redirect('/guilds');
}
}).catch(function(error) {
// gw.statusCode = 500;
// gw.json({error:error});
gw.error(500, error);
});
} else {
gw.error(400);
}
}),
],
}));
// Guilds view
project.routes.add(new Route({
path: '/guilds',
session: true, // active sessions
passport: true, // force automatic PassportGithub login
}, function(gw) {
Promise.all([
project.DB.collection('users').find({}).project({}).toArray(),
project.DB.collection('guilds').find({}).project({}).toArray(),
]).then(function(results) {
const users = results[0];
const guilds = results[1];
gw.render('./templates/home.pug', {
users: function(_id) {
return users.find(function(user) {
return user._id.toString() === _id.toString();
});
},
inGuilders: function(guilders = []) {
return guilders.find(function(guilder) {
return guilder._id.toString() === gw.user._id.toString();
});
},
guilds,
});
}).catch(function(error) {
gw.error(500, error);
});
}));
// Static directory service
project.routes.add(new Route({
id: 'static',
path: '/*:path',
directory: {
path: './static',
listing: true,
},
}));