Skip to content

Commit d951c50

Browse files
committed
update spin host api examples
Signed-off-by: karthik2804 <[email protected]>
1 parent ca255f3 commit d951c50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+634
-701
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+
build/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

examples/spin-host-apis/spin-kv/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ This is a simple example showcasing using Spin key-value storage with TypeScript
77
To build the app run the following commands:
88

99
```bash
10-
$ npm install
1110
$ spin build --up
1211
```
1312

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"version": 1,
3+
"project": {
4+
"worlds": [
5+
"spin-http"
6+
]
7+
},
8+
"packages": {}
9+
}

examples/spin-host-apis/spin-kv/knitwit.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/spin-host-apis/spin-kv/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/spin-kv.wasm",
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/spin-kv.wasm",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],
@@ -15,9 +15,11 @@
1515
"ts-loader": "^9.4.1",
1616
"typescript": "^4.8.4",
1717
"webpack": "^5.74.0",
18-
"webpack-cli": "^4.10.0"
18+
"webpack-cli": "^4.10.0",
19+
"@fermyon/knitwit": "0.3.0"
1920
},
2021
"dependencies": {
21-
"@fermyon/spin-sdk": "^2.0.0"
22+
"@fermyon/spin-sdk": "^3.0.0-rc1",
23+
"itty-router": "^5.0.18"
2224
}
2325
}

examples/spin-host-apis/spin-kv/spin.toml

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

1313
[component.spin-kv]
14-
source = "target/spin-kv.wasm"
14+
source = "dist/spin-kv.wasm"
1515
exclude_files = ["**/node_modules"]
1616
key_value_stores = ["default"]
1717
[component.spin-kv.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: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1-
import { Kv, ResponseBuilder } from '@fermyon/spin-sdk';
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
3+
import { Kv } from '@fermyon/spin-sdk';
24

35
const decoder = new TextDecoder();
46

5-
export async function handler(req: Request, res: ResponseBuilder) {
6-
let store = Kv.openDefault();
7-
let status = 200;
8-
let body;
7+
let router = AutoRouter();
98

10-
switch (req.method) {
11-
case 'POST':
12-
store.set(req.url, (await req.text()) || new Uint8Array().buffer);
13-
break;
14-
case 'GET':
15-
let val;
16-
val = store.get(req.url);
17-
if (!val) {
18-
status = 404;
19-
} else {
20-
body = decoder.decode(val);
21-
}
22-
break;
23-
case 'DELETE':
24-
store.delete(req.url);
25-
break;
26-
case 'HEAD':
27-
if (!store.exists(req.url)) {
28-
status = 404;
29-
}
30-
break;
31-
default:
32-
}
9+
// Route ordering matters, the first route that matches will be used
10+
// Any route that does not return will be treated as a middleware
11+
// Any unmatched route will return a 404
12+
router
13+
.get("*", async (req: Request) => {
14+
let store = Kv.openDefault();
15+
let status = 200;
16+
let body;
3317

34-
res.status(status);
35-
res.send(body);
36-
}
18+
switch (req.method) {
19+
case 'POST':
20+
store.set(req.url, (await req.text()) || new Uint8Array().buffer);
21+
break;
22+
case 'GET':
23+
let val;
24+
val = store.get(req.url);
25+
if (!val) {
26+
status = 404;
27+
} else {
28+
body = decoder.decode(val);
29+
}
30+
break;
31+
case 'DELETE':
32+
store.delete(req.url);
33+
break;
34+
case 'HEAD':
35+
if (!store.exists(req.url)) {
36+
status = 404;
37+
}
38+
break;
39+
default:
40+
}
41+
42+
return new Response(body, { status });
43+
})
44+
45+
//@ts-ignore
46+
addEventListener('fetch', async (event: FetchEvent) => {
47+
event.respondWith(router.fetch(event.request));
48+
});

examples/spin-host-apis/spin-kv/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)