-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
255 lines (232 loc) · 6.22 KB
/
server.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
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
254
255
// !! we should probably factor common code out into its own files?
// load express and make a new express app
const sqlite3 = require("sqlite3");
const express = require("express");
const frontend = express();
// load config file
const config = require("./config");
// the sqlite database to be opened
var database;
// open the database and assert the existance of the tables
// exit on failure
function prepareDatabase()
{
// load the database from file
database = new sqlite3.Database(config.dbFilename, sqlite3.OPEN_READONLY, e => {
if(e) exit(`Error opening database: ${e}`);
});
};
// close the database if its open
function closeDatabase()
{
if(database) database.close().catch(console.error);
}
// take a timestamp and format it
function formatDate(timestamp)
{
return new Date(timestamp).toUTCString();
}
// format a massege for code blocks
function formatMessage(string, keywords)
{
const regex = /\`\`\`(([a-zA-Z]+)\n)?\n*([\s\S]*?)\n*\`\`\`/g;
var s = string.replace(regex, (match, p1, p2, p3, offset, string) =>
`<div class="code">${p2 ? `<span class="lang">#${p2}</span><br />` : ""}${p3.replace(/\n/g, "<br />")}</div>`);
const inlineRegex = /\`([^\`]+)\`/g;
s = s.replace(inlineRegex, (match, p1) => `<span class="inlineCode">${p1}</span>`);
const urlRegex = /(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))/g;
s = s.replace(urlRegex, (match, p1) => `<a href="${p1}">${p1}</a>`);
keywords.filter(word => word.length).forEach(word => s = s.replace(new RegExp(`(${word})`, "gi"), (match, p1) => `<span class="keyword">${p1}</span>`));
return s;
}
// make html output for a message
function makeMessagePage(rows, keywords)
{
const body = rows.map(row => `
<a class="perma" href="/code?msgId=${row.msgId}">permalink</a><br />
Author: ${row.author}<br />
Channel: ${row.channel}<br />` + row.revisions.map(revision => `
<span class="timestamp">${formatDate(revision.date)}</span>
<div class="messageBody">${formatMessage(revision.fullMessage, keywords)}</div>
`).join("<br /><br />")).join("<br /><br />");
return `
<style>
.inlineCode
{
font-family: monospace;
color: #EEE;
background-color: #555;
}
.keyword
{
background-color: yellow;
color: black;
}
.perma
{
font-size: small;
}
.wrapper
{
width: 400px;
}
.field
{
font-weight: bold;
}
.timestamp
{
font-size: small;
font-style: italic;
}
.lang
{
font-style: italic;
font-size: small;
background-color: #999;
color: #FFF;
}
.messageBody
{
color: #111;
background-color: #EEE;
border: solid 1px #CCC;
border-radius: 3px;
padding: 8px;
}
.code
{
font-family: monospace;
color: #EEE;
background-color: #555;
border: solid 1px #222;
border-radius: 3px;
padding: 3px;
}
</style>
<div class="wrapper">
${body}
</div>
`;
}
// search page
frontend.get("/", (request, response) => {
const sql = `SELECT DISTINCT author FROM Message`;
database.all(sql, [], (e, authors) => {
if(e) exit(`Error querying database: ${e}`);
else
{
const sql = `SELECT DISTINCT channel FROM Message`;
database.all(sql, [], (e, channels) => {
if(e) exit(`Error querying database: ${e}`);
else
{
response.send(`
<form action="/code/">
<label for="keywords">Keywords</label>
<input id="keywords" name="keywords"><br />
<label for="author">Author</label>
<select id="author" name="author">
<option value="">*</option>
${authors.map(author => `<option value="${author.author}">${author.author}</option>`).join("")}
</select><br />
<label for="channel">Channel</label>
<select id="channel" name="channel">
<option value="">*</option>
${channels.map(channel => `<option value="${channel.channel}">${channel.channel}</option>`).join("")}
</select><br />
<label for="from">From</label>
<input id="from" name="from" type="date"><br />
<label for="to">To</label>
<input id="to" name="to" type="date"><br />
<label for="as">As</label>
<select id="as" name="as">
<option value="">Webpage</option>
<option value="json">JSON</option>
</select><br />
<input type="submit">
</form>
`);
}
});
}
});
});
function strtotime(string)
{
return new Date(string).getTime();
}
function containsKeywords(string, keywords)
{
return keywords.reduce((acc, word) => word === "" ? acc : acc && (string.toLowerCase().indexOf(word.toLowerCase()) != -1), true);
}
// filter messages query
frontend.get("/code/", (request, response) => {
var sql = `SELECT * FROM Message`;
const bindings = [];
var kw = "WHERE";
if(request.query.author)
{
sql += ` ${kw} author = ?`;
bindings.push(request.query.author);
kw = "AND";
}
if(request.query.channel)
{
sql += ` ${kw} channel = ?`;
bindings.push(request.query.channel);
kw = "AND";
}
if(request.query.msgId)
{
sql += ` ${kw} msgId = ?`;
bindings.push(request.query.msgId);
kw = "AND";
}
database.all(sql, bindings, (e, messageRows) => {
if(e) exit(`Error querying database: ${e}`);
else
{
const sql = `SELECT * FROM Content`;
database.all(sql, [], (e, contentRows) => {
if(e) exit(`Error querying database: ${e}`);
else
{
const keywords = request.query.keywords ? request.query.keywords.split(" ") : [];
var rows = messageRows;
rows = rows.map(row => {
row.revisions = contentRows
.filter(revision => revision.associatedMsg === row.msgId);
delete row.revisions.associatedMsg;
return row;
});
if(request.query.from)
rows = rows.filter(row => row.revisions[0].date >= strtotime(request.query.from));
if(request.query.to)
rows = rows.filter(row => row.revisions[0].date < strtotime(request.query.to));
if(request.query.keywords)
rows = rows.filter(row => row.revisions.reduce((acc, revision) => containsKeywords(revision.fullMessage, keywords), true));
if(request.query.as === "json") response.send(rows);
else response.send(makeMessagePage(rows, keywords));
};
});
};
});
});
// exit with error message
function exit(message)
{
console.error(message);
closeDatabase();
process.exit(1);
}
// start the server!
function main()
{
// open the database
prepareDatabase();
// start the server listening
frontend.listen(config.port, () => console.log(`Server running on port ${config.port}!`));
}
// go!
main();