-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from karthik2804/spin_host_api_examples
- Loading branch information
Showing
40 changed files
with
842 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,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. |
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,14 @@ | ||
{ | ||
"version": 1, | ||
"project": { | ||
"worlds": [ | ||
"spin-http" | ||
] | ||
}, | ||
"packages": { | ||
"@fermyon/spin-sdk": { | ||
"witPath": "../../bin/wit", | ||
"world": "spin-imports" | ||
} | ||
} | ||
} |
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,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" | ||
} | ||
} |
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 @@ | ||
spin_manifest_version = 2 | ||
|
||
[application] | ||
authors = ["karthik2804 <[email protected]>"] | ||
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"] |
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,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); | ||
} |
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,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); | ||
} |
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,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" | ||
} | ||
} |
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,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, | ||
}, | ||
}; |
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 @@ | ||
# 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. |
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,14 @@ | ||
{ | ||
"version": 1, | ||
"project": { | ||
"worlds": [ | ||
"spin-http" | ||
] | ||
}, | ||
"packages": { | ||
"@fermyon/spin-sdk": { | ||
"witPath": "../../bin/wit", | ||
"world": "spin-imports" | ||
} | ||
} | ||
} |
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,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" | ||
} | ||
} |
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,18 @@ | ||
spin_manifest_version = 2 | ||
|
||
[application] | ||
authors = ["karthik2804 <[email protected]>"] | ||
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"] |
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 @@ | ||
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); | ||
} |
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,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); | ||
} |
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,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" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/common-patterns/routing-requests/webpack.config.js
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,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, | ||
}, | ||
}; |
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,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: "<mqtt-broker-address>", | ||
username: "<mqtt-broker-username>", | ||
password: "<mqtt-broker-password>", | ||
KeepAlive: 60, | ||
} | ||
|
||
const topic = "<mqtt-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. |
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,11 @@ | ||
{ | ||
"version": 1, | ||
"project": { | ||
"worlds": [ | ||
"spin-http" | ||
] | ||
}, | ||
"packages": { | ||
"@fermyon/spin-sdk": {} | ||
} | ||
} |
Oops, something went wrong.