-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
819 lines (729 loc) · 23.8 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
require("dotenv").config();
const mysql = require("mysql2/promise");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
const fs = require("fs");
const log4js = require("log4js");
const fg = require("fast-glob");
const jpgPath = "\\\\luxor\\data\\board\\Dev\\PCMR\\jpg_tables";
const pdfPath = "\\\\luxor\\data\\board\\Dev\\PCMR\\pdf_files";
const manualCsvPath = "\\\\luxor\\data\\board\\Dev\\PCMR\\manual_csv";
const csvPath = "\\\\luxor\\data\\board\\Dev\\PCMR\\csv_tables";
[jpgPath, pdfPath, manualCsvPath, csvPath].forEach((p) =>
fs.access(p, (err) => {
if (err) throw new Error(`Path ${p} is not accessible`);
}),
);
const app = express();
app.options("*", cors());
app.use(cors());
app.use("/pdf", express.static(pdfPath));
app.use("/jpg", express.static(jpgPath));
app.use("/csv", express.static(csvPath));
// LOGGING
log4js.configure({
appenders: {
default: { type: "file", filename: "access.log" },
console: { type: "console" },
},
categories: {
default: { appenders: ["default", "console"], level: "debug" },
},
});
const logger = log4js.getLogger();
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_DATABASE,
waitForConnections: true,
connectionLimit: 12,
queueLimit: 0,
});
app.use((req, res, next) => {
res.locals.pool = pool;
next();
});
// Index API
const tableIndex = async (req, res, next) => {
try {
const query = `
SELECT p.pdfId,
p.pdfName,
p.pdfSize,
p.filingId,
p.date,
p.totalPages,
p.status,
COUNT(t.correct_csv) AS tablesValidated,
COUNT(IF(t.relevancy = 0, 1, NULL)) AS tablesIrrelevant,
COUNT(IF(t.correct_csv IS NULL AND t.relevancy = 1, 1, NULL)) AS tablesNotValidated,
COUNT(t.tableId) AS tableCount
FROM pdfs p
LEFT JOIN
tables t ON p.pdfName = t.pdfName
GROUP BY p.pdfId
ORDER BY p.pdfId;
`;
const [result] = await res.locals.pool.execute(query);
res.json(result);
} catch (error) {
next(error);
}
};
// Table Extraction API
const getExtractionData = async (req, res, next) => {
try {
const { pdfName } = req.body;
const tablesQuery = `
SELECT headTable,
page,
pageHeight,
pageWidth,
parentTable,
tableId,
tableTitle,
x1,
x2,
y1,
y2,
level
FROM tables
WHERE pdfName = ?
ORDER BY page DESC, level DESC;
`;
const statusQuery = `
SELECT status
FROM pdfs
WHERE pdfName = ?;
`;
const tablesPromise = res.locals.pool.execute(tablesQuery, [pdfName]);
const pdfStatusPromise = res.locals.pool.execute(statusQuery, [pdfName]);
const [[tables], [pdfStatus]] = await Promise.all([tablesPromise, pdfStatusPromise]);
res.json({ tables, pdfStatus });
} catch (error) {
next(error);
}
};
const setPdfStatus = async (req, res, next) => {
try {
const { status, pdfName } = req.body;
const query = `
UPDATE pdfs
SET status = ?
WHERE pdfName = ?;
`;
const [result] = await res.locals.pool.execute(query, [status, pdfName]);
res.json(result);
} catch (error) {
next(error);
}
};
const insertTable = async (req, res, next) => {
try {
const {
tableId,
pdfName,
page,
tableTitle,
x1,
x2,
y1,
y2,
pageHeight,
pageWidth,
parentTable,
headTable,
level,
} = req.body;
const creatorIp = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
const query = `
INSERT INTO tables (tableId, pdfName, page, pageWidth, pageHeight, x1, y1, x2, y2, tableTitle,
parentTable, creatorIp, headTable, level)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
`;
const params = [
tableId,
pdfName,
page,
pageWidth,
pageHeight,
x1,
y1,
x2,
y2,
tableTitle,
parentTable,
creatorIp,
headTable,
level,
];
const [result] = await res.locals.pool.execute(query, params);
res.json(result);
} catch (error) {
next(error);
}
};
const deleteTable = async (req, res, next) => {
try {
const { tableId } = req.body;
const query = `
DELETE
FROM tables
WHERE tableId = ?;
`;
const [result] = await res.locals.pool.execute(query, [tableId]);
res.json(result);
} catch (error) {
next(error);
}
};
// Validation API
const getValidationData = async (req, res, next) => {
try {
const { pdfName, notFirstLoading } = req.body;
const csvsQuery = `
SELECT csvId, tableId, csvText, method
FROM csvs
WHERE tableId IN (
SELECT tableId
FROM tables
WHERE pdfName = ?
);
`;
const tablesQuery = `
SELECT parentTable, page, pdfName, relevancy, tableId, tableTitle, correct_csv
FROM tables
WHERE pdfName = ?
ORDER BY page, level;
`;
const tagsQuery = `
SELECT tb.tableId,
tg.tagId,
tg.tagName,
IF(tt.tagId IS NULL, 0, 1) AS count
FROM tables tb
CROSS JOIN
tags tg
LEFT JOIN
tables_tags tt ON (tb.tableId = tt.tableId
AND tg.tagId = tt.tagId)
WHERE tb.pdfName = ?
ORDER BY tableId, tagId;
`;
if (notFirstLoading) {
const tablesPromise = res.locals.pool.execute(tablesQuery, [pdfName]);
const tagsPromise = res.locals.pool.execute(tagsQuery, [pdfName]);
const [[tables], [tags]] = await Promise.all([tablesPromise, tagsPromise]);
res.json({ tables, tags });
return;
}
const csvsPromise = res.locals.pool.execute(csvsQuery, [pdfName]);
const tablesPromise = res.locals.pool.execute(tablesQuery, [pdfName]);
const tagsPromise = res.locals.pool.execute(tagsQuery, [pdfName]);
const [[csvs], [tables], [tags]] = await Promise.all([csvsPromise, tablesPromise, tagsPromise]);
res.json({ csvs, tables, tags });
} catch (error) {
next(error);
}
};
// Getting all tables in chain
const getAllTablesInChain = async (headTableId, res) => {
const query1 = `
SELECT tableId, parentTable, level
FROM tables
WHERE headTable = ?
ORDER BY level;
`;
const [tablesResult] = await res.locals.pool.execute(query1, [headTableId]);
return tablesResult.map((t) => t.tableId);
};
// Setting validation to tables
const setValidationForOne = async (tableId, csvId, res) => {
const query = `
UPDATE tables
SET correct_csv = ?
WHERE tableId = ?;
`;
return res.locals.pool.execute(query, [csvId, tableId]);
};
const setValidation = async (req, res, next) => {
try {
const { tableId, csvId, method, isHeadTable } = req.body;
let pairsToProcess = [[tableId, csvId]];
if (isHeadTable && csvId === null) {
const tableIds = await getAllTablesInChain(tableId, res);
pairsToProcess = tableIds.map((t) => [t, null]);
} else if (isHeadTable) {
const tableIds = await getAllTablesInChain(tableId, res);
const query = `
SELECT t.tableId, c.csvId
FROM csvs c
INNER JOIN tables t USING (tableId)
WHERE c.tableId IN (${Array(tableIds.length).fill("?")})
AND method = ?;
`;
const [result] = await res.locals.pool.execute(query, [...tableIds, method]);
pairsToProcess = result.map((r) => [r.tableId, r.csvId]);
}
const promises = pairsToProcess.map(([tableId_, csvId_]) => setValidationForOne(tableId_, csvId_, res));
await Promise.all(promises);
res.json({ result: "Set Validation OK", ...req.body });
} catch (error) {
next(error);
}
};
// Setting relevancy to tables
const setRelevancyForOne = async (relevancy, tableId, res) => {
const query1 = `
UPDATE tables
SET relevancy = ?
WHERE tableId = ?;
`;
const query2 = `
DELETE
FROM tables_tags
WHERE tableId = ?;
`;
const promise1 = res.locals.pool.execute(query1, [relevancy, tableId]);
const promise2 = res.locals.pool.execute(query2, [tableId]);
return Promise.all([promise1, promise2]);
};
const setRelevancy = async (req, res, next) => {
try {
const { tableId, relevancy, isHeadTable } = req.body;
let tableIds = [tableId];
if (isHeadTable) {
tableIds = await getAllTablesInChain(tableId, res);
}
const promises = tableIds.map((id) => setRelevancyForOne(relevancy, id, res));
await Promise.all(promises);
res.json({ result: "Set Relevancy and Removed Tags OK", ...req.body });
} catch (error) {
next(error);
}
};
// Tagging of tables
const getTagsForTable = async (tableId, res) => {
const query2 = `
SELECT tagId
FROM tables_tags
WHERE tableId = ?;
`;
const [tagsResult] = await res.locals.pool.execute(query2, [tableId]);
return tagsResult.map((t) => t.tagId);
};
const deleteAllTags = async (tableIds, res) => {
const query3 = `
DELETE
FROM tables_tags
WHERE tableId IN (${Array(tableIds.length).fill("?")});
`;
return res.locals.pool.execute(query3, [...tableIds]);
};
const assignTagsToTables = async (tableIds, tagIds, res) => {
const query4 = `
INSERT INTO tables_tags (tableId, tagId)
VALUES (?, ?);;
`;
const promises = [];
tableIds.forEach((table) => {
tagIds.forEach((tag) => {
promises.push(res.locals.pool.execute(query4, [table, tag]));
});
});
return Promise.all(promises);
};
const insertRemoveTag = async (tableId, tagId, set, res) => {
let query = `
INSERT INTO tables_tags (tableId, tagId)
VALUES (?, ?);
`;
if (!set) {
query = `
DELETE
FROM tables_tags
WHERE tableId = ?
AND tagId = ?;
`;
}
return res.locals.pool.execute(query, [tableId, tagId]);
};
const tagUntagTable = async (req, res, next) => {
try {
const { tableId, tagId, set, isHeadTable } = req.body;
await insertRemoveTag(tableId, tagId, set, res);
if (isHeadTable) {
const [tables, tags] = await Promise.all([getAllTablesInChain(tableId, res), getTagsForTable(tableId, res)]);
await deleteAllTags(tables, res);
await assignTagsToTables(tables, tags, res);
}
res.json({ result: "Tagged OK", ...req.body });
} catch (error) {
next(error);
}
};
// Postprocessing API
const processingIndex = async (req, res, next) => {
try {
const query = `
WITH manuals AS (
SELECT t.headTable
FROM tables t
LEFT JOIN tables_tags tt ON t.tableId = tt.tableId AND tt.tagId = 13
WHERE relevancy = 1
GROUP BY t.headTable
HAVING count(tt.tagId) > 0
AND count(t.tableId) = count(tt.tagId))
SELECT t.pdfName,
t.headTable,
MIN(t.page) AS page,
COUNT(c.accepted_text) AS accepted,
COUNT(c.processed_text) AS processed,
COUNT(c.csvId) AS totalTables,
IF(m.headTable IS NULL, '', 'true') AS allManuals,
CASE
WHEN COUNT(c.accepted_text) = COUNT(c.csvId) THEN '3. DONE'
WHEN COUNT(c.accepted_text) > 0 THEN '1. IN PROGRESS'
ELSE '2. NOT STARTED'
END AS status,
CASE
WHEN COUNT(c.accepted_text) != COUNT(c.csvId) THEN 'disabled'
WHEN COUNT(c.accepted_text) = 1 THEN 'single table'
WHEN COUNT(IF(c.appendStatus = 0, NULL, 1)) + 1 = COUNT(c.accepted_text) THEN 'done'
ELSE 'pending'
END AS appendStatus
FROM tables t
INNER JOIN csvs c
ON t.correct_csv = c.csvId
LEFT JOIN manuals m ON t.headTable = m.headTable
WHERE relevancy = 1
GROUP BY t.headTable
ORDER BY status, accepted DESC, allManuals, processed DESC, totalTables DESC;
`;
const [result] = await res.locals.pool.execute(query);
res.json(result);
} catch (error) {
next(error);
}
};
const getSequence = async (req, res, next) => {
try {
const { headTable } = req.body;
const tagsQuery = `
SELECT tagId, tagName
FROM tags;
`;
const headQuery = `
SELECT tableTitle, pdfName, page
FROM tables
WHERE tableId = ?;
`;
const tablesQuery = `
SELECT t.tableId,
t.level,
c.processed_text,
c.accepted_text,
c.csvId,
t.level,
c.csvText,
if(
(json_arrayagg(tt.tagId) = cast('[
null
]' AS json)),
CAST('[]' AS json),
json_arrayagg(tt.tagId)
) AS tags
FROM tables t
INNER JOIN csvs c ON t.correct_csv = c.csvId
LEFT JOIN tables_tags tt ON t.tableId = tt.tableId
WHERE headTable = ?
GROUP BY t.tableId, t.level
ORDER BY t.level;
`;
const p = res.locals.pool;
const tagsPromise = p.execute(tagsQuery);
const headPromise = p.execute(headQuery, [headTable]);
const tablesPromise = p.execute(tablesQuery, [headTable]);
const [[tagsList], [head], [tables]] = await Promise.all([tagsPromise, headPromise, tablesPromise]);
res.json({ tagsList, head, tables });
} catch (error) {
next(error);
}
};
const setAccepted = async (req, res, next) => {
try {
const { csvId, newAccepted } = req.body;
const newAcceptedJSON = newAccepted === null ? null : JSON.stringify(newAccepted);
const tagsQuery = `
UPDATE csvs
SET accepted_text = ?
WHERE csvId = ?;
`;
const p = res.locals.pool;
await p.execute(tagsQuery, [newAcceptedJSON, csvId]);
res.json({ result: "Set accepted_text OK", ...req.body });
} catch (error) {
next(error);
}
};
// Concatenation
const getConcatSequence = async (req, res, next) => {
try {
const { headTable } = req.body;
const headQuery = `
SELECT tableTitle,
pdfName,
page,
coalesce(concatenatedText, cast('[]' AS json)) AS concatenatedText,
coalesce(combinedConText, cast('[]' AS json)) AS combinedConText
FROM tables
WHERE tableId = ?;
`;
const tablesQuery = `
SELECT level,
t.tableId,
csvId,
accepted_text,
if(tt.tagId IS NULL, 0, 1) AS noHeaders,
appendStatus
FROM tables t
INNER JOIN csvs c ON t.correct_csv = c.csvId
LEFT JOIN tables_tags tt ON t.tableId = tt.tableId AND tt.tagId = 1
WHERE headTable = ?
ORDER BY level;
`;
const p = res.locals.pool;
const headPromise = p.execute(headQuery, [headTable]);
const tablesPromise = p.execute(tablesQuery, [headTable]);
const [[head], [tables]] = await Promise.all([headPromise, tablesPromise]);
res.json({ head, tables });
} catch (error) {
next(error);
}
};
const setAppendStatus = async (req, res, next) => {
try {
const { csvId, appendStatus } = req.body;
const tagsQuery = `
UPDATE csvs
SET appendStatus = ?
WHERE csvId = ?;
`;
const p = res.locals.pool;
await p.execute(tagsQuery, [appendStatus, csvId]);
res.json({ result: "Set appendStatus OK", ...req.body });
} catch (error) {
next(error);
}
};
// Processing helper
const processingHelper = async (req, res, next) => {
try {
const { tagId } = req.body;
const tagsQuery = "SELECT tagId, tagName FROM tags ORDER BY tagId;";
const tablesQuery = `
WITH wanted_tables AS (
SELECT tableId
FROM tables_tags
WHERE tagId = ?
),
heads_tags AS (
SELECT t.headTable, tt.tagId
FROM tables t
LEFT JOIN tables_tags tt
ON t.tableId = tt.tableId
WHERE relevancy = 1
GROUP BY tt.tagId, t.headTable
),
heads_tags_json AS (
SELECT headTable, JSON_ARRAYAGG(tagId) AS all_tags
FROM heads_tags
GROUP BY headTable
)
SELECT t.tableId,
t.pdfName,
t.correct_csv,
c.csvText,
c.processed_text,
t.level,
if((json_arrayagg(tt.tagId) = cast('[
null
]' AS json)), CAST('[]' AS json), json_arrayagg(tt.tagId)
) AS tags,
htj.all_tags
FROM wanted_tables wt
LEFT JOIN tables t ON t.tableId = wt.tableId
LEFT JOIN csvs c ON t.correct_csv = c.csvId
LEFT JOIN tables_tags tt ON t.tableId = tt.tableId
LEFT JOIN heads_tags_json htj ON htj.headTable = t.headTable
GROUP BY t.tableId;
`;
const p = res.locals.pool;
const dataPromise = p.execute(tablesQuery, [tagId]);
const tagsPromise = p.execute(tagsQuery);
const [[data], [tags]] = await Promise.all([dataPromise, tagsPromise]);
res.json({ data, tags });
} catch (error) {
next(error);
}
};
// Manual processing helper
const manualHelper = async (req, res, next) => {
try {
const query = `
SELECT t.tableId, correct_csv, pdfName, page
FROM tables_tags tt
INNER JOIN tables t ON tt.tableId = t.tableId
INNER JOIN csvs c ON t.correct_csv = c.csvId
WHERE tagId = 13
AND accepted_text IS NULL;
`;
const p = res.locals.pool;
const [dbData] = await p.execute(query);
const csvs = await fg("**/*.csv", { cwd: manualCsvPath });
const existing = new Set(csvs.map((c) => c.split("/").slice(-1)[0].split(".")[0]));
const data = dbData.map((csv) => ({ ...csv, status: existing.has(csv.correct_csv) ? "done" : "" }));
res.json({ data });
} catch (error) {
next(error);
}
};
// Headers tagging
const headerTaggingIndex = async (req, res, next) => {
try {
const query = `
SELECT t.tableId, IF(headers_tagged = 1, 'done', 'pending') AS status, t.pdfName, page
FROM tables t
LEFT JOIN tables_tags tt ON t.tableId = tt.tableId AND tt.tagId = 4
LEFT JOIN pdfs p ON t.pdfName = p.pdfName
WHERE combinedConText IS NOT NULL
AND tt.tagId IS NULL
AND p.latest = 1
GROUP BY t.tableId, headers_tagged
ORDER BY headers_tagged, tableId;
`;
const [tables] = await res.locals.pool.execute(query);
res.json({ tables });
} catch (error) {
next(error);
}
};
const getHeaderTagging = async (req, res, next) => {
try {
const { tableId } = req.body;
const tableQuery = `
SELECT combinedConText, page, pdfName, tableTitle, headers_tagged
FROM tables
WHERE tableId = ?;
`;
const tableTagsQuery = `
SELECT header_idx AS headerIndex, htag
FROM headers_htags
WHERE tableId = ?;
`;
const allTagsQuery = `
SELECT htag, maxTags
FROM htags
ORDER BY htag;
`;
const p = res.locals.pool;
const tablePromise = p.execute(tableQuery, [tableId]);
const tableTagsPromise = p.execute(tableTagsQuery, [tableId]);
const allTagsPromise = p.execute(allTagsQuery);
const [[table], [tableTags], [allTags]] = await Promise.all([tablePromise, tableTagsPromise, allTagsPromise]);
res.json({ table, tableTags, allTags });
} catch (error) {
next(error);
}
};
const setHeaderTaggingStatus = async (req, res, next) => {
try {
const { tableId, newStatus } = req.body;
const query = `
UPDATE tables
SET headers_tagged = ?
WHERE tableId = ?;
`;
const [result] = await res.locals.pool.execute(query, [newStatus, tableId]);
res.json({ result });
} catch (error) {
next(error);
}
};
const setHeaderTag = async (req, res, next) => {
try {
const { tableId, headerIndex, hTag } = req.body;
const query = `
INSERT INTO headers_htags (tableId, header_idx, htag)
VALUES (?, ?, ?);
`;
const [result] = await res.locals.pool.execute(query, [tableId, headerIndex, hTag]);
res.json({ result });
} catch (error) {
next(error);
}
};
const removeHeaderTag = async (req, res, next) => {
try {
const { tableId, headerIndex, hTag } = req.body;
const query = `
DELETE
FROM headers_htags
WHERE tableId = ?
AND header_idx = ?
AND htag = ?;
`;
const [result] = await res.locals.pool.execute(query, [tableId, headerIndex, hTag]);
res.json({ result });
} catch (error) {
next(error);
}
};
// Server setup
function errorHandler(err, req, res, next) {
logger.error(err);
if (res.headersSent) {
next(err);
return;
}
res.statusMessage = err.message.replace(/[^\t\x20-\x7e\x80-\xff]/, "_");
res.status(500).send();
}
app.use(bodyParser.json());
app.post("/tableIndex", (req, res, next) => tableIndex(req, res, next));
app.post("/getExtractionData", (req, res, next) => getExtractionData(req, res, next));
app.post("/insertTable", (req, res, next) => insertTable(req, res, next));
app.post("/deleteTable", (req, res, next) => deleteTable(req, res, next));
app.post("/setPdfStatus", (req, res, next) => setPdfStatus(req, res, next));
app.post("/getValidationData", (req, res, next) => getValidationData(req, res, next));
app.post("/setValidation", (req, res, next) => setValidation(req, res, next));
app.post("/setRelevancy", (req, res, next) => setRelevancy(req, res, next));
app.post("/tagUntagTable", (req, res, next) => tagUntagTable(req, res, next));
app.post("/processingIndex", (req, res, next) => processingIndex(req, res, next));
app.post("/getSequence", (req, res, next) => getSequence(req, res, next));
app.post("/setAccepted", (req, res, next) => setAccepted(req, res, next));
app.post("/getConcatSequence", (req, res, next) => getConcatSequence(req, res, next));
app.post("/setAppendStatus", (req, res, next) => setAppendStatus(req, res, next));
app.post("/processingHelper", (req, res, next) => processingHelper(req, res, next));
app.post("/manualHelper", (req, res, next) => manualHelper(req, res, next));
app.post("/headerTaggingIndex", (req, res, next) => headerTaggingIndex(req, res, next));
app.post("/getHeaderTagging", (req, res, next) => getHeaderTagging(req, res, next));
app.post("/setHeaderTaggingStatus", (req, res, next) => setHeaderTaggingStatus(req, res, next));
app.post("/setHeaderTag", (req, res, next) => setHeaderTag(req, res, next));
app.post("/removeHeaderTag", (req, res, next) => removeHeaderTag(req, res, next));
app.use("/", express.static(path.join(__dirname, "client", "build")));
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.use((err, req, res, next) => errorHandler(err, req, res, next));
// Last resort error catching in libraries that I do not control
process.on("uncaughtException", (err, origin) => {
// eslint-disable-next-line no-console
console.log(`${Date.now()} - Uncaught Exception: ${err} at ${origin}`);
});
const port = process.env.PORT;
// eslint-disable-next-line no-console
app.listen(port, () => console.log(`Listening on port ${port}...`));