Skip to content

Commit

Permalink
Add google cloud function examples (#654)
Browse files Browse the repository at this point in the history
* Add google cloud functions examples

* Fix typo
  • Loading branch information
ogzhanolguncu authored Oct 17, 2023
1 parent 45280f8 commit 09ce667
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/google-cloud-functions-with-typescript/README.md
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 examples/google-cloud-functions-with-typescript/package.json
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 examples/google-cloud-functions-with-typescript/src/index.ts
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 examples/google-cloud-functions-with-typescript/tsconfig.json
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/"]
}
20 changes: 20 additions & 0 deletions examples/google-cloud-functions/README.md
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`
17 changes: 17 additions & 0 deletions examples/google-cloud-functions/index.js
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,
}),
});
});
9 changes: 9 additions & 0 deletions examples/google-cloud-functions/package.json
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"
}
}

0 comments on commit 09ce667

Please sign in to comment.