-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
102 lines (90 loc) · 2.86 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
const express = require('express')
const path = require('path');
const request = require('request');
const serialize = require("serialize-javascript");
import filesToParts from "./io/filesToParts";
const app = express()
const port = 5000
const addr = "localhost"
app.use(express.json());
//app.use(express.static("public"));
app.get('/seqviz.js', function (req, res) {
console.log('seqviz.js')
res.sendFile(path.join(__dirname, 'public', 'seqviz.js'));
})
app.get('/Status', function (req, res) {
console.log('Status')
res.status(200).send('The plugin is up and running')
})
app.post('/Evaluate', function (req, res) {
let type = req.body.type.toString();
console.log('evaluate ' + type)
if (type === 'Component' || type === 'ComponentDefinition') {
res.status(200).send('The app can handle this input');
} else {
res.status(404).end();
}
})
app.post('/Run', async (req, res) => {
let url = req.body.complete_sbol.toString();
let top_level = req.body.top_level.toString();
let hostAddr = req.get('host');
console.log('run url=' + url + ' top=' + top_level + ' hostAddr=' + hostAddr)
try {
// Get SBOL file content string
const csv = await getFileData(url);
// parse SBOL file to get data for sequence view rendering
const {
displayList,
parts
} = await filesToParts(csv, {
topLevel: top_level
});
const propdata = {
style: {
height: 600,
width: 1100
},
size: {
width: 500,
height: 600
},
displayList: displayList,
parts: parts,
}
const theHtml = `<!doctype html>
<html>
<head><title>sequence view</title></head>
<body>
<div id="reactele"></div>
<script type="text/javascript">window.__INITIAL_DATA__ = ${serialize(propdata)}</script>
<script type="text/javascript" src="https://${hostAddr}/seqviz.js" charset="utf-8"></script>
</body>
</html>`;
res.send(theHtml);
} catch (err) {
const theHtml = `<!doctype html>
<html>
<head><title>sequence view</title></head>
<body>
<div id="reactele">
Error when parsing this file to get sequence data!
</div>
</body>
</html>`;
res.send(theHtml);
}
})
function getFileData(url) {
return new Promise((resolve, reject) => {
request.get(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
return resolve(body);
} else {
console.log('error:', error);
return reject(error);
}
});
})
}
app.listen(port, () => console.log(`Example app listening at http://${addr}:${port}`))