Skip to content

Commit 2b8e9db

Browse files
committed
update common pattern examples
Signed-off-by: karthik2804 <[email protected]>
1 parent e181b8e commit 2b8e9db

File tree

20 files changed

+7541
-192
lines changed

20 files changed

+7541
-192
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
dist
3+
target
4+
.spin/
5+
dist.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

examples/common-patterns/outbound-http/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
This example showcases making an outbound HTTP request and returning the response.
44

5-
## Install Dependencies
6-
Install the necessary npm packages:
5+
## Configuring `allowed_outbound_hosts` in `spin.toml`
76

8-
```bash
9-
npm install
10-
```
7+
The `allowed_outbound_hosts` needs to include the address of the host to which the guest tried to call. In the case of this example, the host is `https://random-data-api.fermyon.app/` which is added in the `spin.toml`
118

129
## Building and Running the Example
1310

examples/common-patterns/outbound-http/package-lock.json

Lines changed: 3688 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
2-
"name": "outbound-http",
3-
"version": "1.0.0",
4-
"description": "",
5-
"main": "index.js",
6-
"scripts": {
7-
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/outbound-http.wasm",
8-
"test": "echo \"Error: no test specified\" && exit 1",
9-
"postinstall": "knitwit"
10-
},
11-
"keywords": [],
12-
"author": "",
13-
"license": "ISC",
14-
"devDependencies": {
15-
"mkdirp": "^3.0.1",
16-
"ts-loader": "^9.4.1",
17-
"typescript": "^4.8.4",
18-
"webpack": "^5.74.0",
19-
"webpack-cli": "^4.10.0"
20-
},
21-
"dependencies": {
22-
"@fermyon/spin-sdk": "^2.3.0"
23-
}
24-
}
25-
2+
"name": "outbound-http",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "knitwit --out-dir build/wit/knitwit --out-world combined && npx webpack --mode=production && npx mkdirp dist && npx j2w -i build/bundle.js -d build/wit/knitwit -n combined -o dist/outbound-http.wasm",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"mkdirp": "^3.0.1",
15+
"ts-loader": "^9.4.1",
16+
"typescript": "^4.8.4",
17+
"webpack": "^5.74.0",
18+
"webpack-cli": "^4.10.0",
19+
"@fermyon/knitwit": "0.3.0"
20+
},
21+
"dependencies": {
22+
"@fermyon/spin-sdk": "^3.0.0-rc1",
23+
"itty-router": "^5.0.18"
24+
}
25+
}

examples/common-patterns/outbound-http/spin.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ route = "/..."
1111
component = "outbound-http"
1212

1313
[component.outbound-http]
14-
source = "target/outbound-http.wasm"
14+
source = "dist/outbound-http.wasm"
1515
exclude_files = ["**/node_modules"]
16-
allowed_outbound_hosts = ["*://*:*"]
16+
allowed_outbound_hosts = ["https://random-data-api.fermyon.app/"]
1717
[component.outbound-http.build]
18-
command = "npm run build"
19-
watch = ["src/**/*.ts", "package.json"]
18+
command = ["npm install", "npm run build"]
19+
watch = ["src/**/*.ts"]
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
import { ResponseBuilder } from '@fermyon/spin-sdk';
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
23

3-
export async function handler(_req: Request, res: ResponseBuilder) {
4-
let response = await fetch(
5-
'https://random-data-api.fermyon.app/physics/json',
6-
);
7-
let physicsFact = await response.text();
4+
let router = AutoRouter();
85

9-
res.send(physicsFact);
6+
// Route ordering matters, the first route that matches will be used
7+
// Any route that does not return will be treated as a middleware
8+
// Any unmatched route will return a 404
9+
router
10+
.get("*", getDataFromAPI)
11+
12+
async function getDataFromAPI(_request: Request) {
13+
let response = await fetch(
14+
'https://random-data-api.fermyon.app/physics/json',
15+
);
16+
return response;
1017
}
18+
19+
//@ts-ignore
20+
addEventListener('fetch', async (event: FetchEvent) => {
21+
event.respondWith(router.fetch(event.request));
22+
});

examples/common-patterns/outbound-http/src/spin.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
const path = require('path');
2-
const SpinSdkPlugin = require('@fermyon/spin-sdk/plugins/webpack');
2+
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")
33

44
module.exports = {
5-
entry: './src/spin.ts',
6-
experiments: {
7-
outputModule: true,
8-
},
9-
module: {
10-
rules: [
11-
{
12-
test: /\.tsx?$/,
13-
use: 'ts-loader',
14-
exclude: /node_modules/,
15-
},
5+
entry: './src/index.ts',
6+
experiments: {
7+
outputModule: true,
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.tsx?$/,
13+
use: 'ts-loader',
14+
exclude: /node_modules/,
15+
},
16+
],
17+
},
18+
resolve: {
19+
extensions: ['.tsx', '.ts', '.js'],
20+
},
21+
output: {
22+
path: path.resolve(__dirname, './build'),
23+
filename: 'bundle.js',
24+
module: true,
25+
library: {
26+
type: "module",
27+
}
28+
},
29+
plugins: [
30+
new SpinSdkPlugin()
1631
],
17-
},
18-
resolve: {
19-
extensions: ['.tsx', '.ts', '.js'],
20-
},
21-
output: {
22-
path: path.resolve(__dirname, './'),
23-
filename: 'dist.js',
24-
module: true,
25-
library: {
26-
type: 'module',
32+
optimization: {
33+
minimize: false
2734
},
28-
},
29-
plugins: [new SpinSdkPlugin()],
30-
optimization: {
31-
minimize: false,
32-
},
33-
};
35+
};

0 commit comments

Comments
 (0)