From 11f31912884130dc0b03bb1e9e7fde7ed1b8c69f Mon Sep 17 00:00:00 2001 From: Pete Gautier Date: Tue, 25 Jul 2023 14:56:20 -0700 Subject: [PATCH 1/2] chore: update Cloudflare example README and audit other example READMEs --- examples/cloudflare-workers/README.md | 31 ++++++++++++++++++++------- examples/nodejs/README.md | 10 ++++++--- examples/nodejs/aws/README.md | 2 +- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/examples/cloudflare-workers/README.md b/examples/cloudflare-workers/README.md index 088d79492..aae230194 100644 --- a/examples/cloudflare-workers/README.md +++ b/examples/cloudflare-workers/README.md @@ -6,7 +6,7 @@ typescript worker ## How to use -1. Clone and install the example +1. Clone and install the example: ```bash git clone https://github.com/momentohq/client-sdk-javascript.git @@ -14,16 +14,31 @@ cd client-sdk-javascript/examples/cloudflare-workers npm install ``` -2. Create a Momento cache inside of the [console](https://console.gomomento.com) -3. Create a [token for your cache](https://console.gomomento.com/tokens) -4. Copy the `Rest Endpoint`, and cache name to the `wranger.toml` file -5. Copy the `Auth Token` to the `.dev.vars` files -5. Start the development server +1. Create a cache inside of the [Momento console](https://console.gomomento.com/caches). Note that the Momento Cache HTTP API required for Cloudflare support is currently only available in AWS. Make note of the region you created your cache in, as you'll need it for the next step. +1. Generate a [token for your cache](https://console.gomomento.com/tokens), making sure to choose the same AWS region you used to create the cache. You'll want to use a `Fine-Grained Access Token` with read/write permissions for the cache you created. + ![Console Token Screen](https://assets.website-files.com/628fadb065a50abf13a11485/64b97cb50a7e1d8d752ae539_3fU8mYh6gAhMwUYzrLOEiEXQc-KO79zANMtiH141Js2tZydZ7sFxZtr5TWLcC3OzFJTIEMZQOkLtWtBOOTEOEXmpinv1Ah3AC_LdkovI3FU7iUGY_N35cB0op1PXTNHAW0kZ-9wZ6qrCol5wrz_nuA.png) +1. Copy the `Auth Token` and `HTTP Endpoint` values from the token generation screen for use in the next two steps. + ![Token generation results](https://assets.website-files.com/628fadb065a50abf13a11485/64b97cb50d9a0db6b03c40e8_JZLnsjtwN5RaGx83NX424WKmvauAuqcUD3YeWLx2LFFIwLiXHupq1XF3MOyggObfaC8LE1fQUN4b-9nDTOwGYUHugfZYqYTK92HybD2X1OsuRF-DxmJKekTWgV0SY0LzWpE9vvA0To8sGmNXkG-geQ.png) +1. Cloudflare uses a file called wrangler.toml to configure the development and publishing of a worker. More information about Cloudflare configuration can be found [on their website](https://developers.cloudflare.com/workers/wrangler/configuration/). In the example directory, update the `wrangler.toml` file with the cache name and HTTP endpoint as they appeared in the Momento Console. +1. Update the `.dev.vars` file in the example directory with the Momento auth token. Since this is a secret token, we don’t store it as an environment variable, instead storing it as a Cloudflare secret. +1. Start the development server: ```bash npm run start ``` -6. Open your browser at [localhost:8787](http://localhost:8787) +7. Open your browser to [localhost:8787](http://localhost:8787). The code in this example sets an item sets an item in the cache, gets it, and returns it as a JSON object. -A deployed example can be found [here](https://momento-cloudflare-worker-http.mst-a09.workers.dev) \ No newline at end of file +A deployed example can be found [here](https://momento-cloudflare-worker-http.mst-a09.workers.dev). + +If you would like to deploy this example to your own Cloudflare worker, make sure you add the MOMENTO_AUTH_TOKEN as a secret inside of your Cloudflare account: + +```shell + +npx secret put MOMENTO_AUTH_TOKEN +> Enter a secret value: ************************** +> 🌀 Creating the secret for the Worker "momento-cloudflare-worker-http" +> ✨ Success! Uploaded secret MOMENTO_AUTH_TOKEN +``` + +Then you can run `npm run deploy`. You will be prompted for a Cloudflare login before deploying to your Cloudflare account. diff --git a/examples/nodejs/README.md b/examples/nodejs/README.md index 9f9a83ff7..88c961e71 100644 --- a/examples/nodejs/README.md +++ b/examples/nodejs/README.md @@ -17,8 +17,12 @@ This directory contains several subdirectories with different types of example projects: +- [Access Control](./access-control) - How to implement access control using Momento API tokens +- [AWS Secrets Manager](./aws) - How to retrieve a Momento auth token stored as a secret in AWS Secrets Manager and use it to create a cache - [Cache Examples](./cache) - How to use Momento Cache -- [Topic Examples](./topics) - How to use Momento Topics (aka PubSub) -- [Observability](./observability) - How to configure logging, metrics and traces +- [Lambda Example - Topics Microservice](./lambda-examples/topics-microservice) - Creates a microservice using API Gateway and an AWS Lambda function to ingest data into a topic in Momento Topics - [Load Generator](./load-gen) - An example load generator for observing Momento performance -- [Access Control](./access-control) - How to implement access control using Momento API tokens +- [MongoDB + Momento Cache Read-aside Example](./mongodb-examples/simple-read-aside) - A simple example of using Momento Cache as a read-aside cache for simple MongoDB calls +- [Observability](./observability) - How to configure logging, metrics and traces +- [Token Vending Machine](./token-vending-machine) - Built using AWS CDK, API Gateway and Lambda, the primary use for the Token Vending Machine is to provide temporary, restricted scope Momento Auth tokens. These tokens can be used by browsers that are running apps written against the [Momento Web SDK](https://github.com/momentohq/client-sdk-javascript/tree/main/packages/client-sdk-web). +- [Topic Examples](./topics) - How to use Momento Topics (aka PubSub) diff --git a/examples/nodejs/aws/README.md b/examples/nodejs/aws/README.md index 10bbcb391..8a6ca1ee7 100644 --- a/examples/nodejs/aws/README.md +++ b/examples/nodejs/aws/README.md @@ -29,7 +29,7 @@ This example demonstrates how to retrieve a Momento auth token stored as a secre MOMENTO_AUTH_TOKEN= npm run secrets-example ``` -[Example Code](examples/nodejs/aws/doc-example-files/doc-examples-js-aws-secrets.ts) +[Example Code](doc-example-files/doc-examples-js-aws-secrets.ts) If you have questions or need help experimenting further, please reach out to us! From 18a81f912434b88207d0ae3fdc319f74999690f2 Mon Sep 17 00:00:00 2001 From: Pete Gautier Date: Wed, 26 Jul 2023 16:35:51 -0700 Subject: [PATCH 2/2] chore: fix repeated text and add example code --- examples/cloudflare-workers/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/examples/cloudflare-workers/README.md b/examples/cloudflare-workers/README.md index aae230194..f421c197d 100644 --- a/examples/cloudflare-workers/README.md +++ b/examples/cloudflare-workers/README.md @@ -27,7 +27,18 @@ npm install npm run start ``` -7. Open your browser to [localhost:8787](http://localhost:8787). The code in this example sets an item sets an item in the cache, gets it, and returns it as a JSON object. +7. Open your browser to [localhost:8787](http://localhost:8787). The code in this example sets an item in the cache, gets it, and returns it as a JSON object: + ``` + // setting a value into cache + const setResp = await client.set(cache, key, value); + console.log("setResp", setResp); + + // getting a value from cache + const getResp = await client.get(cache, key) + console.log("getResp", getResp); + + return new Response(JSON.stringify({ response: getResp })); + ``` A deployed example can be found [here](https://momento-cloudflare-worker-http.mst-a09.workers.dev).