OpenSCAD and CadQuery working together in dev
Still clean up of the backend/lamdas, and there's a conda permission issue when deploying the Cadquery image
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
const express = require('express')
|
||||
var cors = require('cors')
|
||||
const axios = require('axios')
|
||||
const stream = require('stream')
|
||||
const app = express()
|
||||
const port = 8080
|
||||
app.use(express.json())
|
||||
@@ -10,14 +9,14 @@ app.use(cors())
|
||||
const invocationURL = (port) =>
|
||||
`http://localhost:${port}/2015-03-31/functions/function/invocations`
|
||||
|
||||
app.post('/render', async (req, res) => {
|
||||
app.post('/openscad/preview', async (req, res) => {
|
||||
const { data } = await axios.post(invocationURL(5052), {
|
||||
body: Buffer.from(JSON.stringify(req.body)).toString('base64'),
|
||||
})
|
||||
res.status(data.statusCode)
|
||||
res.send(data.body)
|
||||
})
|
||||
app.post('/cadquery', async (req, res) => {
|
||||
app.post('/cadquery/stl', async (req, res) => {
|
||||
console.log('making post request to 5060')
|
||||
const { data } = await axios.post(invocationURL(5060), {
|
||||
body: Buffer.from(JSON.stringify(req.body)).toString('base64'),
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
const { runScad, stlExport } = require('./runScad')
|
||||
const { runCQ } = require('./runCQ')
|
||||
const middy = require('middy')
|
||||
const { cors } = require('middy/middlewares')
|
||||
|
||||
const health = async () => {
|
||||
console.log('Health endpoint')
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: 'ok',
|
||||
}
|
||||
}
|
||||
|
||||
// cors true does not seem to work in serverless.yml, perhaps docker lambdas aren't covered by that config
|
||||
// special lambda just for responding to options requests
|
||||
const preflightOptions = (req, _context, callback) => {
|
||||
@@ -24,12 +16,12 @@ const preflightOptions = (req, _context, callback) => {
|
||||
callback(null, response)
|
||||
}
|
||||
|
||||
const render = async (req, _context, callback) => {
|
||||
const stl = async (req, _context, callback) => {
|
||||
_context.callbackWaitsForEmptyEventLoop = false
|
||||
const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
|
||||
console.log(eventBody, 'eventBody')
|
||||
const { file, settings } = JSON.parse(eventBody)
|
||||
const { error, result, tempFile } = await runScad({ file, settings })
|
||||
const { error, result, tempFile } = await runCQ({ file, settings })
|
||||
if (error) {
|
||||
const response = {
|
||||
statusCode: 400,
|
||||
@@ -55,41 +47,7 @@ const render = async (req, _context, callback) => {
|
||||
}
|
||||
}
|
||||
|
||||
const exportstl = async (req, _context, callback) => {
|
||||
_context.callbackWaitsForEmptyEventLoop = false
|
||||
const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
|
||||
console.log(eventBody, 'eventBody')
|
||||
const { file } = JSON.parse(eventBody)
|
||||
const { error, result, tempFile } = await stlExport({ file })
|
||||
if (error) {
|
||||
const response = {
|
||||
statusCode: 400,
|
||||
body: { error, tempFile },
|
||||
}
|
||||
callback(null, response)
|
||||
} else {
|
||||
console.log(`got result in route: ${result}, file is: ${tempFile}`)
|
||||
const fs = require('fs')
|
||||
const stl = fs.readFileSync(`/tmp/${tempFile}/output.stl`, {
|
||||
encoding: 'base64',
|
||||
})
|
||||
console.log('encoded stl', stl)
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'content-type': 'application/stl',
|
||||
},
|
||||
body: stl,
|
||||
isBase64Encoded: true,
|
||||
}
|
||||
console.log('callback fired')
|
||||
callback(null, response)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
health: middy(health).use(cors()),
|
||||
exportstl: middy(exportstl).use(cors()),
|
||||
render: middy(render).use(cors()),
|
||||
stl: middy(stl).use(cors()),
|
||||
preflightOptions,
|
||||
}
|
||||
@@ -3,7 +3,7 @@ const { promises } = require('fs')
|
||||
const { writeFile } = promises
|
||||
const { nanoid } = require('nanoid')
|
||||
|
||||
module.exports.runScad = async ({
|
||||
module.exports.runCQ = async ({
|
||||
file,
|
||||
settings: {
|
||||
size: { x = 500, y = 500 } = {},
|
||||
@@ -29,20 +29,6 @@ module.exports.runScad = async ({
|
||||
}
|
||||
}
|
||||
|
||||
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}`)
|
||||
@@ -13,25 +13,25 @@ services:
|
||||
ports:
|
||||
- "5051:8080"
|
||||
|
||||
openscad-render:
|
||||
openscad-preview:
|
||||
image: openscad
|
||||
# build: ./openscad/.
|
||||
command: openscad.render
|
||||
command: openscad.preview
|
||||
# networks:
|
||||
# - awsland
|
||||
ports:
|
||||
- "5052:8080"
|
||||
|
||||
openscad-export:
|
||||
openscad-stl:
|
||||
image: openscad
|
||||
# build: ./openscad/.
|
||||
command: openscad.exportstl
|
||||
command: openscad.stl
|
||||
ports:
|
||||
- "5053:8080"
|
||||
|
||||
cadquery-render:
|
||||
cadquery-stl:
|
||||
build: ./cadquery/.
|
||||
command: openscad.render
|
||||
command: cadquery.stl
|
||||
ports:
|
||||
- 5060:8080
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ const preflightOptions = (req, _context, callback) => {
|
||||
callback(null, response)
|
||||
}
|
||||
|
||||
const render = async (req, _context, callback) => {
|
||||
const preview = async (req, _context, callback) => {
|
||||
_context.callbackWaitsForEmptyEventLoop = false
|
||||
const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
|
||||
console.log(eventBody, 'eventBody')
|
||||
@@ -55,7 +55,7 @@ const render = async (req, _context, callback) => {
|
||||
}
|
||||
}
|
||||
|
||||
const exportstl = async (req, _context, callback) => {
|
||||
const stl = async (req, _context, callback) => {
|
||||
_context.callbackWaitsForEmptyEventLoop = false
|
||||
const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
|
||||
console.log(eventBody, 'eventBody')
|
||||
@@ -89,7 +89,7 @@ const exportstl = async (req, _context, callback) => {
|
||||
|
||||
module.exports = {
|
||||
health: middy(health).use(cors()),
|
||||
exportstl: middy(exportstl).use(cors()),
|
||||
render: middy(render).use(cors()),
|
||||
stl: middy(stl).use(cors()),
|
||||
preview: middy(preview).use(cors()),
|
||||
preflightOptions,
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ provider:
|
||||
# this image is built locally and push to ECR
|
||||
openscadimage:
|
||||
path: ./openscad/
|
||||
cadqueryimage:
|
||||
path: ./cadquery/
|
||||
apiGateway:
|
||||
metrics: true
|
||||
binaryMediaTypes:
|
||||
@@ -52,7 +54,7 @@ provider:
|
||||
|
||||
functions:
|
||||
# see preflightoptions comment in openscad.js
|
||||
preflightrender:
|
||||
preflightopenscadpreview:
|
||||
image:
|
||||
name: openscadimage
|
||||
command:
|
||||
@@ -61,9 +63,9 @@ functions:
|
||||
- '/entrypoint.sh'
|
||||
events:
|
||||
- http:
|
||||
path: render
|
||||
path: openscad/preview
|
||||
method: options
|
||||
preflightexport:
|
||||
preflightopenscadstl:
|
||||
image:
|
||||
name: openscadimage
|
||||
command:
|
||||
@@ -72,30 +74,54 @@ functions:
|
||||
- '/entrypoint.sh'
|
||||
events:
|
||||
- http:
|
||||
path: export
|
||||
path: openscad/stl
|
||||
method: options
|
||||
render:
|
||||
openscadpreview:
|
||||
image:
|
||||
name: openscadimage
|
||||
command:
|
||||
- openscad.render
|
||||
- openscad.preview
|
||||
entryPoint:
|
||||
- '/entrypoint.sh'
|
||||
events:
|
||||
- http:
|
||||
path: render
|
||||
path: openscad/preview
|
||||
method: post
|
||||
timeout: 15
|
||||
exportStl:
|
||||
openscadstl:
|
||||
image:
|
||||
name: openscadimage
|
||||
command:
|
||||
- openscad.exportstl
|
||||
- openscad.stl
|
||||
entryPoint:
|
||||
- '/entrypoint.sh'
|
||||
events:
|
||||
- http:
|
||||
path: export
|
||||
path: openscad/stl
|
||||
method: post
|
||||
timeout: 30
|
||||
|
||||
preflightcadquerystl:
|
||||
image:
|
||||
name: cadqueryimage
|
||||
command:
|
||||
- cadquery.preflightOptions
|
||||
entryPoint:
|
||||
- '/entrypoint.sh'
|
||||
events:
|
||||
- http:
|
||||
path: cadquery/stl
|
||||
method: options
|
||||
cadquerystl:
|
||||
image:
|
||||
name: cadqueryimage
|
||||
command:
|
||||
- cadquery.stl
|
||||
entryPoint:
|
||||
- '/entrypoint.sh'
|
||||
events:
|
||||
- http:
|
||||
path: cadquery/stl
|
||||
method: post
|
||||
timeout: 30
|
||||
# The following are a few example events you can configure
|
||||
|
||||
Reference in New Issue
Block a user