-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (90 loc) · 3.35 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
const express = require('express')
const { exec } = require("child_process");
const fs = require('fs');
const crypto = require('crypto');
const app = express()
const port = 3000
const dirPrefix = "kmpgdT9Q1CSwy4R6"
const htmlTemplateFiles = ["/css/asyncapi.min.css","/css/global.min.css","/index.html","/js/asyncapi-ui.min.js"]
const getVersionHash = async function(url){
return new Promise((resolve, reject) => {
const lib = url.startsWith('https') ? require('https') : require('http');
const request = lib.get(url, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
const body = [];
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => resolve( crypto.createHash('md5').update(body.join('')).digest("hex")));
});
request.on('error', (err) => reject(err))
})
}
app.get('/asyncapi/file', (req, res) => {
let id = req.query.id
let versionHash = req.query.version
let file = req.query.file
if (!id || !versionHash || !file){
res.status(400);
res.json({ error: 'validation.params.required'})
return
}
let cachingKey = dirPrefix + id + versionHash
let outputDir = '/app/' + cachingKey;
let filePath = outputDir + file
console.log('Loading file from ' + filePath)
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.status(404);
res.json({ error: 'file.notFound'})
return;
}
res.status(200);
res.json({ id: id, version: versionHash, file: file, body: data})
});
});
app.post('/asyncapi', (req, res) => {
let src = req.query.src
if (!src){
res.status(400);
res.json({ error: 'validation.params.required'})
return
}
getVersionHash(src).then(versionHash => {
console.log('Current file version = ' + versionHash)
let id = crypto.createHash('md5').update(src).digest("hex")
let cachingKey = dirPrefix + id + versionHash
let outputDir = '/app/' + cachingKey;
console.log('Storing output under = ' + outputDir)
let command = "ag " + src + " @asyncapi/html-template -o " + cachingKey
if (fs.existsSync(outputDir)) {
res.status(200);
res.json({ id: id, version: versionHash, files: htmlTemplateFiles})
return
}
exec(command, (error, stdout, stderr) => {
if (error) {
res.status(500);
res.json({ error: 'execution.error', message: error })
return;
}
if (stderr) {
res.status(500);
res.json({ error: 'execution.error', message: stderr })
return;
}
res.status(200);
res.json({ id: id, version: versionHash, files: htmlTemplateFiles})
});
}).catch((err) => {
res.status(500);
res.json({ error: 'execution.error.version', message: err })
});
})
process.on('SIGINT', function() {
console.log("Shutting down. Ciao 👋")
process.exit();
});
app.listen(port, () => {
console.log("AsyncAPI Generator has started on port " + port + " 🚀")
})