Skip to content

Commit

Permalink
feat(subscribers): add trigger for pdf on create
Browse files Browse the repository at this point in the history
  • Loading branch information
Dale Nguyen committed May 21, 2024
1 parent 66fb282 commit a7727d5
Show file tree
Hide file tree
Showing 18 changed files with 1,489 additions and 199 deletions.
44 changes: 44 additions & 0 deletions build-new-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# enable to printout full logs
set -x

# Fail on any error.
set -eo pipefail

# Case statement to pull in inputs
while true; do
case "$1" in
-d|--dir)
dir="$2"
shift 2;;
-s|--image)
image="$2"
shift 2;;
-b|--buildId)
buildId="$2"
shift
break;;
*)
break;;
esac
done
# 20220703r0 or 20220703.local-Dale
buildNumber=${BUILD_NUMBER:-$(date +'%Y%m%d')'.local-'$(git config --global user.name | tr -d ' ' | tr '[:upper:]' '[:lower:]' )}
buildId=${buildId:-$buildNumber}

GCLOUD_PROJECT="pdfun-prod"
REGION="us-central1"
REPO="pdf"

imageTag=${REGION}-docker.pkg.dev/$GCLOUD_PROJECT/$REPO/$image


# set the dir where the command will run
dir=$dir
echo "dir: $dir image: $image project: $GCLOUD_PROJECT buildId: $buildId"

# Build the image locally
(cd $dir && docker build -t $imageTag -f Dockerfile --platform linux/x86_64 .)

# Push it to Artifact Registry:
docker push $imageTag

34 changes: 34 additions & 0 deletions doc/decisions/0003-trigger-firestore-event-to-cloud-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 3. Trigger Firestore event to Cloud Run

Date: 2024-05-20

## Status

Accepted

## Context

We we want to listen to an event where a document is created, so we can start to load the file and resize PDF file.

## Decision

- Utilize [Eventarc](https://cloud.google.com/eventarc/docs/run/route-trigger-cloud-firestore#gcloud)
- Create `firestore-trigger` service account to trigger cloud run. The path will be in the `ce-document` under the headers.

Example of event trigger

```
gcloud eventarc triggers create public-on-create-trigger \
--location=nam5 \
--destination-run-service=pdf-on-create \
--destination-run-region=us-central1 \
--event-filters="type=google.cloud.firestore.document.v1.created" \
--event-filters="database=(default)" \
--event-filters-path-pattern="document=public/{docId}" \
--event-data-content-type="application/protobuf" \
--service-account=firestore-trigger@pdfun-prod.iam.gserviceaccount.com
```

## Consequences

What becomes easier or more difficult to do and any risks introduced by the change that will need to be mitigated.
16 changes: 15 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"{workspaceRoot}/.eslintignore",
"{workspaceRoot}/eslint.config.js"
]
},
"@nx/esbuild:esbuild": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
}
},
"generators": {
Expand All @@ -47,5 +52,14 @@
"@nx/angular:component": {
"style": "scss"
}
}
},
"plugins": [
{
"plugin": "@nx/eslint/plugin",
"options": {
"targetName": "lint",
"extensions": ["ts", "tsx", "js", "jsx", "html", "vue"]
}
}
]
}
22 changes: 18 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@angular/platform-browser-dynamic": "^17.2.0",
"@angular/platform-server": "^17.2.0",
"@angular/router": "^17.2.0",
"firebase-admin": "^12.1.0",
"firebase-functions": "^5.0.1",
"front-matter": "^4.0.2",
"marked": "^5.0.2",
"marked-gfm-heading-id": "^3.1.0",
Expand All @@ -41,26 +43,32 @@
"@angular/compiler-cli": "^17.2.0",
"@nx/angular": "^18.0.0",
"@nx/devkit": "^18.0.0",
"@nx/esbuild": "18.0.0",
"@nx/eslint": "18.3.5",
"@nx/eslint-plugin": "18.3.5",
"@nx/jest": "18.3.5",
"@nx/js": "18.3.5",
"@nx/eslint-plugin": "^18.0.0",
"@nx/eslint-plugin-nx": "^16.0.0-beta.4",
"@nx/jest": "18.0.0",
"@nx/js": "18.0.0",
"@nx/node": "^18.0.0",
"@nx/vite": "^18.0.0",
"@nx/workspace": "19.0.4",
"@nx/workspace": "18.0.0",
"@schematics/angular": "^17.2.0",
"@swc-node/register": "~1.8.0",
"@swc/core": "~1.3.85",
"@swc/helpers": "~0.5.2",
"@swc/jest": "0.2.20",
"@types/jest": "^29.4.0",
"@types/node": "18.16.9",
"@types/shelljs": "^0.8.15",
"@typescript-eslint/eslint-plugin": "^7.3.0",
"@typescript-eslint/parser": "^7.3.0",
"autoprefixer": "^10.4.0",
"esbuild": "^0.19.2",
"eslint": "~8.57.0",
"eslint-config-prettier": "^9.0.0",
"jest": "^29.4.1",
"jest-environment-jsdom": "^29.4.1",
"jest-environment-node": "^29.4.1",
"jest-preset-angular": "~14.0.3",
"jsdom": "^22.1.0",
"nx": "18.0.0",
Expand All @@ -73,5 +81,11 @@
"vite": "^5.0.0",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^1.3.1"
},
"optionalDependencies": {
"@nx/nx-darwin-arm64": "18.0.0",
"@nx/nx-darwin-x64": "18.0.0",
"@nx/nx-linux-x64-gnu": "18.0.0",
"@nx/nx-win32-x64-msvc": "18.0.0"
}
}
2 changes: 1 addition & 1 deletion pdf-deploy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# from the dropdown at the top of Cloud Console:
export GCLOUD_PROJECT="dalenguyen-prod"
export GCLOUD_PROJECT="pdfun-prod"
# from Step 2.2 above:
export REPO="pdf"
# the region you chose in Step 2.4:
Expand Down
2 changes: 1 addition & 1 deletion pdf/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"outputs": ["{projectRoot}/coverage"]
},
"deploy": {
"command": "gcloud run deploy pdfun --image us-central1-docker.pkg.dev/dalenguyen-prod/pdf/pdfun:latest --region us-central1 --project dalenguyen-prod"
"command": "gcloud run deploy pdfun --image us-central1-docker.pkg.dev/pdfun-prod/pdf/pdfun:latest --region us-central1 --project pdfun-prod"
}
},
"tags": [],
Expand Down
18 changes: 18 additions & 0 deletions subscribers/pdf-on-create/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
14 changes: 14 additions & 0 deletions subscribers/pdf-on-create/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:20-slim

RUN set -ex; \
apt-get -y update; \
apt-get -y install ghostscript; \
rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

COPY . ./

RUN yarn --pure-lockfile --non-interactive

CMD ["node", "main.js"]
11 changes: 11 additions & 0 deletions subscribers/pdf-on-create/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'pdf-on-create',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': '@swc/jest',
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/subscribers/pdf-on-create',
}
97 changes: 97 additions & 0 deletions subscribers/pdf-on-create/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "pdf-on-create",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "subscribers/pdf-on-create/src",
"projectType": "application",
"tags": ["type:app"],
"targets": {
"build": {
"executor": "@nx/esbuild:esbuild",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"platform": "node",
"outputPath": "dist/subscribers/pdf-on-create",
"outputFileName": "main.js",
"format": ["cjs"],
"bundle": false,
"compiler": "tsc",
"main": "subscribers/pdf-on-create/src/main.ts",
"tsConfig": "subscribers/pdf-on-create/tsconfig.app.json",
"assets": ["subscribers/pdf-on-create/src/assets"],
"generatePackageJson": true,
"esbuildOptions": {
"sourcemap": true,
"outExtension": {
".js": ".js"
}
}
},
"configurations": {
"development": {},
"production": {
"generateLockfile": true,
"esbuildOptions": {
"sourcemap": false,
"outExtension": {
".js": ".js"
}
}
}
}
},
"serve": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"options": {
"buildTarget": "pdf-on-create:build"
},
"configurations": {
"development": {
"buildTarget": "pdf-on-create:build:development"
},
"production": {
"buildTarget": "pdf-on-create:build:production"
}
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "subscribers/pdf-on-create/jest.config.ts"
}
},
"deploy": {
"executor": "nx:run-commands",
"options": {
"commands": [
"nx deploy-docker pdf-on-create",
"nx deploy-cloudrun pdf-on-create"
],
"color": true,
"parallel": false
},
"dependsOn": [
{
"target": "build"
}
]
},
"deploy-docker": {
"command": "./build-new-image.sh --dir dist/subscribers/pdf-on-create --image pdf-on-create",
"parallel": false,
"dependsOn": [
{
"target": "copy"
}
]
},
"deploy-cloudrun": {
"command": "gcloud run deploy pdf-on-create --image=us-central1-docker.pkg.dev/pdfun-prod/pdf/pdf-on-create --platform=managed --project=pdfun-prod --region=us-central1 --no-allow-unauthenticated"
},
"copy": {
"command": "cp subscribers/pdf-on-create/Dockerfile dist/subscribers/pdf-on-create"
}
}
}
1 change: 1 addition & 0 deletions subscribers/pdf-on-create/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './pdf.handler'
16 changes: 16 additions & 0 deletions subscribers/pdf-on-create/src/app/pdf.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Request, Response } from 'express'

export const handler = async (req: Request, res: Response) => {
const path = req.headers['ce-document']

console.log({ path })

if (!path) {
const msg = 'No valid path received'
console.error(`error: ${msg}`)
res.status(400).send(`Bad Request: ${msg}`)
return
}

return res.json({ success: 'true' })
}
Empty file.
14 changes: 14 additions & 0 deletions subscribers/pdf-on-create/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import cors from 'cors'
import express, { NextFunction, Request, Response } from 'express'
import { handler } from './app/pdf.handler'

export const server = express()
.use(cors())
.use(express.json())
.use(handler)
.use((err: Error, req: Request, res: Response, _next: NextFunction) => {
console.error('unhandled error', err)
})
.listen(8080, () =>
console.log(`🚀 [APP] is running on: http://localhost:8080`)
)
10 changes: 10 additions & 0 deletions subscribers/pdf-on-create/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
16 changes: 16 additions & 0 deletions subscribers/pdf-on-create/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"compilerOptions": {
"esModuleInterop": true
}
}
Loading

0 comments on commit a7727d5

Please sign in to comment.