-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
107 lines (87 loc) · 2.7 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
"use strict";
var
config = require('./config'),
_ = require('lodash'),
cabinet = require('cabinet'),
express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
fs = require('fs'),
md = require("node-markdown").Markdown,
sio = require('socket.io').listen(server),
mongoose = require('mongoose'),
redis = require('redis'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
Gnd = require('gnd');
console.log('Using: '+Gnd.lib);
app.use(cabinet(__dirname + '/public', {
files: {
'/lib/gnd.js': Gnd.debug,//Gnd.lib,
'/lib/third/lodash.js': Gnd.third.underscore,
'/lib/third/curl.js': Gnd.third.curl,
},
}));
app.use(cabinet(Gnd.docs, {
prefix: '/api'
}));
app.set('views', __dirname + '/views');
app.get('/', function(req, res){
fs.readFile(Gnd.readme, {encoding: 'utf8'}, function(err, text){
if(!err){
res.render('index.jade', {content: md(text.toString())});
}else{
console.log(err);
res.send('Error generating page, please try later on...');
}
});
});
app.get('/demos/table', function(req,res) {
res.render('demos/table.jade',{ pretty: true });
})
app.get('/demos/list', function(req,res) {
res.render('demos/list.jade',{ pretty: true });
})
app.get('/demos/route', function(req,res) {
res.render('demos/routes.jade');
})
app.get('/demos/chat', function(req,res) {
res.render('demos/chat/index.jade',{ pretty: true });
})
//
// Define Models (redundant with chat example, TODO: refactor)
//
var MessageSchema = new Gnd.Schema({
text: {type: String},
ts: {type: Number}
});
var Message = Gnd.Model.extend('messages', MessageSchema);
var RoomSchema = new Gnd.Schema({
name : {type: String},
ts: {type: Number},
messages: new Gnd.CollectionSchemaType(Message, 'messages'),
url: String
});
var Room = Gnd.Model.extend('rooms', RoomSchema);
var ChatSchema = new Gnd.Schema({
rooms: new Gnd.CollectionSchemaType(Room, 'rooms')
});
var Chat = Gnd.Model.extend('chats', ChatSchema);
var models = {
messages: Message,
rooms: Room,
chats: Chat
};
// Setup mongodb
mongoose.connect(config.mongo.href);
var mongooseStorage = new Gnd.Storage.MongooseStorage(mongoose, models);
var pubClient = redis.createClient(config.redis.port, config.redis.hostname),
subClient = redis.createClient(config.redis.port, config.redis.hostname);
var syncHub = new Gnd.Sync.Hub(pubClient, subClient, sio.sockets);
var gndServer = new Gnd.Server(mongooseStorage, new Gnd.SessionManager(), syncHub);
var socketServer = new Gnd.SocketBackend(sio.sockets, gndServer);
app.set('port', config.port);
server.listen(app.get('port'), function(){
console.info('Express server listening on port ' + app.get('port'));
});