-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsite.js
101 lines (91 loc) · 3.65 KB
/
site.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
/*jslint regexp:false */
// gogled
// shape server. Look up pages by key or add to them.
//
// This is so not-RESTy that it's not even funny. Having URLs each be own
// resources would be mighty inconvenient, so we'll just do like this:
// http://server/?lookup=http%3a%2f%2fgoogle.com%2f
//
// This page handles (should handle!) all of the view functions.
var http = require('http'),
url = require('url'),
Pagestore = require('./models/pagestore').Pagestore,
Shape = require('./models/shape').Shape,
bmr = require('./views/bookmarklet_renderer'),
staticfiles = require('./views/static'),
view = require('./views/view_helpers'),
fading = require('./models/fading.js'),
switchboard = require('./views/switchboard');
exports.makeGogglesServer = function(conf) {
var ps = new Pagestore(conf.storeDir, conf.emptyCbTimeout);
bmr.load();
if (conf.closure) {
bmr.closureCompile();
}
if (conf.fade) {
fading.fade(ps);
}
function receive(req, res) {
// this actually sends the response back
function render(data) { return view.renderJson(req, res, data); }
try {
return switchboard.dispatch(req, res, url.parse(req.url).pathname, {
'': staticfiles.makeSingleFileServer('resources/index.htm'),
'img': staticfiles.makeFileServer('resources'),
'css': staticfiles.makeFileServer('resources'),
'favicon.ico': function(){
res.writeHead(404); res.end("No favicon here");
},
'bookmarklet.js': bmr.makeRenderer(req, res),
'page': switchboard.makeDispatchQueryOverloader(
['add', 'page','t','r','g','b','a','p'],
function(req,res, add,page,t,r,g,b,a,p){
var shape = Shape.fromParams(p, t, r, g, b, a);
if (shape) {
console.log((req.headers['x-forwarded-for']||req.connection.remoteAddress)+" +++ "+page);
return ps.addShapeToPage(page, shape, render);
} else {
view.failWith(req, res, "One or more of your shape paramaters is invalid.");
}
},
['del', 'page','id'],
function(req,res, del,page,id){
// I know it's stupid to delete the shape by passing in every
// parameter...
console.log((req.headers['x-forwarded-for']||req.connection.remoteAddress)+" --- "+page);
try {
var p = parseInt(id, 10);
if (isNaN(p)) {
return view.failWith(req, res, "One or more of your shape paramaters is invalid.");
} else {
return ps.deleteShapeFromPage(page, parseInt(id, 10), render);
}
} catch(e) {
view.failWith(req, res, "One or more of your shape paramaters is invalid.");
}
},
['stream', 'page'],
function(req,res, stream,page){
// Stream shape updates =3
return ps.streamPageUpdates(page, parseInt(stream, 10), render);
},
['page'],
function(req,res, page){
// Just lookup the page
return ps.getPageInfo(page, render);
},
[],
function(rq,rs){
view.failWith(req, res, "unknown action");
}
)
});
} catch(e) {
console.log(e.stack);
return view.failWith(req, res, "Server error -- please try again");
}
}
var svr = http.createServer(receive);
svr.listen(conf.port||8002, "0.0.0.0");
return svr;
};