-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
144 lines (124 loc) · 3.92 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
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const temp = require('temp');
const YAML = require('json-to-pretty-yaml');
const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;
const YamlGenerator = require('./src/YamlGenerator.js');
const app = express();
app.server = http.createServer(app);
const PORT = 8080;
// Connector Definitions
const chassis = require('./definitions/chassis.json');
const ecus = require('./definitions/ecus.json');
const engines = require('./definitions/engines.json');
const inserts = require('./definitions/inserts.json');
const connectors = require('./definitions/connector-list.json');
const utils = require('./src/helpers/utils.js');
temp.track();
app.use(bodyParser.json())
app.set('view engine', 'ejs');
app.use('/public', express.static(__dirname + '/views/public'));
app.get('/definitions', (req, res) => {
return res.json({
chassis,
ecus,
engines,
inserts,
connectors
});
});
app.post('/fetch', (req, res) => {
const input = req.body;
let output;
try {
const yg = new YamlGenerator(input);
output = yg.generateOutput()
} catch(e) {
console.log(e)
return res.status(400).json({
data: null,
error: {
code: 400,
message: `${e}`
}
});
}
output.tweak = {
override: {
graph: {
ranksep: '6.5',
pad: '10.0'
}
}
}
// Return JSON payload with entire I/O List
let io_sheet = utils.getIOList(output.connections, input.ecu);
let output_yaml = YAML.stringify(output);
console.dir(input)
temp.cleanupSync();
temp.mkdir({
dir: path.join(__dirname, 'tmp')
}, (err, dirPath) => {
if (err) return res.status(400).json({
data: null,
error: {
code: 400,
message: 'Error occured spawning temp directory.'
}
});
const yamlFilePath = path.join(dirPath, 'output.yaml');
fs.writeFile(yamlFilePath, output_yaml, (err) => {
if (err) return res.status(400).json({
data: null,
error: {
code: 400,
message: 'Write Error has occured.'
}
});
process.chdir(dirPath);
console.log(`YAML File Generated: ${yamlFilePath}`)
exec(`wireviz ${yamlFilePath}`, (err, _stdout) => {
if (err) console.error(err)
if (err) return res.status(400).json({
data: null,
error: {
code: 400,
message: 'WireViz Engine Failure.'
}
});
const pngFilePath = yamlFilePath.split('.yaml')[0] + '.png';
const buffer = fs.readFileSync(pngFilePath);
const base64String = Buffer.from(buffer).toString('base64');
return res.status(200).json({
data: {
image: `data:image/png;base64,${base64String}`,
io_sheet
},
error: null
});
});
});
});
});
app.get('/', (req, res) => {
return res.render('viz', {
chassis,
ecus,
engines,
inserts,
connectors,
raw: {
chassis: JSON.stringify(chassis),
ecus: JSON.stringify(ecus),
engines: JSON.stringify(engines),
inserts: JSON.stringify(inserts),
connectors: JSON.stringify(connectors)
}
});
});
app.server.listen(process.env.PORT || PORT, () => {
console.log(`App is running on at http://127.0.0.1:${app.server.address().port}`);
});