From d1f1f35d0697c5e0d101e51d4e0719454ea89e44 Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Mon, 22 Jul 2024 14:23:20 +0200 Subject: [PATCH] add outbound redis example Signed-off-by: karthik2804 --- .../spin-host-apis/spin-redis/README.md | 23 ++++++++++++ .../spin-host-apis/spin-redis/knitwit.json | 14 ++++++++ .../spin-host-apis/spin-redis/package.json | 23 ++++++++++++ .../spin-host-apis/spin-redis/spin.toml | 19 ++++++++++ .../spin-host-apis/spin-redis/src/index.ts | 25 +++++++++++++ .../spin-host-apis/spin-redis/src/spin.ts | 22 ++++++++++++ .../spin-host-apis/spin-redis/tsconfig.json | 18 ++++++++++ .../spin-redis/webpack.config.js | 35 +++++++++++++++++++ .../spin-variables/package.json | 2 +- 9 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 examples/typescript/spin-host-apis/spin-redis/README.md create mode 100644 examples/typescript/spin-host-apis/spin-redis/knitwit.json create mode 100644 examples/typescript/spin-host-apis/spin-redis/package.json create mode 100644 examples/typescript/spin-host-apis/spin-redis/spin.toml create mode 100644 examples/typescript/spin-host-apis/spin-redis/src/index.ts create mode 100644 examples/typescript/spin-host-apis/spin-redis/src/spin.ts create mode 100644 examples/typescript/spin-host-apis/spin-redis/tsconfig.json create mode 100644 examples/typescript/spin-host-apis/spin-redis/webpack.config.js diff --git a/examples/typescript/spin-host-apis/spin-redis/README.md b/examples/typescript/spin-host-apis/spin-redis/README.md new file mode 100644 index 00000000..44f6ce1b --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/README.md @@ -0,0 +1,23 @@ +# Using Spin Outbound Redis + +This example showcases using outbound Redis using the Spin SDK. + +## Install Dependencies +Install the necessary npm packages: + +```bash +npm install +``` + +## Prerequisites + +- A Redis server is running at `localhost:6379` + +## Building and Running the Example + +```bash +spin build +spin up +``` + +Use e.g. `curl -v http://127.0.0.1:3000/` to test the endpoint. \ No newline at end of file diff --git a/examples/typescript/spin-host-apis/spin-redis/knitwit.json b/examples/typescript/spin-host-apis/spin-redis/knitwit.json new file mode 100644 index 00000000..0d9f4396 --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/knitwit.json @@ -0,0 +1,14 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-http" + ] + }, + "packages": { + "@fermyon/spin-sdk": { + "witPath": "../../bin/wit", + "world": "spin-imports" + } + } +} \ No newline at end of file diff --git a/examples/typescript/spin-host-apis/spin-redis/package.json b/examples/typescript/spin-host-apis/spin-redis/package.json new file mode 100644 index 00000000..b4233a8d --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/package.json @@ -0,0 +1,23 @@ +{ + "name": "spin-redis", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/spin-redis.wasm", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "mkdirp": "^3.0.1", + "ts-loader": "^9.4.1", + "typescript": "^4.8.4", + "webpack": "^5.74.0", + "webpack-cli": "^4.10.0" + }, + "dependencies": { + "@fermyon/spin-sdk": "^2.0.0-alpha.3" + } +} \ No newline at end of file diff --git a/examples/typescript/spin-host-apis/spin-redis/spin.toml b/examples/typescript/spin-host-apis/spin-redis/spin.toml new file mode 100644 index 00000000..0255881b --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/spin.toml @@ -0,0 +1,19 @@ +spin_manifest_version = 2 + +[application] +authors = ["karthik2804 "] +description = "" +name = "spin-redis" +version = "0.1.0" + +[[trigger.http]] +route = "/..." +component = "spin-redis" + +[component.spin-redis] +source = "target/spin-redis.wasm" +exclude_files = ["**/node_modules"] +allowed_hosts = ["redis://*:*"] +[component.spin-redis.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/examples/typescript/spin-host-apis/spin-redis/src/index.ts b/examples/typescript/spin-host-apis/spin-redis/src/index.ts new file mode 100644 index 00000000..fccb1ed4 --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/src/index.ts @@ -0,0 +1,25 @@ +import { ResponseBuilder, Redis } from "@fermyon/spin-sdk"; + +const encoder = new TextEncoder() +const decoder = new TextDecoder() +const redisAddress = "redis://localhost:6379/" + +export async function handler(_req: Request, res: ResponseBuilder) { + + try { + let db = Redis.open(redisAddress) + db.set("test", encoder.encode("Hello world")) + let val = db.get("test") + + if (!val) { + res.status(404) + res.send() + return + } + + res.send(val) + } catch (e: any) { + res.status(500) + res.send(`Error: ${JSON.stringify(e.payload)}`) + } +} \ No newline at end of file diff --git a/examples/typescript/spin-host-apis/spin-redis/src/spin.ts b/examples/typescript/spin-host-apis/spin-redis/src/spin.ts new file mode 100644 index 00000000..b745952f --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/src/spin.ts @@ -0,0 +1,22 @@ +import { ResponseBuilder } from "@fermyon/spin-sdk"; +import { handler } from "."; + +//@ts-ignore +addEventListener('fetch', (event: FetchEvent) => { + handleEvent(event); +}); + +async function handleEvent(event: FetchEvent) { + + let resolve: any, reject: any; + let responsePromise = new Promise(async (res, rej) => { + resolve = res; + reject = rej; + }); + //@ts-ignore + event.respondWith(responsePromise); + + let res = new ResponseBuilder(resolve); + + await handler(event.request, res) +} \ No newline at end of file diff --git a/examples/typescript/spin-host-apis/spin-redis/tsconfig.json b/examples/typescript/spin-host-apis/spin-redis/tsconfig.json new file mode 100644 index 00000000..851fe99f --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "outDir": "./dist/", + "noImplicitAny": true, + "module": "es6", + "target": "es2020", + "jsx": "react", + "skipLibCheck": true, + "lib": [ + "ES2015", + "WebWorker" + ], + "allowJs": true, + "strict": true, + "noImplicitReturns": true, + "moduleResolution": "node" + } +} \ No newline at end of file diff --git a/examples/typescript/spin-host-apis/spin-redis/webpack.config.js b/examples/typescript/spin-host-apis/spin-redis/webpack.config.js new file mode 100644 index 00000000..6b624f55 --- /dev/null +++ b/examples/typescript/spin-host-apis/spin-redis/webpack.config.js @@ -0,0 +1,35 @@ +const path = require('path'); +const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") + +module.exports = { + entry: './src/spin.ts', + experiments: { + outputModule: true, + }, + module: { + rules: [ + { + test: /\.tsx?$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: ['.tsx', '.ts', '.js'], + }, + output: { + path: path.resolve(__dirname, './'), + filename: 'dist.js', + module: true, + library: { + type: "module", + } + }, + plugins: [ + new SpinSdkPlugin() + ], + optimization: { + minimize: false + }, +}; \ No newline at end of file diff --git a/examples/typescript/spin-host-apis/spin-variables/package.json b/examples/typescript/spin-host-apis/spin-variables/package.json index e8176a14..872ce525 100644 --- a/examples/typescript/spin-host-apis/spin-variables/package.json +++ b/examples/typescript/spin-host-apis/spin-variables/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/octokit-rest.wasm", + "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/spin-variables.wasm", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [],