Create and deploy simple openscad api

A minimal frontend integration has been added to as a POC

resolves #219 and #222
This commit is contained in:
Kurt Hutten
2021-02-25 20:28:59 +11:00
parent 8493838f0c
commit 63c2a79a5d
15 changed files with 1848 additions and 44 deletions

View File

@@ -0,0 +1,68 @@
const { exec } = require('child_process')
const { promises } = require('fs')
const { writeFile } = promises
const { nanoid } = require('nanoid')
module.exports.runScad = async ({
file,
settings: { size: { x = 500, y = 500 } = {} } = {}, // TODO add view settings
} = {}) => {
const tempFile = await makeFile(file)
try {
const result = await runCommand(
`xvfb-run --auto-servernum --server-args "-screen 0 1024x768x24" openscad -o /tmp/${tempFile}/output.png --imgsize=${x},${y} /tmp/${tempFile}/main.scad`,
10000
)
return { result, tempFile }
} catch (error) {
return { error, tempFile }
}
}
module.exports.stlExport = async ({ file } = {}) => {
const tempFile = await makeFile(file)
try {
const result = await runCommand(
`openscad -o /tmp/${tempFile}/output.stl /tmp/${tempFile}/main.scad`,
300000 // lambda will time out before this, we might need to look at background jobs if we do git integration stl generation
)
return { result, tempFile }
} catch (error) {
return { error, tempFile }
}
}
async function makeFile(file) {
const tempFile = 'a' + nanoid() // 'a' ensure nothing funny happens if it start with a bad character like "-", maybe I should pick a safer id generator :shrug:
console.log(`file to write: ${file}`)
await runCommand(`mkdir /tmp/${tempFile}`)
await writeFile(`/tmp/${tempFile}/main.scad`, file)
return tempFile
}
async function runCommand(command, timeout = 5000) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
console.log(`stderr: ${stderr}`)
console.log(`stdout: ${stdout}`)
reject(stdout || stderr) // it seems random if the message is in stdout or stderr, but not normally both
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
resolve(`stderr: ${stderr}`)
return
}
console.log(`stdout: ${stdout}`)
resolve(`stdout: ${stdout}`)
})
setTimeout(() => {
reject('timeout')
}, timeout)
})
}