Skip to content

Commit 01d9d92

Browse files
feat: DVC-9055 adding Cloudflare Worker example app for new js-cloud-server-sdk (#561)
1 parent 7d1ccfe commit 01d9d92

File tree

12 files changed

+523
-18
lines changed

12 files changed

+523
-18
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Run CF Worker Example App
2+
on:
3+
pull_request:
4+
branches: [main]
5+
6+
jobs:
7+
build:
8+
runs-on:
9+
labels: ubuntu-latest-4-core
10+
strategy:
11+
matrix:
12+
node-version: [18.x]
13+
steps:
14+
- uses: actions/checkout@v3
15+
with:
16+
fetch-depth: 0
17+
- name: Use Node.js ${{ matrix.node-version }}
18+
uses: actions/setup-node@v3
19+
with:
20+
node-version: ${{ matrix.node-version }}
21+
cache: 'yarn'
22+
- name: Run Yarn
23+
run: yarn --immutable
24+
- name: Setup .dev.vars file
25+
run: echo "DEVCYCLE_SERVER_SDK_KEY=${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}" > examples/js-cloud-server/cloudflare-worker/.dev.vars
26+
- name: Run example app in background
27+
run: |
28+
yarn nx serve example-js-cloud-server-sdk-cf-worker &
29+
echo "SERVER_PID=$!" >> $GITHUB_ENV
30+
continue-on-error: true
31+
- name: Wait for the server to be up
32+
run: sleep 10
33+
- name: Test server with curl
34+
run: |
35+
RESPONSE=$(curl -s http://localhost:8787) # Replace with your server's port
36+
# Check the response or do something based on the result.
37+
if [[ "$RESPONSE" != *"DevCycle Variables:"* ]]; then
38+
echo "Server didn't return the expected 'DevCycle Variables:' response"
39+
exit 1
40+
fi
41+
- name: Cleanup server
42+
run: kill ${{ env.SERVER_PID }}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": ["../../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*", "node_modules/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
}
17+
]
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.env
2+
.env.*
3+
dist
4+
node_modules
5+
.dev.vars
6+
.wrangler
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# DevCycle JS Cloud Bucketing Server SDK - Cloudflare Worker Example App
2+
3+
Example app for using the DevCycle JS Cloud Bucketing Server SDK with Cloudflare Workers.
4+
5+
## Serve Worker Locally
6+
7+
1. Run `yarn` in the root of this repo to install all dependencies.
8+
2. Set up a `.dev.vars` file in this folder containing your DevCycle Server SDK Key for your project:
9+
- ```DEVCYCLE_SERVER_SDK_KEY=<YOUR SERVER SDK KEY>```
10+
3. Run this worker locally using: `yarn nx serve example-js-cloud-server-sdk-cf-worker`
11+
12+
## Deploy Worker to Cloudflare
13+
14+
1. Run `yarn` in the root of this repo to install all dependencies.
15+
2. Set a `DEVCYCLE_SERVER_SDK_KEY` secret in Cloudflare using the wrangler CLI from the root:
16+
- ```yarn wrangler secret put DEVCYCLE_SERVER_SDK_KEY -c examples/js-cloud-server/cloudflare-worker/wrangler.toml```
17+
- you may be prompted to login to Wrangler with your Cloudflare account
18+
3. Set your Cloudflare account ID in `wrangler.toml`:
19+
- you can find this under your account's profile on the Cloudflare dashboard
20+
4. Deploy this worker to Cloudflare using: `yarn nx deploy example-js-cloud-server-sdk-cf-worker`
21+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "js-cloud-server-sdk-test-worker",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "wrangler deploy",
7+
"start": "wrangler dev"
8+
},
9+
"dependencies": {
10+
"@devcycle/js-cloud-server-sdk": "1.0.0"
11+
}
12+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "example-js-cloud-server-sdk-cf-worker",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "examples/js-cloud-server/cloudflare-worker/src",
5+
"projectType": "application",
6+
"targets": {
7+
"build": {
8+
"executor": "@nx/webpack:webpack",
9+
"outputs": ["{options.outputPath}"],
10+
"options": {
11+
"outputPath": "dist/examples/js-cloud-server/cloudflare-worker",
12+
"main": "examples/js-cloud-server/cloudflare-worker/src/index.ts",
13+
"tsConfig": "examples/js-cloud-server/cloudflare-worker/tsconfig.json",
14+
"compiler": "tsc",
15+
"target": "node"
16+
}
17+
},
18+
"serve": {
19+
"executor": "nx:run-commands",
20+
"options": {
21+
"command": "yarn run -T wrangler dev",
22+
"cwd": "examples/js-cloud-server/cloudflare-worker"
23+
}
24+
},
25+
"deploy": {
26+
"executor": "nx:run-commands",
27+
"options": {
28+
"command": "yarn run -T wrangler deploy",
29+
"cwd": "examples/js-cloud-server/cloudflare-worker"
30+
},
31+
"dependsOn": ["upload-sourcemaps"]
32+
},
33+
"lint": {
34+
"executor": "@nx/linter:eslint",
35+
"outputs": ["{options.outputFile}"],
36+
"options": {
37+
"lintFilePatterns": [
38+
"examples/js-cloud-server/cloudflare-worker/**/*.ts"
39+
]
40+
}
41+
}
42+
},
43+
"tags": []
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { initializeDevCycle } from '@devcycle/js-cloud-server-sdk'
2+
3+
export interface Env {
4+
DEVCYCLE_SERVER_SDK_KEY: string
5+
}
6+
7+
export default {
8+
async fetch(
9+
request: Request,
10+
env: Env,
11+
ctx: ExecutionContext,
12+
): Promise<Response> {
13+
const devcycleClient = initializeDevCycle(env.DEVCYCLE_SERVER_SDK_KEY)
14+
15+
const variables = await devcycleClient.allVariables({
16+
user_id: 'test-user',
17+
})
18+
19+
return new Response(
20+
'DevCycle Variables: ' + JSON.stringify(variables, null, 2),
21+
)
22+
},
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../../dist/out-tsc",
5+
6+
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
7+
"lib": [
8+
"es2021"
9+
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
10+
"jsx": "react" /* Specify what JSX code is generated. */,
11+
"module": "es2022" /* Specify what module code is generated. */,
12+
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
13+
"types": [
14+
"@cloudflare/workers-types"
15+
] /* Specify type package names to be included without being referenced in a source file. */,
16+
"resolveJsonModule": true /* Enable importing .json files */,
17+
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
18+
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
19+
"noEmit": true /* Disable emitting files from a compilation. */,
20+
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
21+
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
22+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
23+
24+
"strict": true /* Enable all strict type-checking options. */,
25+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
26+
},
27+
"exclude": ["**/*.spec.ts", "**/*.test.ts"],
28+
"include": ["**/*.ts"]
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"files": [],
4+
"include": [],
5+
"references": [
6+
{
7+
"path": "./tsconfig.app.json"
8+
},
9+
{
10+
"path": "./tsconfig.spec.json"
11+
}
12+
]
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "js-cloud-server-sdk-test-worker"
2+
main = "src/index.ts"
3+
compatibility_date = "2023-09-18"
4+
account_id = "<Your Cloudflare Account ID>"

0 commit comments

Comments
 (0)