Skip to content

Commit 06dcba7

Browse files
authored
Add support for hono, itty and no router to http-ts and http-js (#356)
* chore: add support for hono,itty,none router to http-ts and http-js templates * chore: unify response payload across all template configs * chore: use new select ui for http router * chore: update GitHub Action to test all routers * chore: use single quotes in all http-* template source files --------- Signed-off-by: Thorsten Hans <[email protected]>
1 parent 99578c9 commit 06dcba7

File tree

9 files changed

+233
-139
lines changed

9 files changed

+233
-139
lines changed

.github/workflows/test.yaml

+58-62
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Test
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88
workflow_dispatch:
99

1010
env:
@@ -15,21 +15,21 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v3
19-
20-
- name: Install Rust toolchain
21-
shell: bash
22-
run: |
23-
rustup toolchain install ${{ env.RUST_VERSION }} --no-self-update
24-
rustup default ${{ env.RUST_VERSION }}
25-
26-
- name: "Install Wasm Rust target"
27-
run: rustup target add wasm32-wasip1 wasm32-unknown-unknown --toolchain ${{ env.RUST_VERSION }}
28-
29-
- name: Use Node.js 22
30-
uses: actions/setup-node@v3
31-
with:
32-
node-version: 22
18+
- uses: actions/checkout@v3
19+
20+
- name: Install Rust toolchain
21+
shell: bash
22+
run: |
23+
rustup toolchain install ${{ env.RUST_VERSION }} --no-self-update
24+
rustup default ${{ env.RUST_VERSION }}
25+
26+
- name: 'Install Wasm Rust target'
27+
run: rustup target add wasm32-wasip1 wasm32-unknown-unknown --toolchain ${{ env.RUST_VERSION }}
28+
29+
- name: Use Node.js 22
30+
uses: actions/setup-node@v3
31+
with:
32+
node-version: 22
3333

3434
# - name: Install dependencies
3535
# shell: bash
@@ -39,55 +39,51 @@ jobs:
3939
# shell: bash
4040
# run: npm run fmt-check
4141

42-
- name: Setup `spin`
43-
uses: fermyon/actions/spin/setup@v1
44-
with:
45-
github_token: ${{ secrets.GITHUB_TOKEN }}
46-
47-
- name: Run Test
48-
shell: bash
49-
run: |
50-
cd test
51-
./test.sh
52-
42+
- name: Setup `spin`
43+
uses: fermyon/actions/spin/setup@v1
44+
with:
45+
github_token: ${{ secrets.GITHUB_TOKEN }}
46+
47+
- name: Run Test
48+
shell: bash
49+
run: |
50+
cd test
51+
./test.sh
52+
5353
test_template:
5454
runs-on: ${{ matrix.os }}
5555
strategy:
5656
matrix:
5757
os: [ubuntu-latest, macos-latest, windows-latest]
58+
router: ['hono', 'itty', 'none']
5859

5960
steps:
60-
- uses: actions/checkout@v3
61-
62-
- name: Use Node.js 22
63-
uses: actions/setup-node@v3
64-
with:
65-
node-version: 22
66-
67-
- name: Setup `spin`
68-
uses: fermyon/actions/spin/setup@v1
69-
with:
70-
github_token: ${{ secrets.GITHUB_TOKEN }}
71-
72-
- name: Install templates
73-
run: spin templates install --dir .
74-
75-
- name: Create new project
76-
run: spin new -t http-ts test-project -a
77-
78-
- name: Install dependencies of the test project
79-
run: |
80-
cd test-project
81-
npm install
82-
83-
- name: Add new component to project
84-
run: |
85-
cd test-project
86-
spin add -t http-ts new-component -a
87-
cd new-component
88-
npm install
89-
90-
- name: Build the application
91-
run: |
92-
cd test-project
93-
spin build
61+
- uses: actions/checkout@v3
62+
63+
- name: Use Node.js 22
64+
uses: actions/setup-node@v3
65+
with:
66+
node-version: 22
67+
68+
- name: Setup `spin`
69+
uses: fermyon/actions/spin/setup@v1
70+
with:
71+
github_token: ${{ secrets.GITHUB_TOKEN }}
72+
73+
- name: Install templates
74+
run: spin templates install --dir .
75+
76+
- name: Create new project
77+
run: spin new -t http-ts --value http-router=${{ matrix.router }} -a test-project
78+
79+
- name: Add new component to project
80+
run: |
81+
cd test-project
82+
spin add -t http-ts --value http-router=${{ matrix.router }} -a new-component
83+
cd new-component
84+
npm install
85+
86+
- name: Build the application
87+
run: |
88+
cd test-project
89+
spin build
+27-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
{
2-
"name": "{{project-name | kebab_case}}",
3-
"version": "1.0.0",
4-
"description": "{{project-description}}",
5-
"main": "index.js",
6-
"scripts": {
7-
"build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.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-
"webpack": "^5.74.0",
16-
"webpack-cli": "^4.10.0"
17-
},
18-
"dependencies": {
19-
"@spinframework/build-tools": "^1.0.1",
20-
"@spinframework/wasi-http-proxy": "^1.0.0",
21-
"itty-router": "^5.0.18"
22-
}
23-
}
2+
"name": "{{project-name | kebab_case}}",
3+
"version": "1.0.0",
4+
"description": "{{project-description}}",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.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+
"webpack": "^5.74.0",
16+
"webpack-cli": "^4.10.0"
17+
},
18+
"dependencies": {
19+
{%- case http-router -%}
20+
{% when "hono" %}
21+
"hono": "^4.7.4",
22+
{% when "itty" %}
23+
"itty-router": "^5.0.18",
24+
{%- endcase %}
25+
"@spinframework/build-tools": "^1.0.1",
26+
"@spinframework/wasi-http-proxy": "^1.0.0"
27+
}
28+
}

templates/http-js/content/src/index.js

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{%- case http-router -%}
2+
{% when "hono" %}
3+
// For Hono documentation refer to https://hono.dev/docs/
4+
import { Hono } from 'hono';
5+
import { logger } from 'hono/logger';
6+
7+
let app = new Hono();
8+
9+
// Logging to stdout via built-in middleware
10+
app.use(logger())
11+
12+
// Example of a custom middleware to set HTTP response header
13+
app.use(async (c, next) => {
14+
c.header('server', 'Spin CLI')
15+
await next();
16+
})
17+
18+
app.get('/', (c) => c.text('Hello, Spin!'));
19+
app.get('/hello/:name', (c) => {
20+
return c.json({ message: `Hello, ${c.req.param('name')}` })
21+
});
22+
23+
app.fire();
24+
{% when "itty" %}
25+
// For AutoRouter documentation refer to https://itty.dev/itty-router/routers/autorouter
26+
import { AutoRouter } from 'itty-router';
27+
28+
let router = AutoRouter();
29+
30+
// Route ordering matters, the first route that matches will be used
31+
// Any route that does not return will be treated as a middleware
32+
// Any unmatched route will return a 404
33+
router
34+
.get('/', () => new Response('Hello, Spin!'))
35+
.get('/hello/:name', ({ name }) => `Hello, ${name}!`)
36+
37+
addEventListener('fetch', (event) => {
38+
event.respondWith(router.fetch(event.request));
39+
});
40+
{% else %}
41+
function handle(_request) {
42+
return new Response('Hello, Spin!', {
43+
status: 200,
44+
headers: {
45+
'content-type': 'text/plain'
46+
}
47+
})
48+
}
49+
50+
addEventListener('fetch', (event) => {
51+
event.respondWith(handle(event.request));
52+
});
53+
{%- endcase %}

templates/http-js/metadata/spin-template.toml

+5
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ component = "component.txt"
1010
[parameters]
1111
project-description = { type = "string", prompt = "Description", default = "" }
1212
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }
13+
http-router = { type = "string", prompt = "HTTP Router", default = "itty", allowed_values = [
14+
"itty",
15+
"hono",
16+
"none",
17+
] }
+29-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
{
2-
"name": "{{project-name | kebab_case}}",
3-
"version": "1.0.0",
4-
"description": "{{project-description}}",
5-
"main": "index.js",
6-
"scripts": {
7-
"build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.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-
},
20-
"dependencies": {
21-
"@spinframework/build-tools": "^1.0.1",
22-
"@spinframework/wasi-http-proxy": "^1.0.0",
23-
"itty-router": "^5.0.18"
24-
}
25-
}
2+
"name": "{{project-name | kebab_case}}",
3+
"version": "1.0.0",
4+
"description": "{{project-description}}",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.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+
},
20+
"dependencies": {
21+
{%- case http-router -%}
22+
{% when "hono" %}
23+
"hono": "^4.7.4",
24+
{% when "itty" %}
25+
"itty-router": "^5.0.18",
26+
{%- endcase %}
27+
"@spinframework/build-tools": "^1.0.1",
28+
"@spinframework/wasi-http-proxy": "^1.0.0"
29+
}
30+
}

templates/http-ts/content/src/index.ts

-16
This file was deleted.

0 commit comments

Comments
 (0)