-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
120 lines (100 loc) · 3.67 KB
/
app.js
File metadata and controls
120 lines (100 loc) · 3.67 KB
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
const express = require('express')
const fs = require('fs');
const app = express();
const path = require('path');
const http = require('http');
const https = require('https');
const port = 5000
app.use(express.json());
//app.use(express.static(path.join(__dirname, "public")));
app.get('/public/:file_name', function (req, res){
let file_path = path.join(__dirname, 'public', req.params.file_name)
res.sendFile(file_path,function(err){
if(err){
res.send('The this file ('+req.params.file_name +') does not exist');
}
});
})
app.get('/download', function (req, res) {
let file_path = path.join(__dirname, "DownloadTest.html")
res.download(file_path, "Test.html", headers={})
})
app.get('/Status', function (req, res) {
//read in html and substitute in the values extracted from the request above
fs.readFile('StatusTest.html', function(err, data) {
let host_path = req.header('Host')
host_path = "http://"+ host_path + "/download"
let html_read = data.toString()
html_read = html_read.replace(/URL_REPLACE/g, host_path);
//return html
res.send(html_read)
});
})
app.post('/Evaluate', function (req, res) {
var data = JSON.stringify(req.body)
var rdf_type = req.body.type.toString()
////////REPLACE THIS SECTION WITH OWN RUN CODE ////////////
acceptable = true
//////////////////END SECTION//////////////////////////////
if (acceptable) {
res.status(200).send(`The type sent (${rdf_type}) is an accepted type`);
} else {
res.status(415).send(`The type sent (${rdf_type}) is NOT an accepted type`);
};
})
app.post('/Run', async function (req, res) {
let hostHeader = req.get('host');
let hostAddr;
var complete_sbol = req.body.complete_sbol.toString();
var url = complete_sbol.replace("/sbol", "");
var token = (typeof req.body.token === 'undefined' || req.body.token === null) ? undefined : req.body.token.toString();
var tokenVal = (typeof token === 'undefined' || token === null) ? 'null' : token;
var headers = { 'Accept': 'text/plain' };
if (tokenVal !== 'null') {
headers['X-authorization'] = tokenVal;
}
var responseStatusCode = 'ERROR';
if (typeof fetch === 'function') {
var response = await fetch(url, { method: 'GET', headers: headers });
responseStatusCode = response.status;
} else {
// Fallback for older Node: use http/https without changing control flow elsewhere.
responseStatusCode = await new Promise(function(resolve) {
var client = url.startsWith('https://') ? https : http;
var req2 = client.get(url, { headers: headers }, function(r) {
r.on('data', function(){});
r.on('end', function(){ resolve(r.statusCode); });
});
req2.on('error', function(){ resolve('ERROR'); });
});
}
// determine SEQVIZ script URL (respect SERVER_HOST env override)
const overrideHost = process.env.SERVER_HOST;
if (overrideHost) {
const o = overrideHost.replace(/\/+$/,''); // strip trailing slashes
if (o.startsWith('http://') || o.startsWith('https://')) {
hostAddr = o;
} else {
hostAddr = 'http://' + o;
}
} else {
hostAddr = 'http://' + hostHeader;
}
// this works if you can access the plugin via an exposed port on the internet.
// Note that for synbiohub it must be https
// <img src="http://${hostAddr}/public/success.jpg" alt="Success">
const html_read = `<!doctype html>
<html>
<head><title>sequence view</title>
<script src="${hostAddr + '/seqviz.js'}"></script></head>
<body>
<p>Response from ${url}: ${responseStatusCode}</p>
<div id="reactele"></div>
<img src="${hostAddr + '/public/success.jpg'}" alt="Success">
<p>Host address: ${hostAddr}</p>
</body>
</html>
`;
res.send(html_read);
})
app.listen(port, () => console.log(`Test Serve app is listening at http://localhost:${port}`))