-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
79 lines (66 loc) · 2.2 KB
/
main.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
var apejs = require("apejs.js");
var memcache = require("memcache.js");
var googlestore = require("googlestore.js");
var httpget = require('./httpget.js')
var mustache = require("./common/mustache.js");
apejs.urls = {
"/cache-url": {
get: function(request, response) {
var p = param(request)
var url = p('url')
// check if url is in cache
var data = memcache.get(url)
if(data) {
return print(response).jsonp(data, p('callback'))
}
// check if url is in datastore
try {
var key = googlestore.createKey('url', url)
var urlEntity = googlestore.get(key)
} catch(e) { // entity wasn't found
}
if(urlEntity) {
var value = urlEntity.getProperty('value').getValue()
// add it to cache
memcache.put(url, value)
return print(response).jsonp(value, p('callback'))
}
// else download it from the web
var ret = httpget(url)
// add it to cache
memcache.put(url, ret);
// add it to datastore
var e = googlestore.entity('url', url, {
'value': new Text(ret)
})
googlestore.put(e)
return print(response).jsonp(ret, p('callback'))
}
}
}
// simple syntax sugar
function print(response) {
return {
// callback is a Java string that contains the name
// of the callback, so we can run JSONP if it exists
jsonp: function(jsonString, callback) {
if(response == null) return;
if(callback) { // JSONP
jsonString = "" + callback + "(" + jsonString + ");";
}
response.setContentType("application/json");
response.getWriter().println(jsonString);
return jsonString;
},
text: function(text) {
if(text) response.getWriter().println(text);
}
};
}
function param(request) {
return function(par) {
var p = request.getParameter(par);
if(p) return p;
else return false;
}
}