-
Notifications
You must be signed in to change notification settings - Fork 10
feat: DVC-9055 adding Cloudflare Worker example app for new js-cloud-server-sdk #561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9c5029c
feat: adding Cloudflare Worker example app for js-cloud-server-sdk
jonathannorris 469291d
feat: hook up cf worker example app to nx, add README.md
jonathannorris b3597ad
feat: add cf worker example test workflow
jonathannorris 4e33795
fix: test workflow
jonathannorris da80774
fix: test workflow on push
jonathannorris a201395
fix: try running in background
jonathannorris 5a12cbd
fix: run command
jonathannorris cf5c87c
fix: on run on PRs against main
jonathannorris e70200f
fix: resolve PR comments
jonathannorris 6903e29
fix: remove build dep
jonathannorris 65c293b
fix: update example init
jonathannorris 122537f
fix: eslint by adding .eslintrc.json file
jonathannorris 877bdf6
fix: run prettier
jonathannorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
| name: Run CF Worker Example App | ||
| on: | ||
| pull_request: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: | ||
| labels: ubuntu-latest-4-core | ||
| strategy: | ||
| matrix: | ||
| node-version: [18.x] | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v3 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| cache: 'yarn' | ||
| - name: Run Yarn | ||
| run: yarn --immutable | ||
| - name: Setup .dev.vars file | ||
| run: echo "DEVCYCLE_SERVER_SDK_KEY=${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}" > examples/js-cloud-server/cloudflare-worker/.dev.vars | ||
| - name: Run example app in background | ||
| run: | | ||
| yarn nx serve example-js-cloud-server-sdk-cf-worker & | ||
| echo "SERVER_PID=$!" >> $GITHUB_ENV | ||
| continue-on-error: true | ||
| - name: Wait for the server to be up | ||
| run: sleep 10 | ||
| - name: Test server with curl | ||
| run: | | ||
| RESPONSE=$(curl -s http://localhost:8787) # Replace with your server's port | ||
| # Check the response or do something based on the result. | ||
| if [[ "$RESPONSE" != *"DevCycle Variables:"* ]]; then | ||
| echo "Server didn't return the expected 'DevCycle Variables:' response" | ||
| exit 1 | ||
| fi | ||
| - name: Cleanup server | ||
| run: kill ${{ env.SERVER_PID }} |
This file contains hidden or 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": ["!**/*", "node_modules/*"], | ||
| "overrides": [ | ||
| { | ||
| "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.ts", "*.tsx"], | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.js", "*.jsx"], | ||
| "rules": {} | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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,6 @@ | ||
| .env | ||
| .env.* | ||
| dist | ||
| node_modules | ||
| .dev.vars | ||
| .wrangler |
This file contains hidden or 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,21 @@ | ||
| # DevCycle JS Cloud Bucketing Server SDK - Cloudflare Worker Example App | ||
|
|
||
| Example app for using the DevCycle JS Cloud Bucketing Server SDK with Cloudflare Workers. | ||
|
|
||
| ## Serve Worker Locally | ||
|
|
||
| 1. Run `yarn` in the root of this repo to install all dependencies. | ||
| 2. Set up a `.dev.vars` file in this folder containing your DevCycle Server SDK Key for your project: | ||
| - ```DEVCYCLE_SERVER_SDK_KEY=<YOUR SERVER SDK KEY>``` | ||
| 3. Run this worker locally using: `yarn nx serve example-js-cloud-server-sdk-cf-worker` | ||
|
|
||
| ## Deploy Worker to Cloudflare | ||
|
|
||
| 1. Run `yarn` in the root of this repo to install all dependencies. | ||
| 2. Set a `DEVCYCLE_SERVER_SDK_KEY` secret in Cloudflare using the wrangler CLI from the root: | ||
| - ```yarn wrangler secret put DEVCYCLE_SERVER_SDK_KEY -c examples/js-cloud-server/cloudflare-worker/wrangler.toml``` | ||
| - you may be prompted to login to Wrangler with your Cloudflare account | ||
| 3. Set your Cloudflare account ID in `wrangler.toml`: | ||
| - you can find this under your account's profile on the Cloudflare dashboard | ||
| 4. Deploy this worker to Cloudflare using: `yarn nx deploy example-js-cloud-server-sdk-cf-worker` | ||
|
|
||
This file contains hidden or 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,12 @@ | ||
| { | ||
| "name": "js-cloud-server-sdk-test-worker", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "deploy": "wrangler deploy", | ||
| "start": "wrangler dev" | ||
| }, | ||
| "dependencies": { | ||
| "@devcycle/js-cloud-server-sdk": "1.0.0" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| { | ||
| "name": "example-js-cloud-server-sdk-cf-worker", | ||
| "$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
| "sourceRoot": "examples/js-cloud-server/cloudflare-worker/src", | ||
| "projectType": "application", | ||
| "targets": { | ||
| "build": { | ||
| "executor": "@nx/webpack:webpack", | ||
| "outputs": ["{options.outputPath}"], | ||
| "options": { | ||
| "outputPath": "dist/examples/js-cloud-server/cloudflare-worker", | ||
| "main": "examples/js-cloud-server/cloudflare-worker/src/index.ts", | ||
| "tsConfig": "examples/js-cloud-server/cloudflare-worker/tsconfig.json", | ||
| "compiler": "tsc", | ||
| "target": "node" | ||
| } | ||
| }, | ||
| "serve": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "command": "yarn run -T wrangler dev", | ||
| "cwd": "examples/js-cloud-server/cloudflare-worker" | ||
| } | ||
| }, | ||
| "deploy": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "command": "yarn run -T wrangler deploy", | ||
| "cwd": "examples/js-cloud-server/cloudflare-worker" | ||
| }, | ||
| "dependsOn": ["upload-sourcemaps"] | ||
| }, | ||
| "lint": { | ||
| "executor": "@nx/linter:eslint", | ||
| "outputs": ["{options.outputFile}"], | ||
| "options": { | ||
| "lintFilePatterns": [ | ||
| "examples/js-cloud-server/cloudflare-worker/**/*.ts" | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "tags": [] | ||
| } |
This file contains hidden or 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,23 @@ | ||
| import { initializeDevCycle } from '@devcycle/js-cloud-server-sdk' | ||
|
|
||
| export interface Env { | ||
| DEVCYCLE_SERVER_SDK_KEY: string | ||
| } | ||
|
|
||
| export default { | ||
| async fetch( | ||
| request: Request, | ||
| env: Env, | ||
| ctx: ExecutionContext, | ||
| ): Promise<Response> { | ||
| const devcycleClient = initializeDevCycle(env.DEVCYCLE_SERVER_SDK_KEY) | ||
|
|
||
| const variables = await devcycleClient.allVariables({ | ||
| user_id: 'test-user', | ||
| }) | ||
|
|
||
| return new Response( | ||
| 'DevCycle Variables: ' + JSON.stringify(variables, null, 2), | ||
| ) | ||
| }, | ||
| } |
29 changes: 29 additions & 0 deletions
29
examples/js-cloud-server/cloudflare-worker/tsconfig.app.json
This file contains hidden or 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,29 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "../../../dist/out-tsc", | ||
|
|
||
| "target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, | ||
| "lib": [ | ||
| "es2021" | ||
| ] /* Specify a set of bundled library declaration files that describe the target runtime environment. */, | ||
| "jsx": "react" /* Specify what JSX code is generated. */, | ||
| "module": "es2022" /* Specify what module code is generated. */, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that this is the first time we're compiling to es modules in this repo. May be relevant later |
||
| "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, | ||
| "types": [ | ||
| "@cloudflare/workers-types" | ||
| ] /* Specify type package names to be included without being referenced in a source file. */, | ||
| "resolveJsonModule": true /* Enable importing .json files */, | ||
| "allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */, | ||
| "checkJs": false /* Enable error reporting in type-checked JavaScript files. */, | ||
| "noEmit": true /* Disable emitting files from a compilation. */, | ||
| "isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */, | ||
| "allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */, | ||
| "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, | ||
|
|
||
| "strict": true /* Enable all strict type-checking options. */, | ||
| "skipLibCheck": true /* Skip type checking all .d.ts files. */ | ||
| }, | ||
| "exclude": ["**/*.spec.ts", "**/*.test.ts"], | ||
| "include": ["**/*.ts"] | ||
| } | ||
This file contains hidden or 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,13 @@ | ||
| { | ||
| "extends": "../../../tsconfig.base.json", | ||
| "files": [], | ||
| "include": [], | ||
| "references": [ | ||
| { | ||
| "path": "./tsconfig.app.json" | ||
| }, | ||
| { | ||
| "path": "./tsconfig.spec.json" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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,4 @@ | ||
| name = "js-cloud-server-sdk-test-worker" | ||
| main = "src/index.ts" | ||
| compatibility_date = "2023-09-18" | ||
| account_id = "<Your Cloudflare Account ID>" |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whats the purpose of this example app? is it to provide an example for the public looking to use devcycle in a cloudflare worker? or is it a development aid for trying out the SDK the worker?
if its the former, I don't think it should be in the monorepo
if its the latter, it's not gonna be locally linked to the source files of the SDK like the other examples are
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As with all the example apps in this repo, its a bit of both.
Why do you say its not going to be linked?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually I guess it will be if webpack is able to resolve all of the packages through the tsconfig aliases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea like it works as is right now, and the npm package isn't published yet.