Skip to content

Commit

Permalink
chore: update Cloudflare example README and audit other example READM…
Browse files Browse the repository at this point in the history
…Es (#676)

* chore: update Cloudflare example README and audit other example READMEs

* chore: fix repeated text and add example code
  • Loading branch information
pgautier404 authored Jul 31, 2023
1 parent 80ab1fb commit a94a6cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
42 changes: 34 additions & 8 deletions examples/cloudflare-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,50 @@ 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
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 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);
A deployed example can be found [here](https://momento-cloudflare-worker-http.mst-a09.workers.dev)
// 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).

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.
10 changes: 7 additions & 3 deletions examples/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion examples/nodejs/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This example demonstrates how to retrieve a Momento auth token stored as a secre
MOMENTO_AUTH_TOKEN=<YOUR 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!

Expand Down

0 comments on commit a94a6cd

Please sign in to comment.