-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
62 lines (45 loc) · 1.38 KB
/
main.cpp
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
#include "pinhawiki.h"
#define COMMAND_LINE_INTERFACE_MODE false
#if COMMAND_LINE_INTERFACE_MODE
int main() {
command_line_interface::Run();
return 0;
}
// Code below is for running the c++ addon for Node.JS
#else
#include <node.h>
string HandleClientRequest(string req) {
if (req.empty())
return "";
char req_type = req.back();
req.pop_back();
if (req_type == 'i') {
indexer::LoadEngine();
return "loaded";
}
else if (req_type == 'q') {
const int p_idx = req.find_last_of('p');
const int page_number = stoi(req.substr(p_idx + 1));
while (int(req.length()) > p_idx)
req.pop_back();
const string processed_query = preprocess::LowerAsciiSingleLine(req);
string ans = indexer::Query(processed_query, page_number);
return ans;
}
cout << "Error: unexpected req_type\n";
return "Error";
}
// Code below handles communication with Node.JS server (app.js)
void Boilerplate(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
v8::String::Utf8Value input(isolate, args[0]);
string output = HandleClientRequest(*input);
args.GetReturnValue().Set(
v8::String::NewFromUtf8(isolate, output.c_str()).ToLocalChecked()
);
}
void Initialize(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "HandleClientRequest", Boilerplate);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
#endif