-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add google cloud function examples (#654)
* Add google cloud functions examples * Fix typo
- Loading branch information
1 parent
45280f8
commit 09ce667
Showing
7 changed files
with
110 additions
and
0 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,20 @@ | ||
# Google Cloud Functions Example with Typescript | ||
|
||
## How to use | ||
|
||
1. Clone and install the example | ||
|
||
```bash | ||
git clone https://github.com/upstash/upstash-redis.git | ||
cd upstash-redis/examples/google-cloud-functions | ||
npm install | ||
``` | ||
|
||
2. Create a free Database on [upstash.com](https://console.upstash.com/redis) | ||
3. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to | ||
`Runtime, build, connections and security settings > Runtime environment variables` in GCP website when creating a new function or pass those keys when you are deploying from the CLI. | ||
|
||
|
||
## Work locally | ||
|
||
Simply run `npm run start` |
19 changes: 19 additions & 0 deletions
19
examples/google-cloud-functions-with-typescript/package.json
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,19 @@ | ||
{ | ||
"name": "google-cloud-functions-typescript", | ||
"version": "1.0.0", | ||
"description": "@upstash-redis example using Google cloud functions with Typescript", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"start": "npx tsc-watch --onSuccess 'npx @google-cloud/functions-framework --target=helloWorld'", | ||
"build": "npx tsc" | ||
}, | ||
"dependencies": { | ||
"@google-cloud/functions-framework": "^3.3.0", | ||
"typescript": "^5.2.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.7.0", | ||
"ts-node": "^10.9.1", | ||
"tsc-watch": "^6.0.4" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/google-cloud-functions-with-typescript/src/index.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,17 @@ | ||
import functions = require("@google-cloud/functions-framework"); | ||
import { Redis } from "@upstash/redis/with-fetch"; | ||
|
||
functions.http("helloWorld", async (_req, res) => { | ||
const redis = Redis.fromEnv(); | ||
|
||
const set = await redis.set("rng", Math.random()); | ||
const get = await redis.get("rng"); | ||
|
||
res.send({ | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
set, | ||
get, | ||
}), | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
examples/google-cloud-functions-with-typescript/tsconfig.json
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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"outDir": "dist" | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["./node_modules/", "./dist/"] | ||
} |
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,20 @@ | ||
# Google Cloud Functions Example | ||
|
||
## How to use | ||
|
||
1. Clone and install the example | ||
|
||
```bash | ||
git clone https://github.com/upstash/upstash-redis.git | ||
cd upstash-redis/examples/google-cloud-functions | ||
npm install | ||
``` | ||
|
||
2. Create a free Database on [upstash.com](https://console.upstash.com/redis) | ||
3. Copy the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` to | ||
`Runtime, build, connections and security settings > Runtime environment variables` in GCP when creating a new function or pass those keys when you are deploying from the CLI. | ||
|
||
|
||
## Work locally | ||
|
||
Simply run `npm run start` |
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,17 @@ | ||
const functions = require("@google-cloud/functions-framework"); | ||
const { Redis } = require("@upstash/redis/with-fetch"); | ||
|
||
functions.http("helloWorld", async (_req, res) => { | ||
const redis = Redis.fromEnv(); | ||
|
||
const set = await redis.set("rng", Math.random()); | ||
const get = await redis.get("rng"); | ||
|
||
res.send({ | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
set, | ||
get, | ||
}), | ||
}); | ||
}); |
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,9 @@ | ||
{ | ||
"dependencies": { | ||
"@google-cloud/functions-framework": "^3.3.0", | ||
"@upstash/redis": "^1.23.3" | ||
}, | ||
"scripts": { | ||
"start": "npx @google-cloud/functions-framework --target=helloWorld" | ||
} | ||
} |