-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (86 loc) · 3.04 KB
/
app.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
const express = require('express')
const fetch = require('node-fetch')
const cors = require('cors')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const Logger = require('./logger')
const logging = new Logger('chiron-client')
const { ContentEngine } = require('./contentEngine')
let contentEngine = new ContentEngine()
const app = express()
app.use(cors())
app.use(express.static('./static/'))
app.use(express.json())
app.get('/health', (req, res) => {
res.send('OK')
})
app.post('/content', async (req, res) => {
logging.info('Got POST for content, trying to load it')
const contentUrl = req.body.contentUrl
if (!contentUrl) {
logging.error('No content URL has been provided')
res.sendStatus(400)
return
}
try {
const ilContent = await fetch(`http://${contentUrl}`)
if (ilContent.status === 404) {
logging.error('Could not find content')
res.sendStatus(404)
return
}
const ilResponse = await ilContent.json()
logging.info('Found content, processing through IL')
contentEngine = new ContentEngine()
await contentEngine.init(ilResponse)
res.sendStatus(200)
} catch (e) {
logging.error(`ERROR: ${e}`)
res.status(500).json(e)
}
})
app.get('/htmlcontent', (req, res) => {
const content = contentEngine?.getHtmlContent()
content ? res.send(content) : res.sendStatus(404)
})
app.post('/command', async (req, res) => {
logging.info(`Running command ${req.body.command}`)
try {
// We might ve waiting for a specific command to be run before progressing the content
// so we need to check the current chunm conditions
const newContent = await contentEngine.checkChunkConditions(req.body.command)
/* istanbul ignore next */
logging.debug(newContent ? 'Call me back for new content' : 'There is no new content')
// Due to the way we have to import ChildProcess and then Promisify it
// (because it's still working with CallBacks *sigh*) we check if we're
// being run as part of the test harness and then skip if we are, this is
// a hack but it's 2022 and child_process is still using CallBacks.
/* istanbul ignore next */
if (process.env.npm_command !== 'test') {
const { stdout, stderr } = await exec(req.body.command)
logging.info(stdout)
logging.info(stderr)
res.json({
newContent,
commandOutput: stdout || stderr
})
} else {
logging.error('RUNNING IN TEST MODE, THIS SHOULD BE IN PROD')
logging.error('RUNNING IN TEST MODE, THIS SHOULD BE IN PROD')
logging.error('RUNNING IN TEST MODE, THIS SHOULD BE IN PROD')
logging.error('RUNNING IN TEST MODE, THIS SHOULD BE IN PROD')
res.sendStatus(204)
}
} catch (error) {
logging.error(error)
res.sendStatus(500)
}
})
app.get('/history', async (req, res) => {
logging.info('Getting the current command history')
const chunks = contentEngine.completedChunks
chunks !== undefined
? res.json(contentEngine.completedChunks)
: res.sendStatus(204)
})
module.exports = app