1 | const express = require("express");
|
---|
2 | const tmpfs = require('temporary');
|
---|
3 |
|
---|
4 |
|
---|
5 | const { spawn } = require('child_process');
|
---|
6 |
|
---|
7 | module.exports = app => {
|
---|
8 |
|
---|
9 | function getDOI(script_name,doi,output_json_file,res) {
|
---|
10 |
|
---|
11 | var output_json_filename = output_json_file.path;
|
---|
12 |
|
---|
13 | var dataToSend;
|
---|
14 |
|
---|
15 | // Spawn new child process to call the python script
|
---|
16 | console.log("Running python3 " + script_name + " " + doi + " " + output_json_filename);
|
---|
17 | const python = spawn('python3', [script_name, doi, output_json_filename]);
|
---|
18 |
|
---|
19 | // Collect data from script
|
---|
20 | python.stdout.on('data', function (data) {
|
---|
21 | //console.log('Pipe data from python script ...');
|
---|
22 | dataToSend = data.toString();
|
---|
23 | });
|
---|
24 |
|
---|
25 | // In close event we are sure that stream from child process is closed
|
---|
26 | python.on('close', (code) => {
|
---|
27 | console.log(`child process closed, with exit value: ${code}`);
|
---|
28 |
|
---|
29 | //res.send(dataToSend)
|
---|
30 | //res.sendFile(output_json_filename, { root: __dirname }, function(err) {
|
---|
31 | res.sendFile(output_json_filename, function(err) {
|
---|
32 | if (err) {
|
---|
33 | console.error("Error: Failed to send file '" + output_json_filename + "'");
|
---|
34 | console.error(err);
|
---|
35 | res.status(404);
|
---|
36 | //res.status(err.status);
|
---|
37 | res.end();
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | console.log("Away to remove: '" + output_json_filename + "'");
|
---|
41 | try {
|
---|
42 | output_json_file.unlink();
|
---|
43 | }
|
---|
44 | catch(e) {
|
---|
45 | console.error("Failed to delete file");
|
---|
46 | console.error(e);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | });
|
---|
50 | });
|
---|
51 | }
|
---|
52 |
|
---|
53 | function OACoreGetDOI(doi,res)
|
---|
54 | {
|
---|
55 | var unique_tmp_file = new tmpfs.File("oacore-output");
|
---|
56 | console.log("unique tmp file = " + unique_tmp_file.path);
|
---|
57 |
|
---|
58 | getDOI("doi-stinky/oacore-get-doi.py", doi, unique_tmp_file, res);
|
---|
59 |
|
---|
60 | //getDOI("doi-stinky/oacore-get-doi.py", doi, "oacore-output.json", res);
|
---|
61 | }
|
---|
62 |
|
---|
63 | function CrossRefGetDOI(doi,res)
|
---|
64 | {
|
---|
65 | var unique_tmp_file = new tmpfs.File("crossref-output");
|
---|
66 | getDOI("doi-stinky/crossref-get-doi.py", doi, unique_tmp_file, res);
|
---|
67 |
|
---|
68 | //getDOI("doi-stinky/crossref-get-doi.py", doi, "crossref-output.json", res);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 | let router = express.Router();
|
---|
74 |
|
---|
75 | // General logging
|
---|
76 | router.use(function(req,res,next) {
|
---|
77 | console.log("/" + req.method);
|
---|
78 | next();
|
---|
79 | });
|
---|
80 |
|
---|
81 | // API Routing Rules
|
---|
82 |
|
---|
83 | router.get("/",function(req,res) {
|
---|
84 | res.json({"message" : "A Gateway API for accessing a range of DOI-based APIs provided by others"});
|
---|
85 | });
|
---|
86 |
|
---|
87 |
|
---|
88 | router.get("/get-doi", (req, res) => {
|
---|
89 | var oacoreDOI = req.query.coreDOI;
|
---|
90 | var crossrefDOI = req.query.crossrefDOI;
|
---|
91 |
|
---|
92 | if (oacoreDOI) {
|
---|
93 | OACoreGetDOI(oacoreDOI,res);
|
---|
94 | }
|
---|
95 | else if (crossrefDOI) {
|
---|
96 | CrossRefGetDOI(crossrefDOI,res);
|
---|
97 | }
|
---|
98 |
|
---|
99 | });
|
---|
100 |
|
---|
101 | app.use("/api",router);
|
---|
102 | }
|
---|
103 |
|
---|