-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.node.js
More file actions
254 lines (181 loc) · 5.47 KB
/
server.node.js
File metadata and controls
254 lines (181 loc) · 5.47 KB
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
// node.js
/*
*/
/*
testing:
*/
// connect to db,
var async = require("async");
var urlparser = require("url");
var fs = require("fs");
var pathparser = require("path");
var jsdom = require("jsdom");
$ = require("jquery")(jsdom.jsdom().createWindow());
var secrets = require("./secrets").secrets();
var site = secrets.hackpad_name;
// hp_clientid and hp_secret
var Hackpad = require("hackpad");
var port = secrets.port;
if(process && process.env && process.env.NODE_ENV == "production"){
port = secrets.prod_port;
}
// create some sample objects, put in couchdb
var client = new Hackpad(secrets.hp_clientid, secrets.hp_secret, {site: site});
var pads = {};
updateList(startServer);
function updateList(doneCallback){
pads = {};
client.list(function(error, list){
var titleReg = /^# (.*)\n/;
// eg [Accessibility](/lWrAuo7a0Uf)
var linksReg = /\[([^\]]+)]\(\/([^\)]+)\)/g;
console.log("got list" + list.length);
// console.log(list);
async.eachSeries(list, function(padid, callback){
console.log("\n\n+++value " + padid);
client.export(padid, null, 'md', function(error, content){
/*
console.log("\n\n\n\n*********************************************** content");
console.log("padid : " + padid);
console.log(error);
*/
console.log(content);
// parse content from md
var pad;
if(pads[padid]){
pad = pads[padid];
}else{
pad = {padid : padid, numLinks: 0, numInLinks: 0, links: {}, inLinks : {}};
}
var matches = content.match(titleReg);
if(matches){
pad.title = matches[1];
}else{
console.log("page no title");
callback();
return;
}
var linkMatches;
while (linkMatches = linksReg.exec(content)){
var linkTitle = linkMatches[1];
if(linkTitle.trim() != ""){
var linkPadID = linkMatches[2];
if(linkPadID != padid){ // no self-links!
linkPadID = linkPadID.split("#")[0];
var link = {title: linkTitle, padid : linkPadID};
if(!pad.links[linkPadID]){
pad.links[linkPadID] = link;
pad.numLinks++;
}
// need inboiund links as well.
var destPad= pads[linkPadID];
if(!destPad){
destPad = {padid : linkPadID, title: linkTitle, links: {} , inLinks: {} , numLinks: 0, numInLinks : 0};
pads[linkPadID] = destPad;
}
if(!destPad.inLinks){
destPad.inLinks = {};
}
if(!destPad[padid]){
destPad.inLinks[padid] = padid;
destPad.numInLinks++;
}
}
}
}
console.log(pad);
pads[padid] = pad;
callback();
});
},
doneCallback
);
});
}
var started = false;
function startServer(){
if(!started){
started = true;
}else{
console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! already started!");
return;
}
console.log(JSON.stringify(pads, null, ' '));
console.log("starting server");
var http = require('http');
http.createServer(function (req, res) {
parseRequest(req, res);
}).listen(port);
console.log('Server running at port ' + port);
console.log("pads length" + pads.length);
}
function parseRequest(req, res){
console.log("got request");
var parsed = urlparser.parse(req.url, true)
var query = urlparser.parse(req.url, true).query;
console.log('~~~~~~~~~~~~~~~~~');
// console.log(parsed);
console.log('~~~~~~~~~~~~~~~~~');
//console.log(query);
console.log('~~~~~~~~~~~~~~~~~');
if(!query.action){
sendFile(parsed.pathname, query, res);
}else if (query.action == "listpads"){
listpads(query, res);
}else if (query.action == "updatelist"){
updateList(function (){
console.log("%%%%%%%%%%%%%%%%%%%%%%%%updating happened!");
res.writeHead(200, {'Content-Type': 'text/html'});
res.end("<html><body>list updated</body></html>");
});
}else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.end("<html><body><pre>not sure what to do</pre></body></html>");
}
}
function listpads(query, res){
res.writeHead(200, {'Content-Type' : 'application/json'});
res.end(JSON.stringify(pads));
}
var dataCache = {};
function sendFile(path, query, res){
if(path == "/"){
path = "/index.html";
}
var extname = pathparser.extname(path);
var contentType = 'text/html';
if(path.match(/secrets\.js/)){
res.writeHead(404, {'Content-Type': contentType});
//indexhtml = data;
res.end("I'm afraid I can't do that.");
return;
}
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
}
if(!dataCache[path]){
fs.readFile("."+path, function(err, data){
if(err){
console.log("file read error");
console.log(err);
res.writeHead(404, {'Content-Type': contentType});
//indexhtml = data;
res.end(data);
}else{
res.writeHead(200, {'Content-Type': contentType});
console.log("writing file " + path);
// console.log(data);
//dataCache[path] = data;
res.end(data);
}
});
}else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(dataCache[path]);
}
}