diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8f00562c..2141eafd 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -2,9 +2,9 @@ name: Test on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] workflow_dispatch: env: @@ -15,21 +15,21 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - - name: Install Rust toolchain - shell: bash - run: | - rustup toolchain install ${{ env.RUST_VERSION }} --no-self-update - rustup default ${{ env.RUST_VERSION }} - - - name: "Install Wasm Rust target" - run: rustup target add wasm32-wasip1 wasm32-unknown-unknown --toolchain ${{ env.RUST_VERSION }} - - - name: Use Node.js 22 - uses: actions/setup-node@v3 - with: - node-version: 22 + - uses: actions/checkout@v3 + + - name: Install Rust toolchain + shell: bash + run: | + rustup toolchain install ${{ env.RUST_VERSION }} --no-self-update + rustup default ${{ env.RUST_VERSION }} + + - name: 'Install Wasm Rust target' + run: rustup target add wasm32-wasip1 wasm32-unknown-unknown --toolchain ${{ env.RUST_VERSION }} + + - name: Use Node.js 22 + uses: actions/setup-node@v3 + with: + node-version: 22 # - name: Install dependencies # shell: bash @@ -39,55 +39,51 @@ jobs: # shell: bash # run: npm run fmt-check - - name: Setup `spin` - uses: fermyon/actions/spin/setup@v1 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - - - name: Run Test - shell: bash - run: | - cd test - ./test.sh - + - name: Setup `spin` + uses: fermyon/actions/spin/setup@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + + - name: Run Test + shell: bash + run: | + cd test + ./test.sh + test_template: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] + router: ['hono', 'itty', 'none'] steps: - - uses: actions/checkout@v3 - - - name: Use Node.js 22 - uses: actions/setup-node@v3 - with: - node-version: 22 - - - name: Setup `spin` - uses: fermyon/actions/spin/setup@v1 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - - - name: Install templates - run: spin templates install --dir . - - - name: Create new project - run: spin new -t http-ts test-project -a - - - name: Install dependencies of the test project - run: | - cd test-project - npm install - - - name: Add new component to project - run: | - cd test-project - spin add -t http-ts new-component -a - cd new-component - npm install - - - name: Build the application - run: | - cd test-project - spin build \ No newline at end of file + - uses: actions/checkout@v3 + + - name: Use Node.js 22 + uses: actions/setup-node@v3 + with: + node-version: 22 + + - name: Setup `spin` + uses: fermyon/actions/spin/setup@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install templates + run: spin templates install --dir . + + - name: Create new project + run: spin new -t http-ts --value http-router=${{ matrix.router }} -a test-project + + - name: Add new component to project + run: | + cd test-project + spin add -t http-ts --value http-router=${{ matrix.router }} -a new-component + cd new-component + npm install + + - name: Build the application + run: | + cd test-project + spin build diff --git a/templates/http-js/content/package.json b/templates/http-js/content/package.json index be018a35..6bb8f845 100644 --- a/templates/http-js/content/package.json +++ b/templates/http-js/content/package.json @@ -1,23 +1,28 @@ { - "name": "{{project-name | kebab_case}}", - "version": "1.0.0", - "description": "{{project-description}}", - "main": "index.js", - "scripts": { - "build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.wasm", - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC", - "devDependencies": { - "mkdirp": "^3.0.1", - "webpack": "^5.74.0", - "webpack-cli": "^4.10.0" - }, - "dependencies": { - "@spinframework/build-tools": "^1.0.1", - "@spinframework/wasi-http-proxy": "^1.0.0", - "itty-router": "^5.0.18" - } -} \ No newline at end of file + "name": "{{project-name | kebab_case}}", + "version": "1.0.0", + "description": "{{project-description}}", + "main": "index.js", + "scripts": { + "build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.wasm", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "mkdirp": "^3.0.1", + "webpack": "^5.74.0", + "webpack-cli": "^4.10.0" + }, + "dependencies": { + {%- case http-router -%} + {% when "hono" %} + "hono": "^4.7.4", + {% when "itty" %} + "itty-router": "^5.0.18", + {%- endcase %} + "@spinframework/build-tools": "^1.0.1", + "@spinframework/wasi-http-proxy": "^1.0.0" + } +} diff --git a/templates/http-js/content/src/index.js b/templates/http-js/content/src/index.js deleted file mode 100644 index 23e6369e..00000000 --- a/templates/http-js/content/src/index.js +++ /dev/null @@ -1,15 +0,0 @@ -// For AutoRouter documentation refer to https://itty.dev/itty-router/routers/autorouter -import { AutoRouter } from 'itty-router'; - -let router = AutoRouter(); - -// Route ordering matters, the first route that matches will be used -// Any route that does not return will be treated as a middleware -// Any unmatched route will return a 404 -router - .get("/", () => new Response("hello universe")) - .get("/hello/:name", ({ name }) => `Hello, ${name}!`) - -addEventListener('fetch', (event) => { - event.respondWith(router.fetch(event.request)); -}); \ No newline at end of file diff --git a/templates/http-js/content/src/index.js.tmpl b/templates/http-js/content/src/index.js.tmpl new file mode 100644 index 00000000..9669c87b --- /dev/null +++ b/templates/http-js/content/src/index.js.tmpl @@ -0,0 +1,53 @@ +{%- case http-router -%} +{% when "hono" %} +// For Hono documentation refer to https://hono.dev/docs/ +import { Hono } from 'hono'; +import { logger } from 'hono/logger'; + +let app = new Hono(); + +// Logging to stdout via built-in middleware +app.use(logger()) + +// Example of a custom middleware to set HTTP response header +app.use(async (c, next) => { + c.header('server', 'Spin CLI') + await next(); +}) + +app.get('/', (c) => c.text('Hello, Spin!')); +app.get('/hello/:name', (c) => { + return c.json({ message: `Hello, ${c.req.param('name')}` }) +}); + +app.fire(); +{% when "itty" %} +// For AutoRouter documentation refer to https://itty.dev/itty-router/routers/autorouter +import { AutoRouter } from 'itty-router'; + +let router = AutoRouter(); + +// Route ordering matters, the first route that matches will be used +// Any route that does not return will be treated as a middleware +// Any unmatched route will return a 404 +router + .get('/', () => new Response('Hello, Spin!')) + .get('/hello/:name', ({ name }) => `Hello, ${name}!`) + +addEventListener('fetch', (event) => { + event.respondWith(router.fetch(event.request)); +}); +{% else %} +function handle(_request) { + return new Response('Hello, Spin!', { + status: 200, + headers: { + 'content-type': 'text/plain' + } + }) +} + +addEventListener('fetch', (event) => { + event.respondWith(handle(event.request)); +}); +{%- endcase %} diff --git a/templates/http-js/metadata/spin-template.toml b/templates/http-js/metadata/spin-template.toml index a138db92..1ba50b62 100644 --- a/templates/http-js/metadata/spin-template.toml +++ b/templates/http-js/metadata/spin-template.toml @@ -10,3 +10,8 @@ component = "component.txt" [parameters] project-description = { type = "string", prompt = "Description", default = "" } http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } +http-router = { type = "string", prompt = "HTTP Router", default = "itty", allowed_values = [ + "itty", + "hono", + "none", +] } diff --git a/templates/http-ts/content/package.json b/templates/http-ts/content/package.json index d703050c..45f26a7b 100644 --- a/templates/http-ts/content/package.json +++ b/templates/http-ts/content/package.json @@ -1,25 +1,30 @@ { - "name": "{{project-name | kebab_case}}", - "version": "1.0.0", - "description": "{{project-description}}", - "main": "index.js", - "scripts": { - "build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.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": { - "@spinframework/build-tools": "^1.0.1", - "@spinframework/wasi-http-proxy": "^1.0.0", - "itty-router": "^5.0.18" - } -} \ No newline at end of file + "name": "{{project-name | kebab_case}}", + "version": "1.0.0", + "description": "{{project-description}}", + "main": "index.js", + "scripts": { + "build": "npx webpack && mkdirp dist && j2w -i build/bundle.js -o dist/{{ project-name | kebab_case }}.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": { + {%- case http-router -%} + {% when "hono" %} + "hono": "^4.7.4", + {% when "itty" %} + "itty-router": "^5.0.18", + {%- endcase %} + "@spinframework/build-tools": "^1.0.1", + "@spinframework/wasi-http-proxy": "^1.0.0" + } +} diff --git a/templates/http-ts/content/src/index.ts b/templates/http-ts/content/src/index.ts deleted file mode 100644 index a6870e9a..00000000 --- a/templates/http-ts/content/src/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -// For AutoRouter documentation refer to https://itty.dev/itty-router/routers/autorouter -import { AutoRouter } from 'itty-router'; - -let router = AutoRouter(); - -// Route ordering matters, the first route that matches will be used -// Any route that does not return will be treated as a middleware -// Any unmatched route will return a 404 -router - .get("/", () => new Response("hello universe")) - .get("/hello/:name", ({ name }) => `Hello, ${name}!`) - -//@ts-ignore -addEventListener('fetch', (event: FetchEvent) => { - event.respondWith(router.fetch(event.request)); -}); diff --git a/templates/http-ts/content/src/index.ts.tmpl b/templates/http-ts/content/src/index.ts.tmpl new file mode 100644 index 00000000..bb3a9443 --- /dev/null +++ b/templates/http-ts/content/src/index.ts.tmpl @@ -0,0 +1,56 @@ +{%- case http-router -%} +{% when "hono" %} +// For Hono documentation refer to https://hono.dev/docs/ +import { Hono } from 'hono'; +import type { Context, Next } from 'hono' +import { logger } from 'hono/logger'; + +let app = new Hono(); + +// Logging to stdout via built-in middleware +app.use(logger()) + +// Example of a custom middleware to set HTTP response header +app.use(async (c: Context, next: Next) => { + c.header('server', 'Spin CLI') + await next(); +}) + +app.get('/', (c: Context) => c.text('Hello, Spin!')); +app.get('/hello/:name', (c: Context) => { + return c.json({ message: `Hello, ${c.req.param('name')}!` }) +}); + +app.fire(); +{% when "itty" %} +// For AutoRouter documentation refer to https://itty.dev/itty-router/routers/autorouter +import { AutoRouter } from 'itty-router'; + +let router = AutoRouter(); + +// Route ordering matters, the first route that matches will be used +// Any route that does not return will be treated as a middleware +// Any unmatched route will return a 404 +router + .get('/', () => new Response('Hello, Spin!')) + .get('/hello/:name', ({ name }) => `Hello, ${name}!`) + +//@ts-ignore +addEventListener('fetch', (event: FetchEvent) => { + event.respondWith(router.fetch(event.request)); +}); +{% else %} +function handle(_request: Request): Response { + return new Response('Hello, Spin!', { + status: 200, + headers: { + 'content-type': 'text/plain' + } + }) +} + +//@ts-ignore +addEventListener('fetch', (event: FetchEvent) => { + event.respondWith(handle(event.request)); +}); +{%- endcase %} diff --git a/templates/http-ts/metadata/spin-template.toml b/templates/http-ts/metadata/spin-template.toml index c468b052..0d4e8bad 100644 --- a/templates/http-ts/metadata/spin-template.toml +++ b/templates/http-ts/metadata/spin-template.toml @@ -10,3 +10,8 @@ component = "component.txt" [parameters] project-description = { type = "string", prompt = "Description", default = "" } http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } +http-router = { type = "string", prompt = "HTTP Router", default = "itty", allowed_values = [ + "itty", + "hono", + "none", +] }