forked from coderwassananmol/BUB2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
233 lines (207 loc) · 6.28 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
#!/usr/bin/env node
const express = require("express");
const next = require("next");
const bodyParser = require("body-parser");
const cors = require("cors");
const open = require("open");
const compression = require("compression");
require("dotenv").config();
const dev = process.env.NODE_ENV !== "production";
const PORT = process.env.PORT || 5000;
const GB_KEY = process.env.GB_KEY;
const app = next({
dev,
});
const handle = app.getRequestHandler();
var emailaddr = "";
const { customFetch } = require("./utils/helper.js");
const { queueData } = require("./utils/helper.js");
const { checkForPublicDomain } = require("./controller/GB");
const Arena = require("bull-arena");
const GoogleBooksProducer = require("./bull/google-books-queue/producer");
const PDLProducer = require("./bull/pdl-queue/producer");
const { exec } = require("child_process");
const config = require("./utils/bullconfig");
app
.prepare()
.then(() => {
const server = express();
//Parse application/x-www-form-urlencoded
server.use(
bodyParser.urlencoded({
extended: true,
})
);
//Parse application/json
server.use(bodyParser.json());
//Enable and use CORS
server.use(
cors({
credentials: true,
origin: true,
})
);
server.use(compression());
const arenaConfig = Arena(
{
queues: [
{
// Name of the bull queue, this name must match up exactly with what you've defined in bull.
name: "pdl-queue",
// Redis auth.
redis: {
port: process.env.redisport,
host: process.env.redishost,
},
},
],
},
{
// Make the arena dashboard become available at {my-site.com}/arena.
basePath: "/arena",
// Let express handle the listening.
disableListen: true,
}
);
server.use(arenaConfig);
/**
* Every custom route that we build needs to arrive before the * wildcard.
* This is necessary because otherwise the server won't recognise the route.
*/
server.get("/getstats", async (req, res) => {
const pdl_queue = await config.getNewQueue("pdl-queue").getJobCounts();
const google_books_queue = await config
.getNewQueue("google-books-queue")
.getJobCounts();
const queryParams = { pdl_queue, google_books_queue };
res.send(queryParams);
});
server.get('/getqueue',async (req,res) => {
const pdl_queue = await config.getNewQueue('pdl-queue');
const google_books_queue = await config.getNewQueue('google-books-queue')
const queryParams = {
"gb-queue": {
active: {},
waiting: {},
},
"pdl-queue": {
active: {},
waiting: {},
},
};
const pdlqueue_active_job = await pdl_queue.getActive(0);
const pdlqueue_waiting_job = await pdl_queue.getWaiting(0);
const gbqueue_active_job = await google_books_queue.getActive(0);
const gbqueue_waiting_job = await google_books_queue.getWaiting(0);
queryParams["pdl-queue"]["active"] = await queueData(
pdlqueue_active_job,
pdl_queue
);
queryParams["pdl-queue"]["waiting"] = await queueData(
pdlqueue_waiting_job,
pdl_queue
);
queryParams["gb-queue"]["active"] = await queueData(
gbqueue_active_job,
google_books_queue
);
queryParams["gb-queue"]["waiting"] = await queueData(
gbqueue_waiting_job,
google_books_queue
);
res.send(queryParams);
});
let GBdetails = {};
server.get("/check", async (req, res) => {
const { bookid, option, email } = req.query;
emailaddr = email;
switch (option) {
case "gb":
customFetch(
`https://www.googleapis.com/books/v1/volumes/${bookid}?key=${GB_KEY}`,
"GET",
new Headers({
"Content-Type": "application/json",
})
).then((data) => {
const { error } = checkForPublicDomain(data, res);
if (!error) {
GBdetails = data;
}
});
break;
case "obp":
res.send({
error: false,
message: "You will be mailed with the details soon!"
});
case "pn":
//Check for duplicates
const { categoryID } = req.query;
res.send({
error: false,
message: "You will be mailed with the details soon!",
});
PDLProducer(bookid, categoryID, email);
// const isDuplicate = checkForDuplicatesFromIA(`bub_pn_${bookid}`);
// isDuplicate.then(resp => {
// if (resp.response.numFound != 0) {
// res.send({
// error: true,
// message: "The document already exists on Internet Archive."
// })
// }
// else {
// }
// })
break;
}
});
server.post("/webhook", async (req, res) => {
exec(
"git pull; yes | npm install; webservice --backend kubernetes node10 restart",
(err, stdout, stderr) => {
if (err) {
console.log("::err::", err);
} else if (stderr) {
console.log("::stderr::", stderr);
} else {
console.log("::stdout::".stdout);
}
}
);
res.send();
});
server.post("/download", async (req, res) => {
res.send({
error: false,
message: "You will be mailed with the details soon!",
});
GoogleBooksProducer(req.body.url, GBdetails, emailaddr);
// download.downloadFromGoogleBooks(
// req.body.url,
// GBdetails,
// emailaddr
// );
});
/**
* The express handler for default routes.
*/
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(PORT, (err) => {
if (err) throw err;
console.log(`> Ready on port ${PORT}`);
if(dev){
(async () => {
console.log(`opening into browser: http://localhost:${PORT}/`);
await open(`http://localhost:${PORT}/`);
})();
}
});
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});