/
app.js
212 lines (190 loc) · 5.29 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
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
var http = require('http'),
util = require('util'),
mu = require('mu2'),
jsdom = require('jsdom'),
$ = require('jquery'),
async = require('async'),
director = require('director'),
send = require('send'),
url = require('url'),
auth = require('http-auth'),
//mongodb settings
dbserver = '',
dbport = ,
dbname = '',
dbuser = '',
dbpass = '',
collection_name = '',
mongodb = require('mongodb'),
BSON = mongodb.BSONPure,
mongodbServer = new mongodb.Server(dbserver, dbport, {
safe: false
}),
//only known by author
editable = auth({
authRealm: "Do you have permit to edit this?",
authList: ['username:password']
}),
//server settings
svrport = 999;
mu.root = __dirname + '/templates'
//connect database
new mongodb.Db(dbname, mongodbServer, {w: 1}).open(function(error, client) {
if (error) throw error;
client.authenticate(dbuser, dbpass, function(err, val) {
if (err) throw error;
else {
//select collection
var collection = new mongodb.Collection(client, collection_name);
console.log("Database Connected!")
var router = new director.http.Router();
//create
router.get('/', function() {
var res = this.res;
async.series([ dashboard_recent_slides ], function(err, results) {
var recent_slides = results[0];
var stream = mu.compileAndRender('index.html', {recent_slides: recent_slides});
stream.pipe(res);
})
})
//read
router.get('/pre/:id', function(id) {
var res = this.res;
collection.find({_id: new BSON.ObjectID(id)}, function(err, data) {
data.toArray(function(key, val) {
if(val.length >= 1) {
var markdown = val[0].markdown;
var stream = mu.compileAndRender('presentation.html', {_id: id, data: markdown});
stream.pipe(res);
}
else {
res.end("not found")
}
})
});
})
//update
router.post('/save', function () {
var res = this.res;
res.writeHead(200, { 'Content-Type': 'application/json' })
var id = this.req.body._id;
var md = this.req.body.markdown;
var title = this.req.body.title;
if(id != "") {
collection.update({_id: new BSON.ObjectID(id)}, {$set: {title: title, markdown: md}}, function(err) {
if (err) {
console.log('Failed to update');
} else {
console.log('Successfully update ('+id+')');
}
res.end();
})
}
else {
collection.insert({title: title, markdown: md}, function(err, data) {
if (data) {
console.log('Successfully insert ('+data[0]._id.toString()+')');
} else {
console.log('Failed to insert');
}
res.end(data[0]._id.toString());
})
}
})
//update page
router.get('/edit/:id', function(id) {
var res = this.res;
var req = this.req;
editable.apply(req, res, function(username) {
async.series([
dashboard_recent_slides,
function(callback) {
collection.find({_id: new BSON.ObjectID(id)}, function(err, data) {
data.toArray(function(key, val) {
if(val.length >= 1) {
var title = decodeURIComponent(val[0].title);
var markdown = decodeURIComponent(val[0].markdown);
callback(null, {title: title, markdown: markdown});
}
else {
callback(null, "")
}
})
});
}], function(err, results) {
var recent_slides = results[0];
var nowSlide = results[1];
if(nowSlide != "") {
var stream = mu.compileAndRender('index.html', {_id: id, title: nowSlide.title, data: nowSlide.markdown, recent_slides: recent_slides});
stream.pipe(res);
}
else {
res.end("not found");
}
})
})
})
//delete
router.post('/delete', function () {
var res = this.res;
var id = this.req.body._id;
if(id != "") {
collection.remove({_id: new BSON.ObjectID(id)}, function(err, removed){
if(err) {
console.log('Failed to delete ('+id+')');
res.end();
} else {
console.log('Successfully delete ('+id+')');
res.end(id);
}
});
}
else {
console.log('Nothing to delete');
res.end();
}
})
//serve files from assets/
router.get(/\/assets[^*]*/, function() {
var res = this.res;
var req = this.req;
send(req, url.parse(req.url).pathname)
.root(__dirname)
.on('error', function(err) {
res.statusCode = err.status || 500;
res.end(err.message);
})
.on('directory', function() {
res.statusCode = 301;
res.setHeader('Location', req.url + '/');
res.end('Redirecting to ' + req.url + '/');
})
.pipe(res);
});
var dashboard_recent_slides = function(callback) {
var arr = [];
collection.find({}).sort('_id','descending').limit(15).toArray(function(key, val) {
$.each(val, function(k, v) {
arr.push({_id: v._id, title: decodeURIComponent(v.title)});
if(k == val.length - 1) {
callback(null, arr);
}
})
});
};
http.createServer(function (req, res) {
mu.clearCache();
req.chunks = [];
req.on('data', function (chunk) {
req.chunks.push(chunk.toString());
});
router.dispatch(req, res, function (err) {
if (err) {
res.writeHead(404);
res.end();
}
});
}).listen(svrport, null);
}
});
});