4 Commits

Author SHA1 Message Date
Kurt Hutten
41798682b0 Tweaks 2021-11-21 09:05:30 +11:00
Kurt Hutten
bca9c531a6 Add backend drawing 2021-11-21 07:23:37 +11:00
Kurt Hutten
9e0f1eee60 Get Cad Function working along side other app functions
with serverless
2021-11-20 20:51:36 +11:00
Kurt Hutten
53a6639fd1 Initial rw serverless setup 2021-11-20 17:36:06 +11:00
18 changed files with 5210 additions and 1571 deletions

4
app/.gitignore vendored
View File

@@ -1,3 +1,7 @@
dist
web/types/graphql.d.ts
api/types/graphql.d.ts
# Deployment
.serverless

2547
app/api/backend.tldr Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,7 @@ datasource db {
generator client {
provider = "prisma-client-js"
binaryTargets = "native"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
// sqlLight does not suport enums so we can't use enums until we set up postgresql in dev mode

View File

@@ -17,8 +17,11 @@
"serverless-binary-cors": "^0.0.1"
},
"devDependencies": {
"@netlify/zip-it-and-ship-it": "^4.30.0",
"@types/nodemailer": "^6.4.2",
"concurrently": "^6.0.0",
"nodemon": "^2.0.7"
"nodemon": "^2.0.7",
"serverless-dotenv-plugin": "^3.10.0",
"serverless-plugin-git-variables": "^5.1.0"
}
}
}

View File

@@ -9,7 +9,7 @@ RUN apt-get update -qq
RUN apt-get install -y wget
# 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 bash nodesource_setup.sh
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),
# 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.
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
RUN ["chmod", "+x", "/usr/local/bin/aws-lambda-rie"]
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/
RUN npm install
RUN npm install aws-lambda-ric@1.0.0
RUN conda --version
@@ -53,10 +54,13 @@ RUN apt-get install -y libglew2.1
RUN echo "cadhub-concat-split" > /var/task/cadhub-concat-split
# using built javascript from dist
# run `yarn rw build` before bulding this image
COPY dist/docker/cadquery/*.js /var/task/js/
COPY dist/docker/common/*.js /var/task/common/
COPY src/docker/common/entrypoint.sh /entrypoint.sh
# run `yarn rw build` and $(npm bin)/zip-it-and-ship-it api/dist/functions/ api/dist/zipball before bulding this image
COPY api/dist/zipball/cadquery.zip /var/task/
# -n stops aws-lamda-ric from being overridden.
RUN unzip -n /var/task/cadquery.zip
COPY api/src/docker/common/entrypoint.sh /entrypoint.sh
RUN ["chmod", "+x", "/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 { loggerWrap, storeAssetAndReturnUrl } from '../common/utils'
const stl = 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)
@@ -18,6 +18,4 @@ const stl = async (req, _context, callback) => {
})
}
module.exports = {
stl: middy(loggerWrap(stl)).use(cors()),
}
export const stl = middy(loggerWrap(_stl)).use(cors())

View File

@@ -2,14 +2,14 @@ services:
openscad-preview:
build:
context: ../../
dockerfile: ./src/docker/openscad/Dockerfile
context: ../../../
dockerfile: ./api/src/docker/openscad/Dockerfile
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
volumes:
- ../../dist/docker/openscad:/var/task/js/
- ../../dist/docker/common:/var/task/common/
- ../dist/docker/openscad:/var/task/js/
- ../dist/docker/common:/var/task/common/
ports:
- "5052:8080"
environment:
@@ -20,9 +20,9 @@ services:
openscad-stl:
image: openscad
volumes:
- ../../dist/docker/openscad:/var/task/js/
- ../../dist/docker/common:/var/task/common/
command: js/openscad.stl
- ../dist/docker/openscad:/var/task/js/
- ../dist/docker/common:/var/task/common/
command: openscad.stl
ports:
- "5053:8080"
environment:
@@ -32,12 +32,12 @@ services:
cadquery-stl:
build:
context: ../../
dockerfile: ./src/docker/cadquery/Dockerfile
context: ../../../
dockerfile: ./api/src/docker/cadquery/Dockerfile
volumes:
- ../../dist/docker/cadquery:/var/task/js/
- ../../dist/docker/common:/var/task/common/
command: js/cadquery.stl
- ../dist/docker/cadquery:/var/task/js/
- ../dist/docker/common:/var/task/common/
command: cadquery.stl
ports:
- 5060:8080
environment:

View File

@@ -14,7 +14,7 @@ RUN apt-get update -qq
RUN apt-get install -y openscad-nightly
# 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 bash nodesource_setup.sh
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),
# 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.
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
RUN ["chmod", "+x", "/usr/local/bin/aws-lambda-rie"]
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/
RUN npm install
RUN npm install aws-lambda-ric@1.0.0
# 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 && \
unzip /var/task/openscad/1.0.4
# 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
# using built javascript from dist
# run `yarn rw build` before bulding this image
COPY dist/docker/openscad/* /var/task/js/
COPY dist/docker/common/* /var/task/common/
COPY src/docker/common/entrypoint.sh /entrypoint.sh
# run `yarn rw build` and $(npm bin)/zip-it-and-ship-it api/dist/functions/ api/dist/zipball before bulding this image
COPY api/dist/zipball/openscad.zip /var/task/
# -n stops aws-lamda-ric from being overridden.
RUN unzip -n /var/task/openscad.zip
COPY api/src/docker/common/entrypoint.sh /entrypoint.sh
RUN ["chmod", "+x", "/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 { loggerWrap, storeAssetAndReturnUrl } from '../common/utils'
const preview = 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)
@@ -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
const eventBody = Buffer.from(req.body, 'base64').toString('ascii')
@@ -40,7 +40,5 @@ const stl = async (req, _context, callback) => {
})
}
module.exports = {
stl: middy(loggerWrap(stl)).use(cors()),
preview: middy(loggerWrap(preview)).use(cors()),
}
export const stl = middy(loggerWrap(_stl)).use(cors())
export const 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

@@ -16,6 +16,10 @@ export const handler = createGraphQLHandler({
sdls,
services,
plugins: [createSentryApolloPlugin()],
cors: {
origin: '*',
credentials: true,
},
onException: () => {
// Disconnect from your database with an unhandled exception.

View File

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

View File

@@ -7,8 +7,8 @@
]
},
"scripts": {
"cad": "yarn rw build api && 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": "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 && 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"
},
"devDependencies": {
@@ -36,4 +36,4 @@
"prisma": {
"seed": "yarn rw exec seed"
}
}
}

View File

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

188
app/serverless.yml Normal file
View File

@@ -0,0 +1,188 @@
# See the full yml reference at https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/
service: cadhubapi
# Uncomment org and app if you want to integrate your deployment with the Serverless dashboard. See https://www.serverless.com/framework/docs/dashboard/ for more details.
# org: your-org
# app: your-app
plugins:
- serverless-dotenv-plugin
- serverless-binary-cors
- serverless-plugin-git-variables
custom:
dotenv:
include:
- DATABASE_URL_PROD
- CLOUDINARY_API_KEY
- CLOUDINARY_API_SECRET
- EMAIL_PASSWORD
- SENTRY_DSN
# - # List the environment variables you want to include from your .env file here.
provider:
name: aws
lambdaHashingVersion: 20201221
runtime: nodejs14.x
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
cors: true
payload: '1.0'
stackTags: # Add CloudFormation stack tags here
source: serverless
name: Redwood Lambda API with HTTP API Gateway
tags: # Add service wide tags here
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:
individually: true
functions:
check-user-name:
description: check-user-name function deployed on AWS Lambda
package:
artifact: api/dist/zipball/check-user-name.zip # This is the default location of the zip file generated during the deploy command.
memorySize: 1024 # mb
timeout: 25 # seconds (max: 29)
tags: # Tags for this specific lambda function
endpoint: /.netlify/functions/check-user-name
# Uncomment this section to add environment variables either from the Serverless dotenv plugin or using Serverless params
environment:
SENTRY_DSN: ${env:SENTRY_DSN}
DATABASE_URL: ${env:DATABASE_URL_PROD}
COMMIT_REF: ${git:sha1}
CONTEXT: TODO
handler: check-user-name.handler
events:
- httpApi:
path: /.netlify/functions/check-user-name
method: GET
# cors: true
- httpApi:
path: /.netlify/functions/check-user-name
method: POST
# cors: true
graphql:
description: graphql function deployed on AWS Lambda
package:
artifact: api/dist/zipball/graphql.zip # This is the default location of the zip file generated during the deploy command.
memorySize: 1024 # mb
timeout: 25 # seconds (max: 29)
tags: # Tags for this specific lambda function
endpoint: /.netlify/functions/graphql
# Uncomment this section to add environment variables either from the Serverless dotenv plugin or using Serverless params
environment:
CLOUDINARY_API_KEY: ${env:CLOUDINARY_API_KEY}
CLOUDINARY_API_SECRET: ${env:CLOUDINARY_API_SECRET}
EMAIL_PASSWORD: ${env:EMAIL_PASSWORD}
SENTRY_DSN: ${env:SENTRY_DSN}
DATABASE_URL: ${env:DATABASE_URL_PROD}
COMMIT_REF: ${git:sha1}
CONTEXT: TODO
# YOUR_FIRST_ENV_VARIABLE: ${env:YOUR_FIRST_ENV_VARIABLE}
handler: graphql.handler
events:
- httpApi:
path: /.netlify/functions/graphql
method: GET
# cors: true
- httpApi:
path: /.netlify/functions/graphql
method: POST
# cors: true
# identity-signup: # this is netlify specific and is related to go true auth, so we'll continue having that deployed on netlify
# description: identity-signup function deployed on AWS Lambda
# package:
# artifact: api/dist/zipball/identity-signup.zip # This is the default location of the zip file generated during the deploy command.
# memorySize: 1024 # mb
# timeout: 25 # seconds (max: 29)
# tags: # Tags for this specific lambda function
# endpoint: /.netlify/functions/identity-signup
# # Uncomment this section to add environment variables either from the Serverless dotenv plugin or using Serverless params
# # environment:
# # YOUR_FIRST_ENV_VARIABLE: ${env:YOUR_FIRST_ENV_VARIABLE}
# handler: identity-signup.handler
# events:
# - httpApi:
# path: /.netlify/functions/identity-signup
# method: GET
# - httpApi:
# path: /.netlify/functions/identity-signup
# method: POST
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
# 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 =
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) =>
new Promise((resolve, reject) => {

File diff suppressed because it is too large Load Diff