Get Cad Function working along side other app functions

with serverless
This commit is contained in:
Kurt Hutten
2021-11-20 20:51:36 +11:00
parent 53a6639fd1
commit 9e0f1eee60
12 changed files with 139 additions and 221 deletions

View File

@@ -9,7 +9,7 @@ RUN apt-get update -qq
RUN apt-get install -y wget RUN apt-get install -y wget
# install node14, see comment at the to of node14source_setup.sh # install node14, see comment at the to of node14source_setup.sh
ADD src/docker/common/node14source_setup.sh /nodesource_setup.sh ADD api/src/docker/common/node14source_setup.sh /nodesource_setup.sh
RUN ["chmod", "+x", "/nodesource_setup.sh"] RUN ["chmod", "+x", "/nodesource_setup.sh"]
RUN bash nodesource_setup.sh RUN bash nodesource_setup.sh
RUN apt-get install -y nodejs RUN apt-get install -y nodejs
@@ -29,13 +29,14 @@ RUN apt-get update && \
# Add the lambda emulator for local dev, (see entrypoint.sh for where it's used), # Add the lambda emulator for local dev, (see entrypoint.sh for where it's used),
# I have the file locally (gitignored) to speed up build times (as it downloads everytime), # I have the file locally (gitignored) to speed up build times (as it downloads everytime),
# but you can use the http version of the below ADD command or download it yourself from that url. # but you can use the http version of the below ADD command or download it yourself from that url.
ADD src/docker/common/aws-lambda-rie /usr/local/bin/aws-lambda-rie ADD api/src/docker/common/aws-lambda-rie /usr/local/bin/aws-lambda-rie
# ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/download/v1.0/aws-lambda-rie /usr/local/bin/aws-lambda-rie # ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/download/v1.0/aws-lambda-rie /usr/local/bin/aws-lambda-rie
RUN ["chmod", "+x", "/usr/local/bin/aws-lambda-rie"] RUN ["chmod", "+x", "/usr/local/bin/aws-lambda-rie"]
WORKDIR /var/task/ WORKDIR /var/task/
# aws-lambda-ric does not play nice with yarn, so installing it seperately,
# circle back to this later for a proper solution
COPY package*.json /var/task/ COPY package*.json /var/task/
RUN npm install
RUN npm install aws-lambda-ric@1.0.0 RUN npm install aws-lambda-ric@1.0.0
RUN conda --version RUN conda --version
@@ -53,10 +54,13 @@ RUN apt-get install -y libglew2.1
RUN echo "cadhub-concat-split" > /var/task/cadhub-concat-split RUN echo "cadhub-concat-split" > /var/task/cadhub-concat-split
# using built javascript from dist # using built javascript from dist
# run `yarn rw build` before bulding this image # run `yarn rw build` and $(npm bin)/zip-it-and-ship-it api/dist/functions/ api/dist/zipball before bulding this image
COPY dist/docker/cadquery/*.js /var/task/js/ COPY api/dist/zipball/cadquery.zip /var/task/
COPY dist/docker/common/*.js /var/task/common/ # -n stops aws-lamda-ric from being overridden.
COPY src/docker/common/entrypoint.sh /entrypoint.sh RUN unzip -n /var/task/cadquery.zip
COPY api/src/docker/common/entrypoint.sh /entrypoint.sh
RUN ["chmod", "+x", "/entrypoint.sh"] RUN ["chmod", "+x", "/entrypoint.sh"]
ENTRYPOINT ["sh", "/entrypoint.sh"] ENTRYPOINT ["sh", "/entrypoint.sh"]
CMD [ "js/cadquery.stl" ] CMD [ "cadquery.stl" ]

View File

@@ -3,7 +3,7 @@ import middy from 'middy'
import { cors } from 'middy/middlewares' import { cors } from 'middy/middlewares'
import { loggerWrap, storeAssetAndReturnUrl } from '../common/utils' import { loggerWrap, storeAssetAndReturnUrl } from '../common/utils'
const stl = async (req, _context, callback) => { const _stl = async (req, _context, callback) => {
_context.callbackWaitsForEmptyEventLoop = false _context.callbackWaitsForEmptyEventLoop = false
const eventBody = Buffer.from(req.body, 'base64').toString('ascii') const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
console.log('eventBody', eventBody) console.log('eventBody', eventBody)
@@ -18,6 +18,4 @@ const stl = async (req, _context, callback) => {
}) })
} }
module.exports = { export const stl = middy(loggerWrap(_stl)).use(cors())
stl: middy(loggerWrap(stl)).use(cors()),
}

View File

@@ -2,14 +2,14 @@ services:
openscad-preview: openscad-preview:
build: build:
context: ../../ context: ../../../
dockerfile: ./src/docker/openscad/Dockerfile dockerfile: ./api/src/docker/openscad/Dockerfile
image: openscad image: openscad
command: js/openscad.preview command: openscad.preview
# Adding volumes so that the containers can be restarted for js only changes in local dev # Adding volumes so that the containers can be restarted for js only changes in local dev
volumes: volumes:
- ../../dist/docker/openscad:/var/task/js/ - ../dist/docker/openscad:/var/task/js/
- ../../dist/docker/common:/var/task/common/ - ../dist/docker/common:/var/task/common/
ports: ports:
- "5052:8080" - "5052:8080"
environment: environment:
@@ -20,9 +20,9 @@ services:
openscad-stl: openscad-stl:
image: openscad image: openscad
volumes: volumes:
- ../../dist/docker/openscad:/var/task/js/ - ../dist/docker/openscad:/var/task/js/
- ../../dist/docker/common:/var/task/common/ - ../dist/docker/common:/var/task/common/
command: js/openscad.stl command: openscad.stl
ports: ports:
- "5053:8080" - "5053:8080"
environment: environment:
@@ -32,12 +32,12 @@ services:
cadquery-stl: cadquery-stl:
build: build:
context: ../../ context: ../../../
dockerfile: ./src/docker/cadquery/Dockerfile dockerfile: ./api/src/docker/cadquery/Dockerfile
volumes: volumes:
- ../../dist/docker/cadquery:/var/task/js/ - ../dist/docker/cadquery:/var/task/js/
- ../../dist/docker/common:/var/task/common/ - ../dist/docker/common:/var/task/common/
command: js/cadquery.stl command: cadquery.stl
ports: ports:
- 5060:8080 - 5060:8080
environment: environment:

View File

@@ -14,7 +14,7 @@ RUN apt-get update -qq
RUN apt-get install -y openscad-nightly RUN apt-get install -y openscad-nightly
# install node14, see comment at the to of node14source_setup.sh # install node14, see comment at the to of node14source_setup.sh
ADD src/docker/common/node14source_setup.sh /nodesource_setup.sh ADD api/src/docker/common/node14source_setup.sh /nodesource_setup.sh
RUN ["chmod", "+x", "/nodesource_setup.sh"] RUN ["chmod", "+x", "/nodesource_setup.sh"]
RUN bash nodesource_setup.sh RUN bash nodesource_setup.sh
RUN apt-get install -y nodejs RUN apt-get install -y nodejs
@@ -32,13 +32,14 @@ RUN apt-get update && \
# Add the lambda emulator for local dev, (see entrypoint.sh for where it's used), # Add the lambda emulator for local dev, (see entrypoint.sh for where it's used),
# I have the file locally (gitignored) to speed up build times (as it downloads everytime), # I have the file locally (gitignored) to speed up build times (as it downloads everytime),
# but you can use the http version of the below ADD command or download it yourself from that url. # but you can use the http version of the below ADD command or download it yourself from that url.
ADD src/docker/common/aws-lambda-rie /usr/local/bin/aws-lambda-rie ADD api/src/docker/common/aws-lambda-rie /usr/local/bin/aws-lambda-rie
# ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/download/v1.0/aws-lambda-rie /usr/local/bin/aws-lambda-rie # ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/download/v1.0/aws-lambda-rie /usr/local/bin/aws-lambda-rie
RUN ["chmod", "+x", "/usr/local/bin/aws-lambda-rie"] RUN ["chmod", "+x", "/usr/local/bin/aws-lambda-rie"]
WORKDIR /var/task/ WORKDIR /var/task/
# aws-lambda-ric does not play nice with yarn, so installing it seperately,
# circle back to this later for a proper solution
COPY package*.json /var/task/ COPY package*.json /var/task/
RUN npm install
RUN npm install aws-lambda-ric@1.0.0 RUN npm install aws-lambda-ric@1.0.0
# Install OpenSCAD libraries # Install OpenSCAD libraries
@@ -47,16 +48,19 @@ RUN echo "OPENSCADPATH=/var/task/openscad" >>/etc/profile && \
wget -P /var/task/openscad/ https://github.com/Irev-Dev/Round-Anything/archive/refs/tags/1.0.4.zip && \ wget -P /var/task/openscad/ https://github.com/Irev-Dev/Round-Anything/archive/refs/tags/1.0.4.zip && \
unzip /var/task/openscad/1.0.4 unzip /var/task/openscad/1.0.4
# Add our own theming (based on DeepOcean with a different "background" and "opencsg-face-back") # Add our own theming (based on DeepOcean with a different "background" and "opencsg-face-back")
COPY src/docker/openscad/cadhubtheme.json /usr/share/openscad-nightly/color-schemes/render/ COPY api/src/docker/openscad/cadhubtheme.json /usr/share/openscad-nightly/color-schemes/render/
RUN echo "cadhub-concat-split" > /var/task/cadhub-concat-split RUN echo "cadhub-concat-split" > /var/task/cadhub-concat-split
# using built javascript from dist # using built javascript from dist
# run `yarn rw build` before bulding this image # run `yarn rw build` and $(npm bin)/zip-it-and-ship-it api/dist/functions/ api/dist/zipball before bulding this image
COPY dist/docker/openscad/* /var/task/js/ COPY api/dist/zipball/openscad.zip /var/task/
COPY dist/docker/common/* /var/task/common/ # -n stops aws-lamda-ric from being overridden.
COPY src/docker/common/entrypoint.sh /entrypoint.sh RUN unzip -n /var/task/openscad.zip
COPY api/src/docker/common/entrypoint.sh /entrypoint.sh
RUN ["chmod", "+x", "/entrypoint.sh"] RUN ["chmod", "+x", "/entrypoint.sh"]
ENTRYPOINT ["sh", "/entrypoint.sh"] ENTRYPOINT ["sh", "/entrypoint.sh"]
CMD [ "js/openscad.render" ] CMD [ "openscad.preview" ]

View File

@@ -3,7 +3,7 @@ import middy from 'middy'
import { cors } from 'middy/middlewares' import { cors } from 'middy/middlewares'
import { loggerWrap, storeAssetAndReturnUrl } from '../common/utils' import { loggerWrap, storeAssetAndReturnUrl } from '../common/utils'
const preview = async (req, _context, callback) => { const _preview = async (req, _context, callback) => {
_context.callbackWaitsForEmptyEventLoop = false _context.callbackWaitsForEmptyEventLoop = false
const eventBody = Buffer.from(req.body, 'base64').toString('ascii') const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
console.log('eventBody', eventBody) console.log('eventBody', eventBody)
@@ -21,7 +21,7 @@ const preview = async (req, _context, callback) => {
}) })
} }
const stl = async (req, _context, callback) => { const _stl = async (req, _context, callback) => {
_context.callbackWaitsForEmptyEventLoop = false _context.callbackWaitsForEmptyEventLoop = false
const eventBody = Buffer.from(req.body, 'base64').toString('ascii') const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
@@ -40,7 +40,5 @@ const stl = async (req, _context, callback) => {
}) })
} }
module.exports = { export const stl = middy(loggerWrap(_stl)).use(cors())
stl: middy(loggerWrap(stl)).use(cors()), export const preview = middy(loggerWrap(_preview)).use(cors())
preview: middy(loggerWrap(preview)).use(cors()),
}

View File

@@ -1,172 +0,0 @@
service: cad-lambdas
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name
plugins:
- serverless-binary-cors
# - serverless-offline
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '2'
provider:
name: aws
lambdaHashingVersion: 20201221
ecr:
images:
# this image is built locally and push to ECR
openscadimage:
path: ../../
file: ./src/docker/openscad/Dockerfile
cadqueryimage:
path: ../../
file: ./src/docker/cadquery/Dockerfile
apiGateway:
metrics: true
binaryMediaTypes:
# we need to allow binary types to be able to send back images and stls, but it would be better to be more specific
# ie image/png etc. as */* treats everything as binary including the json body as the input the lambdas
# which mean we need to decode the input bode from base64, but the images break with anything other than */* :(
- '*/*'
# you can overwrite defaults here
# stage: dev
# region: us-east-1
# you can add statements to the Lambda function's IAM Role here
iam:
role:
statements:
- Effect: "Allow"
Action:
- "s3:GetObject"
Resource: "arn:aws:s3:::cad-preview-bucket-prod-001/*"
- Effect: "Allow"
Action:
- "s3:PutObject"
Resource: "arn:aws:s3:::cad-preview-bucket-prod-001/*"
# Dev bucket is cad-preview-bucket-dev-001/*"
# you can define service wide environment variables here
# environment:
# variable1: value1
functions:
openscadpreview:
image:
name: openscadimage
command:
- js/openscad.preview
entryPoint:
- '/entrypoint.sh'
events:
- http:
path: openscad/preview
method: post
cors: true
timeout: 25
environment:
BUCKET: cad-preview-bucket-prod-001
openscadstl:
image:
name: openscadimage
command:
- js/openscad.stl
entryPoint:
- '/entrypoint.sh'
events:
- http:
path: openscad/stl
method: post
cors: true
timeout: 30
environment:
BUCKET: cad-preview-bucket-prod-001
cadquerystl:
image:
name: cadqueryimage
command:
- js/cadquery.stl
entryPoint:
- '/entrypoint.sh'
events:
- http:
path: cadquery/stl
method: post
cors: true
timeout: 30
environment:
BUCKET: cad-preview-bucket-prod-001
# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
# events:
# - httpApi:
# path: /users/create
# method: get
# - websocket: $connect
# - s3: ${env:BUCKET}
# - schedule: rate(10 minutes)
# - sns: greeter-topic
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000
# - alexaSkill: amzn1.ask.skill.xx-xx-xx-xx
# - alexaSmartHome: amzn1.ask.skill.xx-xx-xx-xx
# - iot:
# sql: "SELECT * FROM 'some_topic'"
# - cloudwatchEvent:
# event:
# source:
# - "aws.ec2"
# detail-type:
# - "EC2 Instance State-change Notification"
# detail:
# state:
# - pending
# - cloudwatchLog: '/aws/lambda/hello'
# - cognitoUserPool:
# pool: MyUserPool
# trigger: PreSignUp
# - alb:
# listenerArn: arn:aws:elasticloadbalancing:us-east-1:XXXXXX:listener/app/my-load-balancer/50dc6c495c0c9188/
# priority: 1
# conditions:
# host: example.com
# path: /hello
# Define function environment variables here
# environment:
# variable2: value2
# you can add CloudFormation resource templates here
#resources:
# Resources:
# NewResource:
# Type: AWS::S3::Bucket
# Properties:
# BucketName: my-new-bucket
# Outputs:
# NewOutput:
# Description: "Description for the output"
# Value: "Some output value"
resources:
Resources:
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
RestApiId:
Ref: 'ApiGatewayRestApi'
GatewayResponseDefault5XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_5XX
RestApiId:
Ref: 'ApiGatewayRestApi'

View File

@@ -0,0 +1,3 @@
import { stl } from 'src/docker/cadquery/cadquery'
export { stl }

View File

@@ -0,0 +1,3 @@
import { stl, preview } from 'src/docker/openscad/openscad'
export { stl, preview }

View File

@@ -7,8 +7,8 @@
] ]
}, },
"scripts": { "scripts": {
"cad": "yarn rw build api && docker-compose --file ./api/src/docker/docker-compose.yml up --build", "cad": "yarn rw build api && zip-it-and-ship-it api/dist/functions/ api/dist/zipball && docker-compose --file ./api/src/docker/docker-compose.yml up --build",
"cad-r": "yarn rw build api && docker-compose --file ./api/src/docker/docker-compose.yml restart", "cad-r": "yarn rw build api && zip-it-and-ship-it api/dist/functions/ api/dist/zipball && docker-compose --file ./api/src/docker/docker-compose.yml restart",
"aws-emulate": "nodemon ./api/src/docker/aws-emulator.js" "aws-emulate": "nodemon ./api/src/docker/aws-emulator.js"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -7,17 +7,19 @@
[web] [web]
port = 8910 port = 8910
apiUrl = "/.netlify/functions" title = 'CadHub'
# apiUrl = "/.netlify/functions"
apiUrl = "https://uk5gegwopd.execute-api.us-east-2.amazonaws.com/.netlify/functions"
includeEnvironmentVariables = [ includeEnvironmentVariables = [
'GOOGLE_ANALYTICS_ID', 'GOOGLE_ANALYTICS_ID',
'CLOUDINARY_API_KEY', 'CLOUDINARY_API_KEY',
'CLOUDINARY_API_SECRET', # 'CLOUDINARY_API_SECRET',
'CAD_LAMBDA_BASE_URL', 'CAD_LAMBDA_BASE_URL',
'SENTRY_DSN', 'SENTRY_DSN',
'SENTRY_AUTH_TOKEN', 'SENTRY_AUTH_TOKEN',
'SENTRY_ORG', 'SENTRY_ORG',
'SENTRY_PROJECT', 'SENTRY_PROJECT',
'EMAIL_PASSWORD' # 'EMAIL_PASSWORD'
] ]
# experimentalFastRefresh = true # this seems to break cascadeStudio # experimentalFastRefresh = true # this seems to break cascadeStudio
[api] [api]

View File

@@ -7,6 +7,7 @@ service: cadhubapi
plugins: plugins:
- serverless-dotenv-plugin - serverless-dotenv-plugin
- serverless-binary-cors
custom: custom:
dotenv: dotenv:
@@ -20,7 +21,8 @@ custom:
provider: provider:
name: aws name: aws
runtime: nodejs12.x lambdaHashingVersion: 20201221
runtime: nodejs14.x
region: us-east-2 # This is the AWS region where the service will be deployed. region: us-east-2 # This is the AWS region where the service will be deployed.
httpApi: # HTTP API is used by default. To learn about the available options in API Gateway, see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html httpApi: # HTTP API is used by default. To learn about the available options in API Gateway, see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html
cors: true cors: true
@@ -30,6 +32,22 @@ provider:
name: Redwood Lambda API with HTTP API Gateway name: Redwood Lambda API with HTTP API Gateway
tags: # Add service wide tags here tags: # Add service wide tags here
name: Redwood Lambda API with HTTP API Gateway name: Redwood Lambda API with HTTP API Gateway
ecr:
images:
# this image is built locally and push to ECR
openscadimage:
path: ./
file: api/src/docker/openscad/Dockerfile
cadqueryimage:
path: ./
file: api/src/docker/cadquery/Dockerfile
apiGateway:
metrics: true
binaryMediaTypes:
# we need to allow binary types to be able to send back images and stls, but it would be better to be more specific
# ie image/png etc. as */* treats everything as binary including the json body as the input the lambdas
# which mean we need to decode the input bode from base64, but the images break with anything other than */* :(
- '*/*'
package: package:
individually: true individually: true
@@ -79,7 +97,46 @@ functions:
- httpApi: - httpApi:
path: /.netlify/functions/graphql path: /.netlify/functions/graphql
method: POST method: POST
# identity-signup.ts: openscadpreview:
image:
name: openscadimage
command:
- openscad.preview
entryPoint:
- '/entrypoint.sh'
events:
- http:
path: openscad/preview
method: post
cors: true
timeout: 25
openscadstl:
image:
name: openscadimage
command:
- openscad.stl
entryPoint:
- '/entrypoint.sh'
events:
- http:
path: openscad/stl
method: post
cors: true
timeout: 30
cadquerystl:
image:
name: cadqueryimage
command:
- cadquery.stl
entryPoint:
- '/entrypoint.sh'
events:
- http:
path: cadquery/stl
method: post
cors: true
timeout: 30
# identity-signup.ts: # this is netlify specific and is related to go true auth, so we'll continue having that deployed on netlify
# description: identity-signup.ts function deployed on AWS Lambda # description: identity-signup.ts function deployed on AWS Lambda
# package: # package:
# artifact: api/dist/zipball/identity-signup.ts.zip # This is the default location of the zip file generated during the deploy command. # artifact: api/dist/zipball/identity-signup.ts.zip # This is the default location of the zip file generated during the deploy command.
@@ -99,3 +156,24 @@ functions:
# path: /.netlify/functions/identity-signup.ts # path: /.netlify/functions/identity-signup.ts
# method: POST # method: POST
# this allows browsers to see error responses.
resources:
Resources:
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
RestApiId:
Ref: 'ApiGatewayRestApi'
GatewayResponseDefault5XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_5XX
RestApiId:
Ref: 'ApiGatewayRestApi'

View File

@@ -5,7 +5,7 @@ import type { Camera } from 'src/helpers/hooks/useIdeState'
export const lambdaBaseURL = export const lambdaBaseURL =
process.env.CAD_LAMBDA_BASE_URL || process.env.CAD_LAMBDA_BASE_URL ||
'https://oxt2p7ddgj.execute-api.us-east-1.amazonaws.com/prod' 'https://9inkvuvxz5.execute-api.us-east-2.amazonaws.com/dev'
export const stlToGeometry = (url) => export const stlToGeometry = (url) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {