-
Notifications
You must be signed in to change notification settings - Fork 0
/
wahoo-n.js
167 lines (152 loc) · 4.59 KB
/
wahoo-n.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
/**
* NodeJS web service for IRI RowGen.
*
* Main index file.
*
* @link https://github.com/donp-iri/wahoo-n
* @author Don Purnhagen
* @copyright Copyright (c) 2020 Innovative Routines International (IRI), Inc.
*/
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const tmp = require('tmp');
const fs = require('fs');
const process = require('child_process')
const port = 3000
// For parsing: application/x-www-form-urlencoded
//app.use(express.urlencoded({ extended: true }))
// For parsing: application/octet-stream
//app.use(bodyParser.raw())
// For parsing: application/json
app.use(express.json())
// For parsing: text/plain
app.use(bodyParser.text())
app.get('/', (req, res) => res.json({ ping:'pong' }))
app.get('/rowgen/:cmd', function (req, res) {
let cmd = req.params.cmd
console.log(cmd)
process.exec(cmd, { cwd:"rowgen" }, (err, stdout, stderr) => {
if (err) {
console.error(err)
res.json({ error:err })
} else {
let capture = stdout
res.send(capture)
}
})
})
app.post('/script', function (req, res) {
var tmpFile = tmp.fileSync({dir: './', postfix: '.scl'});
console.log('Created temporary file: ', tmpFile.name);
fs.writeSync(tmpFile.fd, (String) (req.body))
fs.closeSync(tmpFile.fd)
var cmd = 'sortcl /SPECIFICATION=' + tmpFile.name
console.log(cmd)
process.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.error(err)
res.json({ error:err })
} else {
let capture = stdout
res.send(capture)
}
fs.unlinkSync(tmpFile.name)
console.log('Removed temporary file: ', tmpFile.name);
})
})
app.post('/run', function (req, res) {
var cmd = 'sortcl ' + req.body
console.log(cmd)
process.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.error(err)
res.json({ error:err })
} else {
let capture = stdout
res.send(capture)
}
})
})
app.post('/cmd', function (req, res) {
var cmd = req.body.command
console.log(cmd)
var ret = req.body.return
console.log(ret)
process.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.error(err)
res.json({ error:err })
} else {
var capture
if (ret != undefined && ret != 'stdout') {
// Open and return the contents of ret.
try {
let fh = fs.openSync(ret)
} catch (err2) {
capture = 'Cannot open file: ' + ret
}
capture = "Open and return the contents."
} else {
capture = stdout
}
res.send(capture)
}
})
})
app.get('/rc', function (req, res) {
var cmd = 'sortcl /RC'
console.log(cmd)
process.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.error(err)
res.json({ error:err })
} else {
let arr = []
let lines = stderr.split("\n")
var i
for (i = 0; i < lines.length; ++i) {
if (i < 6) {
continue
}
let val = lines[i].match("\\s*(\\w+)\\s+(\\w+)")
if (val != null && val.length == 3) {
arr.push({
setting:val[1],
value:val[2]
})
}
}
res.json(arr)
}
})
})
app.post('/loopback', function (req, res) {
console.log(req.body)
res.send(req.body)
})
app.get('/version', function (req, res) {
var cmd = 'sortcl /V'
console.log(cmd)
process.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.error(err)
res.json({ error:err })
} else {
let lines = stdout.split("\n")
//console.log(lines)
let val1 = lines[0].match("(\\w+) ([\\d.]+) ([A-Z][\\d-]+) (\\w+) .*")
let val2 = lines[1].match("([\\d-]{10} [\\d:]{8}) #([\\w\\.]+) .*")
let val3 = lines[2].match(".*: ([\\d-]{10})")
res.json(
{ product:val1[1],
version:val1[2],
tag:val1[3],
arch:val1[4],
serial:val2[2],
expires:val3[1],
timestamp:val2[1] })
}
})
})
app.listen(port, () => console.log(`IRI RowGen web service at http://localhost:${port}`))