-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(subscribers): add trigger for pdf on create
- Loading branch information
Dale Nguyen
committed
May 21, 2024
1 parent
66fb282
commit a7727d5
Showing
18 changed files
with
1,489 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
doc/decisions/0003-trigger-firestore-event-to-cloud-run.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './pdf.handler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.