forked from adamd/exor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (138 loc) · 4.07 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
var Path = require('path');
var Hapi = require('hapi');
// List of presidents
var presidents = ['clinton', 'bush', 'obama'];
var serverOptions = {
views: {
engines: {
html: require('handlebars')
},
path: Path.join(__dirname, 'templates')
}
};
var server = new Hapi.Server(~~process.env.PORT || 3000, '0.0.0.0', serverOptions);
// Home page
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.view('index');
}
});
// Images, CSS and other assets
server.route({
method: "GET",
path: "/assets/{path*}",
handler: {
directory: {
path: "./assets",
listing: false,
index: false
}
}
});
server.route({
method: 'GET',
path: '/search/{q}',
handler: function (request, reply) {
// Determine query
var search = "*";
search = encodeURIComponent(request.params.q);
var results = {};
var db = require('orchestrate')(process.env.OIOKEY_EXOR);
for (var i=0; i<presidents.length; i++) {
var pname = presidents[i];
var psearch = search + " AND president:" + pname;
// Call OiO
db.newSearchBuilder()
.collection('exors_fulltext')
.limit("0")
.query(psearch)
.then(function (result) {
results = save_result(results, result);
if (presidents.length == Object.keys(results).length) {
reply(results);
}
})
.fail(function (err) {
console.log('Oh sad, an error');
});
}
// Log search/results
var ip = request.raw.req.headers['x-forwarded-for'] ||
request.raw.req.connection.remoteAddress ||
request.raw.req.socket.remoteAddress ||
request.raw.req.connection.socket.remoteAddress;
var d = {
"term": search,
"searched_timestamp": Math.floor(new Date().getTime()/1000),
"ip": ip
};
db.post("search_log", d);
}
});
// Load executive order details for one president
server.route({
method: 'GET',
path: '/details/{p}/{q}/{pg?}',
handler: function (request, reply) {
// Settings
var rpp = 100; // Results per page
// Determine page
var pg = parseInt(request.params.pg) ? encodeURIComponent(request.params.pg) : 1;
var offset = rpp * (pg - 1);
// Determine query
search = encodeURIComponent(request.params.q);
// Determine president
var prez = request.params.p;
if (prez == "clinton" || prez == "bush" || prez == "obama") {
console.log("Details for " + request.params.p + " on search term:" + request.params.q);
search = search + " AND president:" + prez;
// Call OiO
var db = require('orchestrate')(process.env.OIOKEY_EXOR);
var sb = db.newSearchBuilder()
.collection('exors_fulltext')
//.sort("signed_date", "desc")
.limit(rpp);
if (offset > 0) {
sb.offset(offset);
}
sb.query(search)
.then(function (result) {
var orders = filter_details(result.body);
var pgs = Math.ceil(result.body.total_count / rpp);
var results = {"pg": pg, "pgs": pgs, "results": orders};
reply(results);
})
.fail(function(err) {
console.log('Oh sad, an error on details');
});
}
else {
reply("{'error': 'President not supported'}");
}
}
});
function filter_details(rjson) {
orders = [];
for (var i=0; i<rjson.results.length; i++) {
var eo = rjson.results[i].value;
delete eo.text;
delete eo.id;
delete eo.president;
orders[orders.length] = eo;
}
return orders;
}
function save_result(robj, rjson) {
for (var i=0; i<presidents.length; i++) {
var pname = presidents[i];
if (rjson.req.path.indexOf("president%3A" + pname) > -1) { // Check for "president%3A" + pname
robj[pname] = rjson.body.total_count;
return robj;
}
}
}
server.start(function () {
console.log('Server running at:', server.info.uri);
});