-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
145 lines (110 loc) · 3.88 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
var express = require('express');
var bodyParser = require('body-parser');
var SearchEngine = require('epub-full-text-search');
var SampleService = function () {
var self = this;
self.app = express();
function setupVariables() {
self.ipaddress = process.env.OPENSHIFT_NODEJS_IP || process.env.IP;
self.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 8080;
if (typeof self.ipaddress === "undefined")
self.ipaddress = "127.0.0.1";
}
//watchForUpdateIndex(service, index, epubs);
function createRoutes() {
self.routes = {};
self.routes['/search'] = function (req, res) {
if (!req.query['q']) {
res.status(500).send('Can`t found query parameter q -> /search?q=word');
return;
}
var q = req.query['q'].toLowerCase().split(/\s+/);
var bookTitle = req.query['t'];
bookTitle = bookTitle || '*'; // if bookTitle undefined return all hits
self.se.search(q, bookTitle, function (result) {
res.send(result);
});
};
self.routes['/matcher'] = function (req, res) {
if (!req.query['beginsWith']) {
res.status(500).send('Can`t found query parameter beginsWith -> /matcher?beginsWith=word');
return;
}
self.se.match(req.query['beginsWith'], function (err, matches) {
res.send(matches);
});
};
}
function initServer() {
createRoutes();
self.app.use(bodyParser.urlencoded({extended: true}));
self.app.use(bodyParser.json());
//var servingPath = process.cwd() + '/dist/cloud-reader';
var servingPath = process.cwd();
console.log("serving static files: " + servingPath);
// serving readium
self.app.use(express.static(servingPath));
for (var r in self.routes) {
self.app.get(r, self.routes[r]);
}
}
function terminator(sig) {
if (typeof sig === "string") {
console.log('%s: Received %s - terminating service ...',
Date(Date.now()), sig);
process.exit(1);
}
console.log('%s: Node server stopped.', Date(Date.now()));
}
function setupTerminationHandlers() {
// Process on exit and signals.
process.on('exit', function () {
terminator();
});
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function (element, index, array) {
process.on(element, function () {
terminator(element);
});
});
}
self.startIndexing = function () {
var epubs = 'epub_content';
self.se = new SearchEngine();
self.se.indexing(epubs, function (info) {
console.log(info);
});
};
self.init = function () {
setupVariables();
setupTerminationHandlers();
initServer();
};
self.start = function () {
// Start the app on the specific interface (and port).
self.app.listen(self.port, self.ipaddress, function () {
console.log('%s: Node server started on %s:%d ...',
Date(Date.now()), self.ipaddress, self.port);
});
};
};
var sase = new SampleService();
sase.startIndexing();
sase.init();
sase.start();
//function watchForUpdateIndex(se, index, epubContent) {
//
// var chokidar = require('chokidar'); // watch for changes in directory
//
// chokidar.watch(epubContent, {
// ignored: /[\/\\]\./,
// persistent: true
// }).on('all', function (event, path) {
//
// se.indexing(epubContent, function (info) {
// console.log(info);
// });
//
// });
//}