From 89b9e27395aaa3f88ef4a84c140876d224a859da Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Tue, 23 Jul 2024 13:37:13 +0200 Subject: [PATCH 1/2] add mysql, pg, mqtt and outbound examples Signed-off-by: karthik2804 --- .../common-patterns/outbound-http/README.md | 19 +++++++++++ .../outbound-http/knitwit.json | 14 ++++++++ .../outbound-http/package.json | 23 +++++++++++++ .../common-patterns/outbound-http/spin.toml | 19 +++++++++++ .../outbound-http/src/index.ts | 10 ++++++ .../common-patterns/outbound-http/src/spin.ts | 21 ++++++++++++ .../outbound-http/tsconfig.json | 18 ++++++++++ .../outbound-http/webpack.config.js | 33 ++++++++++++++++++ examples/spin-host-apis/spin-mqtt/README.md | 34 +++++++++++++++++++ .../spin-host-apis/spin-mqtt/knitwit.json | 11 ++++++ .../spin-host-apis/spin-mqtt/package.json | 23 +++++++++++++ examples/spin-host-apis/spin-mqtt/spin.toml | 19 +++++++++++ .../spin-host-apis/spin-mqtt/src/index.ts | 25 ++++++++++++++ examples/spin-host-apis/spin-mqtt/src/spin.ts | 21 ++++++++++++ .../spin-host-apis/spin-mqtt/tsconfig.json | 18 ++++++++++ .../spin-mqtt/webpack.config.js | 33 ++++++++++++++++++ examples/spin-host-apis/spin-mysql/README.md | 30 ++++++++++++++++ .../spin-host-apis/spin-mysql/knitwit.json | 11 ++++++ .../spin-host-apis/spin-mysql/package.json | 23 +++++++++++++ examples/spin-host-apis/spin-mysql/spin.toml | 19 +++++++++++ .../spin-host-apis/spin-mysql/src/index.ts | 13 +++++++ .../spin-host-apis/spin-mysql/src/spin.ts | 21 ++++++++++++ .../spin-host-apis/spin-mysql/tsconfig.json | 18 ++++++++++ .../spin-mysql/webpack.config.js | 33 ++++++++++++++++++ .../spin-host-apis/spin-postgres/README.md | 30 ++++++++++++++++ .../spin-host-apis/spin-postgres/knitwit.json | 11 ++++++ .../spin-host-apis/spin-postgres/package.json | 23 +++++++++++++ .../spin-host-apis/spin-postgres/spin.toml | 19 +++++++++++ .../spin-host-apis/spin-postgres/src/index.ts | 12 +++++++ .../spin-host-apis/spin-postgres/src/spin.ts | 21 ++++++++++++ .../spin-postgres/tsconfig.json | 18 ++++++++++ .../spin-postgres/webpack.config.js | 33 ++++++++++++++++++ 32 files changed, 676 insertions(+) create mode 100644 examples/common-patterns/outbound-http/README.md create mode 100644 examples/common-patterns/outbound-http/knitwit.json create mode 100644 examples/common-patterns/outbound-http/package.json create mode 100644 examples/common-patterns/outbound-http/spin.toml create mode 100644 examples/common-patterns/outbound-http/src/index.ts create mode 100644 examples/common-patterns/outbound-http/src/spin.ts create mode 100644 examples/common-patterns/outbound-http/tsconfig.json create mode 100644 examples/common-patterns/outbound-http/webpack.config.js create mode 100644 examples/spin-host-apis/spin-mqtt/README.md create mode 100644 examples/spin-host-apis/spin-mqtt/knitwit.json create mode 100644 examples/spin-host-apis/spin-mqtt/package.json create mode 100644 examples/spin-host-apis/spin-mqtt/spin.toml create mode 100644 examples/spin-host-apis/spin-mqtt/src/index.ts create mode 100644 examples/spin-host-apis/spin-mqtt/src/spin.ts create mode 100644 examples/spin-host-apis/spin-mqtt/tsconfig.json create mode 100644 examples/spin-host-apis/spin-mqtt/webpack.config.js create mode 100644 examples/spin-host-apis/spin-mysql/README.md create mode 100644 examples/spin-host-apis/spin-mysql/knitwit.json create mode 100644 examples/spin-host-apis/spin-mysql/package.json create mode 100644 examples/spin-host-apis/spin-mysql/spin.toml create mode 100644 examples/spin-host-apis/spin-mysql/src/index.ts create mode 100644 examples/spin-host-apis/spin-mysql/src/spin.ts create mode 100644 examples/spin-host-apis/spin-mysql/tsconfig.json create mode 100644 examples/spin-host-apis/spin-mysql/webpack.config.js create mode 100644 examples/spin-host-apis/spin-postgres/README.md create mode 100644 examples/spin-host-apis/spin-postgres/knitwit.json create mode 100644 examples/spin-host-apis/spin-postgres/package.json create mode 100644 examples/spin-host-apis/spin-postgres/spin.toml create mode 100644 examples/spin-host-apis/spin-postgres/src/index.ts create mode 100644 examples/spin-host-apis/spin-postgres/src/spin.ts create mode 100644 examples/spin-host-apis/spin-postgres/tsconfig.json create mode 100644 examples/spin-host-apis/spin-postgres/webpack.config.js diff --git a/examples/common-patterns/outbound-http/README.md b/examples/common-patterns/outbound-http/README.md new file mode 100644 index 00000000..53a6de10 --- /dev/null +++ b/examples/common-patterns/outbound-http/README.md @@ -0,0 +1,19 @@ +# Using Outbound HTTP + +This example showcases making an outbound HTTP request and returning the response. + +## Install Dependencies +Install the necessary npm packages: + +```bash +npm install +``` + +## 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. diff --git a/examples/common-patterns/outbound-http/knitwit.json b/examples/common-patterns/outbound-http/knitwit.json new file mode 100644 index 00000000..0d9f4396 --- /dev/null +++ b/examples/common-patterns/outbound-http/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/common-patterns/outbound-http/package.json b/examples/common-patterns/outbound-http/package.json new file mode 100644 index 00000000..77d6c940 --- /dev/null +++ b/examples/common-patterns/outbound-http/package.json @@ -0,0 +1,23 @@ +{ + "name": "outbound-http", + "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/outbound-http.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/common-patterns/outbound-http/spin.toml b/examples/common-patterns/outbound-http/spin.toml new file mode 100644 index 00000000..8541e392 --- /dev/null +++ b/examples/common-patterns/outbound-http/spin.toml @@ -0,0 +1,19 @@ +spin_manifest_version = 2 + +[application] +authors = ["karthik2804 "] +description = "" +name = "outbound-http" +version = "0.1.0" + +[[trigger.http]] +route = "/..." +component = "outbound-http" + +[component.outbound-http] +source = "target/outbound-http.wasm" +exclude_files = ["**/node_modules"] +allowed_outbound_hosts = ["*://*:*"] +[component.outbound-http.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/examples/common-patterns/outbound-http/src/index.ts b/examples/common-patterns/outbound-http/src/index.ts new file mode 100644 index 00000000..a70e345e --- /dev/null +++ b/examples/common-patterns/outbound-http/src/index.ts @@ -0,0 +1,10 @@ +import { ResponseBuilder } from '@fermyon/spin-sdk'; + +export async function handler(_req: Request, res: ResponseBuilder) { + let response = await fetch( + 'https://random-data-api.fermyon.app/physics/json', + ); + let physicsFact = await response.text(); + + res.send(physicsFact); +} diff --git a/examples/common-patterns/outbound-http/src/spin.ts b/examples/common-patterns/outbound-http/src/spin.ts new file mode 100644 index 00000000..9ee47db3 --- /dev/null +++ b/examples/common-patterns/outbound-http/src/spin.ts @@ -0,0 +1,21 @@ +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); +} diff --git a/examples/common-patterns/outbound-http/tsconfig.json b/examples/common-patterns/outbound-http/tsconfig.json new file mode 100644 index 00000000..851fe99f --- /dev/null +++ b/examples/common-patterns/outbound-http/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/common-patterns/outbound-http/webpack.config.js b/examples/common-patterns/outbound-http/webpack.config.js new file mode 100644 index 00000000..3a767ede --- /dev/null +++ b/examples/common-patterns/outbound-http/webpack.config.js @@ -0,0 +1,33 @@ +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, + }, +}; diff --git a/examples/spin-host-apis/spin-mqtt/README.md b/examples/spin-host-apis/spin-mqtt/README.md new file mode 100644 index 00000000..5a0adc9a --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/README.md @@ -0,0 +1,34 @@ +# Using Spin Outbound MQTT + +This example showcases using outbound MQTT with the Spin SDK. + +## Install Dependencies +Install the necessary npm packages: + +```bash +npm install +``` + +## Building and Running the Example + +For this example, configure the app with the address, username password and topics in `src/index.ts`: + +```js +const config = { + address: "", + username: "", + password: "", + KeepAlive: 60, +} + +const topic = ""; +``` + +Build and run the app: + +```bash +spin build +spin up +``` + +Use e.g. `curl -v http://127.0.0.1:3000/` to test the endpoint. diff --git a/examples/spin-host-apis/spin-mqtt/knitwit.json b/examples/spin-host-apis/spin-mqtt/knitwit.json new file mode 100644 index 00000000..5988df65 --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/knitwit.json @@ -0,0 +1,11 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-http" + ] + }, + "packages": { + "@fermyon/spin-sdk": {} + } +} \ No newline at end of file diff --git a/examples/spin-host-apis/spin-mqtt/package.json b/examples/spin-host-apis/spin-mqtt/package.json new file mode 100644 index 00000000..3d67d9a2 --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/package.json @@ -0,0 +1,23 @@ +{ + "name": "spin-mqtt", + "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-mqtt.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/spin-host-apis/spin-mqtt/spin.toml b/examples/spin-host-apis/spin-mqtt/spin.toml new file mode 100644 index 00000000..d5c149e8 --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/spin.toml @@ -0,0 +1,19 @@ +spin_manifest_version = 2 + +[application] +authors = ["karthik2804 "] +description = "" +name = "spin-mqtt" +version = "0.1.0" + +[[trigger.http]] +route = "/..." +component = "spin-mqtt" + +[component.spin-mqtt] +source = "target/spin-mqtt.wasm" +exclude_files = ["**/node_modules"] +allowed_outbound_hosts = ["mqtt://*:*"] +[component.spin-mqtt.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/examples/spin-host-apis/spin-mqtt/src/index.ts b/examples/spin-host-apis/spin-mqtt/src/index.ts new file mode 100644 index 00000000..60a4da2a --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/src/index.ts @@ -0,0 +1,25 @@ +import { ResponseBuilder, Mqtt } from '@fermyon/spin-sdk'; +import { QoS } from '@fermyon/spin-sdk/lib/mqtt'; + +const encoder = new TextEncoder(); + +const config = { + address: '', + username: '', + password: '', + KeepAlive: 60, +}; + +const topic = ''; + +export async function handler(_req: Request, res: ResponseBuilder) { + let conn = Mqtt.open( + config.address, + config.username, + config.password, + config.KeepAlive, + ); + conn.publish(topic, encoder.encode('message'), QoS.AtMostOnce); + + res.send('Message published successfully'); +} diff --git a/examples/spin-host-apis/spin-mqtt/src/spin.ts b/examples/spin-host-apis/spin-mqtt/src/spin.ts new file mode 100644 index 00000000..9ee47db3 --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/src/spin.ts @@ -0,0 +1,21 @@ +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); +} diff --git a/examples/spin-host-apis/spin-mqtt/tsconfig.json b/examples/spin-host-apis/spin-mqtt/tsconfig.json new file mode 100644 index 00000000..851fe99f --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/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/spin-host-apis/spin-mqtt/webpack.config.js b/examples/spin-host-apis/spin-mqtt/webpack.config.js new file mode 100644 index 00000000..3a767ede --- /dev/null +++ b/examples/spin-host-apis/spin-mqtt/webpack.config.js @@ -0,0 +1,33 @@ +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, + }, +}; diff --git a/examples/spin-host-apis/spin-mysql/README.md b/examples/spin-host-apis/spin-mysql/README.md new file mode 100644 index 00000000..dee0efe2 --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/README.md @@ -0,0 +1,30 @@ +# Using Spin Outbound MySQL + +This example showcases using outbound MySQL with the Spin SDK. + +## Install Dependencies +Install the necessary npm packages: + +```bash +npm install +``` + +## Building and Running the Example + +Setup the MySQL database instance running at `127.0.0.1`. Run the following commands after connecting to it: + +```bash +create database spin_dev; +use spin_dev; +create table test(id int, val int); +insert into test values (4,4); +``` + +Build and run the app: + +```bash +spin build +spin up +``` + +Use e.g. `curl -v http://127.0.0.1:3000/` to test the endpoint. diff --git a/examples/spin-host-apis/spin-mysql/knitwit.json b/examples/spin-host-apis/spin-mysql/knitwit.json new file mode 100644 index 00000000..5988df65 --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/knitwit.json @@ -0,0 +1,11 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-http" + ] + }, + "packages": { + "@fermyon/spin-sdk": {} + } +} \ No newline at end of file diff --git a/examples/spin-host-apis/spin-mysql/package.json b/examples/spin-host-apis/spin-mysql/package.json new file mode 100644 index 00000000..59c29970 --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/package.json @@ -0,0 +1,23 @@ +{ + "name": "spin-mysql", + "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-mysql.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/spin-host-apis/spin-mysql/spin.toml b/examples/spin-host-apis/spin-mysql/spin.toml new file mode 100644 index 00000000..7dc46756 --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/spin.toml @@ -0,0 +1,19 @@ +spin_manifest_version = 2 + +[application] +authors = ["karthik2804 "] +description = "" +name = "spin-mysql" +version = "0.1.0" + +[[trigger.http]] +route = "/..." +component = "spin-mysql" + +[component.spin-mysql] +source = "target/spin-mysql.wasm" +exclude_files = ["**/node_modules"] +allowed_outbound_hosts = ["mysql://*:*"] +[component.spin-mysql.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/examples/spin-host-apis/spin-mysql/src/index.ts b/examples/spin-host-apis/spin-mysql/src/index.ts new file mode 100644 index 00000000..005e105d --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/src/index.ts @@ -0,0 +1,13 @@ +import { ResponseBuilder, Mysql } from '@fermyon/spin-sdk'; + +// Connects as the root user without a password +const DB_URL = 'mysql://root:@127.0.0.1/spin_dev'; + +export async function handler(_req: Request, res: ResponseBuilder) { + let conn = Mysql.open(DB_URL); + conn.execute('delete from test where id=?', [4]); + conn.execute('insert into test values (4,5)', []); + let ret = conn.query('select * from test', []); + + res.send(JSON.stringify(ret, null, 2)); +} diff --git a/examples/spin-host-apis/spin-mysql/src/spin.ts b/examples/spin-host-apis/spin-mysql/src/spin.ts new file mode 100644 index 00000000..9ee47db3 --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/src/spin.ts @@ -0,0 +1,21 @@ +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); +} diff --git a/examples/spin-host-apis/spin-mysql/tsconfig.json b/examples/spin-host-apis/spin-mysql/tsconfig.json new file mode 100644 index 00000000..851fe99f --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/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/spin-host-apis/spin-mysql/webpack.config.js b/examples/spin-host-apis/spin-mysql/webpack.config.js new file mode 100644 index 00000000..3a767ede --- /dev/null +++ b/examples/spin-host-apis/spin-mysql/webpack.config.js @@ -0,0 +1,33 @@ +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, + }, +}; diff --git a/examples/spin-host-apis/spin-postgres/README.md b/examples/spin-host-apis/spin-postgres/README.md new file mode 100644 index 00000000..b90de815 --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/README.md @@ -0,0 +1,30 @@ +# Using Spin Outbound PostgreSQL + +This example showcases using outbound PostgreSQL with the Spin SDK. + +## Install Dependencies +Install the necessary npm packages: + +```bash +npm install +``` + +## Building and Running the Example + +Setup the PostgreSQL database instance running at `127.0.0.1`. Run the following commands after connecting to it: + +```bash +create database spin_dev; +\c spin_dev; +create table test(id int, val int); +insert into test values (4,4); +``` + +Build and run the app: + +```bash +spin build +spin up +``` + +Use e.g. `curl -v http://127.0.0.1:3000/` to test the endpoint. diff --git a/examples/spin-host-apis/spin-postgres/knitwit.json b/examples/spin-host-apis/spin-postgres/knitwit.json new file mode 100644 index 00000000..5988df65 --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/knitwit.json @@ -0,0 +1,11 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-http" + ] + }, + "packages": { + "@fermyon/spin-sdk": {} + } +} \ No newline at end of file diff --git a/examples/spin-host-apis/spin-postgres/package.json b/examples/spin-host-apis/spin-postgres/package.json new file mode 100644 index 00000000..7edfd3a5 --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/package.json @@ -0,0 +1,23 @@ +{ + "name": "spin-postgres", + "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-postgres.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/spin-host-apis/spin-postgres/spin.toml b/examples/spin-host-apis/spin-postgres/spin.toml new file mode 100644 index 00000000..a7475be2 --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/spin.toml @@ -0,0 +1,19 @@ +spin_manifest_version = 2 + +[application] +authors = ["karthik2804 "] +description = "" +name = "spin-postgres" +version = "0.1.0" + +[[trigger.http]] +route = "/..." +component = "spin-postgres" + +[component.spin-postgres] +source = "target/spin-postgres.wasm" +exclude_files = ["**/node_modules"] +allowed_outbound_hosts = ["postgres://*:*"] +[component.spin-postgres.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/examples/spin-host-apis/spin-postgres/src/index.ts b/examples/spin-host-apis/spin-postgres/src/index.ts new file mode 100644 index 00000000..949286f9 --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/src/index.ts @@ -0,0 +1,12 @@ +import { ResponseBuilder, Postgres } from '@fermyon/spin-sdk'; + +const DB_URL = 'host=localhost user=postgres dbname=spin_dev'; + +export async function handler(_req: Request, res: ResponseBuilder) { + let conn = Postgres.open(DB_URL); + conn.execute('delete from test where id=4', []); + conn.execute('insert into test values (4,5)', []); + let ret = conn.query('select * from test', []); + + res.send(JSON.stringify(ret, null, 2)); +} diff --git a/examples/spin-host-apis/spin-postgres/src/spin.ts b/examples/spin-host-apis/spin-postgres/src/spin.ts new file mode 100644 index 00000000..9ee47db3 --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/src/spin.ts @@ -0,0 +1,21 @@ +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); +} diff --git a/examples/spin-host-apis/spin-postgres/tsconfig.json b/examples/spin-host-apis/spin-postgres/tsconfig.json new file mode 100644 index 00000000..851fe99f --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/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/spin-host-apis/spin-postgres/webpack.config.js b/examples/spin-host-apis/spin-postgres/webpack.config.js new file mode 100644 index 00000000..3a767ede --- /dev/null +++ b/examples/spin-host-apis/spin-postgres/webpack.config.js @@ -0,0 +1,33 @@ +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, + }, +}; From 3a92fd6a599c7c5c71d97a75d167fe2736658f6d Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Tue, 30 Jul 2024 11:56:37 +0200 Subject: [PATCH 2/2] add routing example Signed-off-by: karthik2804 --- .../routing-requests/README.md | 19 +++++++++++ .../routing-requests/knitwit.json | 14 ++++++++ .../routing-requests/package.json | 23 +++++++++++++ .../routing-requests/spin.toml | 18 ++++++++++ .../routing-requests/src/index.ts | 20 +++++++++++ .../routing-requests/src/spin.ts | 21 ++++++++++++ .../routing-requests/tsconfig.json | 18 ++++++++++ .../routing-requests/webpack.config.js | 33 +++++++++++++++++++ 8 files changed, 166 insertions(+) create mode 100644 examples/common-patterns/routing-requests/README.md create mode 100644 examples/common-patterns/routing-requests/knitwit.json create mode 100644 examples/common-patterns/routing-requests/package.json create mode 100644 examples/common-patterns/routing-requests/spin.toml create mode 100644 examples/common-patterns/routing-requests/src/index.ts create mode 100644 examples/common-patterns/routing-requests/src/spin.ts create mode 100644 examples/common-patterns/routing-requests/tsconfig.json create mode 100644 examples/common-patterns/routing-requests/webpack.config.js diff --git a/examples/common-patterns/routing-requests/README.md b/examples/common-patterns/routing-requests/README.md new file mode 100644 index 00000000..53a6de10 --- /dev/null +++ b/examples/common-patterns/routing-requests/README.md @@ -0,0 +1,19 @@ +# Using Outbound HTTP + +This example showcases making an outbound HTTP request and returning the response. + +## Install Dependencies +Install the necessary npm packages: + +```bash +npm install +``` + +## 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. diff --git a/examples/common-patterns/routing-requests/knitwit.json b/examples/common-patterns/routing-requests/knitwit.json new file mode 100644 index 00000000..0d9f4396 --- /dev/null +++ b/examples/common-patterns/routing-requests/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/common-patterns/routing-requests/package.json b/examples/common-patterns/routing-requests/package.json new file mode 100644 index 00000000..77d6c940 --- /dev/null +++ b/examples/common-patterns/routing-requests/package.json @@ -0,0 +1,23 @@ +{ + "name": "outbound-http", + "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/outbound-http.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/common-patterns/routing-requests/spin.toml b/examples/common-patterns/routing-requests/spin.toml new file mode 100644 index 00000000..08645f37 --- /dev/null +++ b/examples/common-patterns/routing-requests/spin.toml @@ -0,0 +1,18 @@ +spin_manifest_version = 2 + +[application] +authors = ["karthik2804 "] +description = "" +name = "outbound-http" +version = "0.1.0" + +[[trigger.http]] +route = "/..." +component = "outbound-http" + +[component.outbound-http] +source = "target/outbound-http.wasm" +exclude_files = ["**/node_modules"] +[component.outbound-http.build] +command = "npm run build" +watch = ["src/**/*.ts", "package.json"] diff --git a/examples/common-patterns/routing-requests/src/index.ts b/examples/common-patterns/routing-requests/src/index.ts new file mode 100644 index 00000000..afb31b45 --- /dev/null +++ b/examples/common-patterns/routing-requests/src/index.ts @@ -0,0 +1,20 @@ +import { ResponseBuilder, Router } from '@fermyon/spin-sdk'; + +let router = Router(); + +router.get("/", (_, req, res) => { handleDefaultRoute(req, res) }) +router.get("/home/:id", (metadata, req, res) => { handleHomeRoute(req, res, metadata.params.id) }) + +async function handleDefaultRoute(_req: Request, res: ResponseBuilder) { + res.set({ "content-type": "text/plain" }); + res.send("Hello from default route"); +} + +async function handleHomeRoute(_req: Request, res: ResponseBuilder, id: string) { + res.set({ "content-type": "text/plain" }); + res.send(`Hello from home route with id: ${id}`); +} + +export async function handler(req: Request, res: ResponseBuilder) { + await router.handleRequest(req, res); +} diff --git a/examples/common-patterns/routing-requests/src/spin.ts b/examples/common-patterns/routing-requests/src/spin.ts new file mode 100644 index 00000000..9ee47db3 --- /dev/null +++ b/examples/common-patterns/routing-requests/src/spin.ts @@ -0,0 +1,21 @@ +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); +} diff --git a/examples/common-patterns/routing-requests/tsconfig.json b/examples/common-patterns/routing-requests/tsconfig.json new file mode 100644 index 00000000..851fe99f --- /dev/null +++ b/examples/common-patterns/routing-requests/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/common-patterns/routing-requests/webpack.config.js b/examples/common-patterns/routing-requests/webpack.config.js new file mode 100644 index 00000000..3a767ede --- /dev/null +++ b/examples/common-patterns/routing-requests/webpack.config.js @@ -0,0 +1,33 @@ +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, + }, +};