From e46342ab45f75235bded87339376b224dc5d6025 Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Wed, 19 Mar 2025 08:51:25 +0100 Subject: [PATCH 1/9] feat: Add http-hono-ts template Signed-off-by: Thorsten Hans --- templates/http-hono-ts/content/.gitignore | 5 +++ templates/http-hono-ts/content/.npmrc | 1 + .../http-hono-ts/content/config/knitwit.json | 9 +++++ templates/http-hono-ts/content/package.json | 25 ++++++++++++ templates/http-hono-ts/content/spin.toml | 18 +++++++++ templates/http-hono-ts/content/src/index.ts | 22 +++++++++++ templates/http-hono-ts/content/tsconfig.json | 21 ++++++++++ .../http-hono-ts/content/webpack.config.js | 38 +++++++++++++++++++ .../metadata/snippets/component.txt | 11 ++++++ .../http-hono-ts/metadata/spin-template.toml | 12 ++++++ 10 files changed, 162 insertions(+) create mode 100644 templates/http-hono-ts/content/.gitignore create mode 100644 templates/http-hono-ts/content/.npmrc create mode 100644 templates/http-hono-ts/content/config/knitwit.json create mode 100644 templates/http-hono-ts/content/package.json create mode 100644 templates/http-hono-ts/content/spin.toml create mode 100644 templates/http-hono-ts/content/src/index.ts create mode 100644 templates/http-hono-ts/content/tsconfig.json create mode 100644 templates/http-hono-ts/content/webpack.config.js create mode 100644 templates/http-hono-ts/metadata/snippets/component.txt create mode 100644 templates/http-hono-ts/metadata/spin-template.toml diff --git a/templates/http-hono-ts/content/.gitignore b/templates/http-hono-ts/content/.gitignore new file mode 100644 index 00000000..758762fd --- /dev/null +++ b/templates/http-hono-ts/content/.gitignore @@ -0,0 +1,5 @@ +node_modules +dist +target +.spin/ +build/ \ No newline at end of file diff --git a/templates/http-hono-ts/content/.npmrc b/templates/http-hono-ts/content/.npmrc new file mode 100644 index 00000000..408607a3 --- /dev/null +++ b/templates/http-hono-ts/content/.npmrc @@ -0,0 +1 @@ +KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/http-hono-ts/content/config/knitwit.json b/templates/http-hono-ts/content/config/knitwit.json new file mode 100644 index 00000000..a9375762 --- /dev/null +++ b/templates/http-hono-ts/content/config/knitwit.json @@ -0,0 +1,9 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-http" + ] + }, + "packages": {} +} \ No newline at end of file diff --git a/templates/http-hono-ts/content/package.json b/templates/http-hono-ts/content/package.json new file mode 100644 index 00000000..c375a41c --- /dev/null +++ b/templates/http-hono-ts/content/package.json @@ -0,0 +1,25 @@ +{ + "name": "{{project-name | kebab_case}}", + "version": "1.0.0", + "description": "{{project-description}}", + "main": "index.js", + "scripts": { + "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/{{ 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", + "@fermyon/knitwit": "0.3.0" + }, + "dependencies": { + "@fermyon/spin-sdk": "^3.0.0", + "hono": "^4.7.4" + } +} diff --git a/templates/http-hono-ts/content/spin.toml b/templates/http-hono-ts/content/spin.toml new file mode 100644 index 00000000..82c26af2 --- /dev/null +++ b/templates/http-hono-ts/content/spin.toml @@ -0,0 +1,18 @@ +spin_manifest_version = 2 + +[application] +authors = ["{{authors}}"] +description = "{{project-description}}" +name = "{{project-name}}" +version = "0.1.0" + +[[trigger.http]] +route = "{{http-path}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "dist/{{project-name | kebab_case}}.wasm" +exclude_files = ["**/node_modules"] +[component.{{project-name | kebab_case}}.build] +command = ["npm install", "npm run build"] +watch = ["src/**/*.ts"] \ No newline at end of file diff --git a/templates/http-hono-ts/content/src/index.ts b/templates/http-hono-ts/content/src/index.ts new file mode 100644 index 00000000..583af7ba --- /dev/null +++ b/templates/http-hono-ts/content/src/index.ts @@ -0,0 +1,22 @@ +// 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('/:name', (c: Context) => { + return c.json({ message: `Hello, ${c.req.param('name')}` }) +}); + +app.fire(); \ No newline at end of file diff --git a/templates/http-hono-ts/content/tsconfig.json b/templates/http-hono-ts/content/tsconfig.json new file mode 100644 index 00000000..bcbd6f7f --- /dev/null +++ b/templates/http-hono-ts/content/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "outDir": "./dist/", + "noImplicitAny": true, + "module": "es6", + "target": "es2020", + "jsx": "react", + "skipLibCheck": true, + "lib": [ + "ES2020", + "WebWorker" + ], + "allowJs": true, + "strict": true, + "noImplicitReturns": true, + "moduleResolution": "node" + }, + "include": [ + "src/**/*" + ] +} \ No newline at end of file diff --git a/templates/http-hono-ts/content/webpack.config.js b/templates/http-hono-ts/content/webpack.config.js new file mode 100644 index 00000000..dac0a25f --- /dev/null +++ b/templates/http-hono-ts/content/webpack.config.js @@ -0,0 +1,38 @@ +const path = require('path'); +const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") + +module.exports = { + entry: './src/index.ts', + experiments: { + outputModule: true, + }, + module: { + rules: [ + { + test: /\.tsx?$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: ['.tsx', '.ts', '.js'], + }, + output: { + path: path.resolve(__dirname, './build'), + filename: 'bundle.js', + module: true, + library: { + type: "module", + } + }, + plugins: [ + new SpinSdkPlugin() + ], + optimization: { + minimize: false + }, + performance: { + hints: false, + } +}; diff --git a/templates/http-hono-ts/metadata/snippets/component.txt b/templates/http-hono-ts/metadata/snippets/component.txt new file mode 100644 index 00000000..b8184acb --- /dev/null +++ b/templates/http-hono-ts/metadata/snippets/component.txt @@ -0,0 +1,11 @@ +[[trigger.http]] +route = "{{http-path}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" +allowed_outbound_hosts = [] + +[component.{{project-name | kebab_case}}.build] +command = "npm install && npm run build" +workdir = "{{ output-path }}" \ No newline at end of file diff --git a/templates/http-hono-ts/metadata/spin-template.toml b/templates/http-hono-ts/metadata/spin-template.toml new file mode 100644 index 00000000..7178dae7 --- /dev/null +++ b/templates/http-hono-ts/metadata/spin-template.toml @@ -0,0 +1,12 @@ +manifest_version = "1" +id = "http-hono-ts" +description = "HTTP request handler using TypeScript and Hono" + +[add_component] +skip_files = ["spin.toml"] +[add_component.snippets] +component = "component.txt" + +[parameters] +project-description = { type = "string", prompt = "Description", default = "" } +http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } From 2ed9b5a9071786809b005c20a8192ec309a38df8 Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Wed, 19 Mar 2025 09:19:14 +0100 Subject: [PATCH 2/9] feat: Add http-hono-js template Signed-off-by: Thorsten Hans --- templates/http-hono-js/content/.gitignore | 5 ++++ templates/http-hono-js/content/.npmrc | 1 + .../http-hono-js/content/config/knitwit.json | 9 +++++++ templates/http-hono-js/content/package.json | 23 ++++++++++++++++ templates/http-hono-js/content/spin.toml | 18 +++++++++++++ templates/http-hono-js/content/src/index.js | 21 +++++++++++++++ .../http-hono-js/content/webpack.config.js | 26 +++++++++++++++++++ .../metadata/snippets/component.txt | 11 ++++++++ .../http-hono-js/metadata/spin-template.toml | 12 +++++++++ 9 files changed, 126 insertions(+) create mode 100644 templates/http-hono-js/content/.gitignore create mode 100644 templates/http-hono-js/content/.npmrc create mode 100644 templates/http-hono-js/content/config/knitwit.json create mode 100644 templates/http-hono-js/content/package.json create mode 100644 templates/http-hono-js/content/spin.toml create mode 100644 templates/http-hono-js/content/src/index.js create mode 100644 templates/http-hono-js/content/webpack.config.js create mode 100644 templates/http-hono-js/metadata/snippets/component.txt create mode 100644 templates/http-hono-js/metadata/spin-template.toml diff --git a/templates/http-hono-js/content/.gitignore b/templates/http-hono-js/content/.gitignore new file mode 100644 index 00000000..758762fd --- /dev/null +++ b/templates/http-hono-js/content/.gitignore @@ -0,0 +1,5 @@ +node_modules +dist +target +.spin/ +build/ \ No newline at end of file diff --git a/templates/http-hono-js/content/.npmrc b/templates/http-hono-js/content/.npmrc new file mode 100644 index 00000000..408607a3 --- /dev/null +++ b/templates/http-hono-js/content/.npmrc @@ -0,0 +1 @@ +KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/http-hono-js/content/config/knitwit.json b/templates/http-hono-js/content/config/knitwit.json new file mode 100644 index 00000000..a9375762 --- /dev/null +++ b/templates/http-hono-js/content/config/knitwit.json @@ -0,0 +1,9 @@ +{ + "version": 1, + "project": { + "worlds": [ + "spin-http" + ] + }, + "packages": {} +} \ No newline at end of file diff --git a/templates/http-hono-js/content/package.json b/templates/http-hono-js/content/package.json new file mode 100644 index 00000000..d6e77802 --- /dev/null +++ b/templates/http-hono-js/content/package.json @@ -0,0 +1,23 @@ +{ + "name": "{{project-name | kebab_case}}", + "version": "1.0.0", + "description": "{{project-description}}", + "main": "index.js", + "scripts": { + "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/{{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", + "@fermyon/knitwit": "0.3.0" + }, + "dependencies": { + "@fermyon/spin-sdk": "^3.0.0", + "hono": "^4.7.4" + } +} diff --git a/templates/http-hono-js/content/spin.toml b/templates/http-hono-js/content/spin.toml new file mode 100644 index 00000000..038370f3 --- /dev/null +++ b/templates/http-hono-js/content/spin.toml @@ -0,0 +1,18 @@ +spin_manifest_version = 2 + +[application] +authors = ["{{authors}}"] +description = "{{project-description}}" +name = "{{project-name}}" +version = "0.1.0" + +[[trigger.http]] +route = "{{http-path}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "dist/{{project-name | kebab_case}}.wasm" +exclude_files = ["**/node_modules"] +[component.{{project-name | kebab_case}}.build] +command = ["npm install", "npm run build"] +watch = ["src/**/*.js"] \ No newline at end of file diff --git a/templates/http-hono-js/content/src/index.js b/templates/http-hono-js/content/src/index.js new file mode 100644 index 00000000..0f8ede02 --- /dev/null +++ b/templates/http-hono-js/content/src/index.js @@ -0,0 +1,21 @@ +// 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('/:name', (c) => { + return c.json({ message: `Hello, ${c.req.param('name')}` }) +}); + +app.fire(); \ No newline at end of file diff --git a/templates/http-hono-js/content/webpack.config.js b/templates/http-hono-js/content/webpack.config.js new file mode 100644 index 00000000..70ed621d --- /dev/null +++ b/templates/http-hono-js/content/webpack.config.js @@ -0,0 +1,26 @@ +const path = require('path'); +const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") + +module.exports = { + entry: './src/index.js', + experiments: { + outputModule: true, + }, + output: { + path: path.resolve(__dirname, './build'), + filename: 'bundle.js', + module: true, + library: { + type: "module", + } + }, + plugins: [ + new SpinSdkPlugin() + ], + optimization: { + minimize: false + }, + performance: { + hints: false, + } +}; diff --git a/templates/http-hono-js/metadata/snippets/component.txt b/templates/http-hono-js/metadata/snippets/component.txt new file mode 100644 index 00000000..1e5388ae --- /dev/null +++ b/templates/http-hono-js/metadata/snippets/component.txt @@ -0,0 +1,11 @@ +[[trigger.http]] +route = "{{http-path}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" +allowed_outbound_hosts = [] + +[component.{{project-name | kebab_case}}.build] +command = "npm install && npm run build" +workdir = "{{ output-path }}" diff --git a/templates/http-hono-js/metadata/spin-template.toml b/templates/http-hono-js/metadata/spin-template.toml new file mode 100644 index 00000000..e72c4c52 --- /dev/null +++ b/templates/http-hono-js/metadata/spin-template.toml @@ -0,0 +1,12 @@ +manifest_version = "1" +id = "http-hono-js" +description = "HTTP request handler using JavaScript and Hono" + +[add_component] +skip_files = ["spin.toml"] +[add_component.snippets] +component = "component.txt" + +[parameters] +project-description = { type = "string", prompt = "Description", default = "" } +http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } From bb62fb7b7bd2dbb9ff92339e8d371d2b471b212d Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Thu, 20 Mar 2025 07:47:01 +0100 Subject: [PATCH 3/9] chore: rename templates to http-{lang}-hono Signed-off-by: Thorsten Hans --- templates/{http-hono-js => http-js-hono}/content/.gitignore | 0 templates/{http-hono-js => http-js-hono}/content/.npmrc | 0 .../{http-hono-js => http-js-hono}/content/config/knitwit.json | 0 templates/{http-hono-js => http-js-hono}/content/package.json | 0 templates/{http-hono-js => http-js-hono}/content/spin.toml | 0 templates/{http-hono-js => http-js-hono}/content/src/index.js | 0 .../{http-hono-js => http-js-hono}/content/webpack.config.js | 0 .../metadata/snippets/component.txt | 0 .../{http-hono-js => http-js-hono}/metadata/spin-template.toml | 2 +- templates/{http-hono-ts => http-ts-hono}/content/.gitignore | 0 templates/{http-hono-ts => http-ts-hono}/content/.npmrc | 0 .../{http-hono-ts => http-ts-hono}/content/config/knitwit.json | 0 templates/{http-hono-ts => http-ts-hono}/content/package.json | 0 templates/{http-hono-ts => http-ts-hono}/content/spin.toml | 0 templates/{http-hono-ts => http-ts-hono}/content/src/index.ts | 0 templates/{http-hono-ts => http-ts-hono}/content/tsconfig.json | 0 .../{http-hono-ts => http-ts-hono}/content/webpack.config.js | 0 .../metadata/snippets/component.txt | 0 .../{http-hono-ts => http-ts-hono}/metadata/spin-template.toml | 2 +- 19 files changed, 2 insertions(+), 2 deletions(-) rename templates/{http-hono-js => http-js-hono}/content/.gitignore (100%) rename templates/{http-hono-js => http-js-hono}/content/.npmrc (100%) rename templates/{http-hono-js => http-js-hono}/content/config/knitwit.json (100%) rename templates/{http-hono-js => http-js-hono}/content/package.json (100%) rename templates/{http-hono-js => http-js-hono}/content/spin.toml (100%) rename templates/{http-hono-js => http-js-hono}/content/src/index.js (100%) rename templates/{http-hono-js => http-js-hono}/content/webpack.config.js (100%) rename templates/{http-hono-js => http-js-hono}/metadata/snippets/component.txt (100%) rename templates/{http-hono-js => http-js-hono}/metadata/spin-template.toml (94%) rename templates/{http-hono-ts => http-ts-hono}/content/.gitignore (100%) rename templates/{http-hono-ts => http-ts-hono}/content/.npmrc (100%) rename templates/{http-hono-ts => http-ts-hono}/content/config/knitwit.json (100%) rename templates/{http-hono-ts => http-ts-hono}/content/package.json (100%) rename templates/{http-hono-ts => http-ts-hono}/content/spin.toml (100%) rename templates/{http-hono-ts => http-ts-hono}/content/src/index.ts (100%) rename templates/{http-hono-ts => http-ts-hono}/content/tsconfig.json (100%) rename templates/{http-hono-ts => http-ts-hono}/content/webpack.config.js (100%) rename templates/{http-hono-ts => http-ts-hono}/metadata/snippets/component.txt (100%) rename templates/{http-hono-ts => http-ts-hono}/metadata/spin-template.toml (94%) diff --git a/templates/http-hono-js/content/.gitignore b/templates/http-js-hono/content/.gitignore similarity index 100% rename from templates/http-hono-js/content/.gitignore rename to templates/http-js-hono/content/.gitignore diff --git a/templates/http-hono-js/content/.npmrc b/templates/http-js-hono/content/.npmrc similarity index 100% rename from templates/http-hono-js/content/.npmrc rename to templates/http-js-hono/content/.npmrc diff --git a/templates/http-hono-js/content/config/knitwit.json b/templates/http-js-hono/content/config/knitwit.json similarity index 100% rename from templates/http-hono-js/content/config/knitwit.json rename to templates/http-js-hono/content/config/knitwit.json diff --git a/templates/http-hono-js/content/package.json b/templates/http-js-hono/content/package.json similarity index 100% rename from templates/http-hono-js/content/package.json rename to templates/http-js-hono/content/package.json diff --git a/templates/http-hono-js/content/spin.toml b/templates/http-js-hono/content/spin.toml similarity index 100% rename from templates/http-hono-js/content/spin.toml rename to templates/http-js-hono/content/spin.toml diff --git a/templates/http-hono-js/content/src/index.js b/templates/http-js-hono/content/src/index.js similarity index 100% rename from templates/http-hono-js/content/src/index.js rename to templates/http-js-hono/content/src/index.js diff --git a/templates/http-hono-js/content/webpack.config.js b/templates/http-js-hono/content/webpack.config.js similarity index 100% rename from templates/http-hono-js/content/webpack.config.js rename to templates/http-js-hono/content/webpack.config.js diff --git a/templates/http-hono-js/metadata/snippets/component.txt b/templates/http-js-hono/metadata/snippets/component.txt similarity index 100% rename from templates/http-hono-js/metadata/snippets/component.txt rename to templates/http-js-hono/metadata/snippets/component.txt diff --git a/templates/http-hono-js/metadata/spin-template.toml b/templates/http-js-hono/metadata/spin-template.toml similarity index 94% rename from templates/http-hono-js/metadata/spin-template.toml rename to templates/http-js-hono/metadata/spin-template.toml index e72c4c52..bd69da48 100644 --- a/templates/http-hono-js/metadata/spin-template.toml +++ b/templates/http-js-hono/metadata/spin-template.toml @@ -1,5 +1,5 @@ manifest_version = "1" -id = "http-hono-js" +id = "http-js-hono" description = "HTTP request handler using JavaScript and Hono" [add_component] diff --git a/templates/http-hono-ts/content/.gitignore b/templates/http-ts-hono/content/.gitignore similarity index 100% rename from templates/http-hono-ts/content/.gitignore rename to templates/http-ts-hono/content/.gitignore diff --git a/templates/http-hono-ts/content/.npmrc b/templates/http-ts-hono/content/.npmrc similarity index 100% rename from templates/http-hono-ts/content/.npmrc rename to templates/http-ts-hono/content/.npmrc diff --git a/templates/http-hono-ts/content/config/knitwit.json b/templates/http-ts-hono/content/config/knitwit.json similarity index 100% rename from templates/http-hono-ts/content/config/knitwit.json rename to templates/http-ts-hono/content/config/knitwit.json diff --git a/templates/http-hono-ts/content/package.json b/templates/http-ts-hono/content/package.json similarity index 100% rename from templates/http-hono-ts/content/package.json rename to templates/http-ts-hono/content/package.json diff --git a/templates/http-hono-ts/content/spin.toml b/templates/http-ts-hono/content/spin.toml similarity index 100% rename from templates/http-hono-ts/content/spin.toml rename to templates/http-ts-hono/content/spin.toml diff --git a/templates/http-hono-ts/content/src/index.ts b/templates/http-ts-hono/content/src/index.ts similarity index 100% rename from templates/http-hono-ts/content/src/index.ts rename to templates/http-ts-hono/content/src/index.ts diff --git a/templates/http-hono-ts/content/tsconfig.json b/templates/http-ts-hono/content/tsconfig.json similarity index 100% rename from templates/http-hono-ts/content/tsconfig.json rename to templates/http-ts-hono/content/tsconfig.json diff --git a/templates/http-hono-ts/content/webpack.config.js b/templates/http-ts-hono/content/webpack.config.js similarity index 100% rename from templates/http-hono-ts/content/webpack.config.js rename to templates/http-ts-hono/content/webpack.config.js diff --git a/templates/http-hono-ts/metadata/snippets/component.txt b/templates/http-ts-hono/metadata/snippets/component.txt similarity index 100% rename from templates/http-hono-ts/metadata/snippets/component.txt rename to templates/http-ts-hono/metadata/snippets/component.txt diff --git a/templates/http-hono-ts/metadata/spin-template.toml b/templates/http-ts-hono/metadata/spin-template.toml similarity index 94% rename from templates/http-hono-ts/metadata/spin-template.toml rename to templates/http-ts-hono/metadata/spin-template.toml index 7178dae7..f0a99582 100644 --- a/templates/http-hono-ts/metadata/spin-template.toml +++ b/templates/http-ts-hono/metadata/spin-template.toml @@ -1,5 +1,5 @@ manifest_version = "1" -id = "http-hono-ts" +id = "http-ts-hono" description = "HTTP request handler using TypeScript and Hono" [add_component] From 5418bd8b0aff51bc8c65e144e27083b4a6971c79 Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Thu, 20 Mar 2025 19:07:10 +0100 Subject: [PATCH 4/9] chore: add support for hono,itty,none router to http-ts and http-js templates Signed-off-by: Thorsten Hans --- templates/http-js-hono/content/.gitignore | 5 -- templates/http-js-hono/content/.npmrc | 1 - .../http-js-hono/content/config/knitwit.json | 9 --- templates/http-js-hono/content/package.json | 23 -------- templates/http-js-hono/content/spin.toml | 18 ------ templates/http-js-hono/content/src/index.js | 21 ------- .../http-js-hono/content/webpack.config.js | 26 --------- .../metadata/snippets/component.txt | 11 ---- .../http-js-hono/metadata/spin-template.toml | 12 ---- templates/http-js/content/package.json | 49 ++++++++-------- templates/http-js/content/src/index.js | 15 ----- templates/http-js/content/src/index.js.tmpl | 53 ++++++++++++++++++ templates/http-js/metadata/spin-template.toml | 1 + templates/http-ts-hono/content/.gitignore | 5 -- templates/http-ts-hono/content/.npmrc | 1 - .../http-ts-hono/content/config/knitwit.json | 9 --- templates/http-ts-hono/content/package.json | 25 --------- templates/http-ts-hono/content/spin.toml | 18 ------ templates/http-ts-hono/content/src/index.ts | 22 -------- templates/http-ts-hono/content/tsconfig.json | 21 ------- .../http-ts-hono/content/webpack.config.js | 38 ------------- .../metadata/snippets/component.txt | 11 ---- .../http-ts-hono/metadata/spin-template.toml | 12 ---- templates/http-ts/content/package.json | 48 ++++++++-------- templates/http-ts/content/src/index.ts | 16 ------ templates/http-ts/content/src/index.ts.tmpl | 56 +++++++++++++++++++ templates/http-ts/metadata/spin-template.toml | 1 + 27 files changed, 162 insertions(+), 365 deletions(-) delete mode 100644 templates/http-js-hono/content/.gitignore delete mode 100644 templates/http-js-hono/content/.npmrc delete mode 100644 templates/http-js-hono/content/config/knitwit.json delete mode 100644 templates/http-js-hono/content/package.json delete mode 100644 templates/http-js-hono/content/spin.toml delete mode 100644 templates/http-js-hono/content/src/index.js delete mode 100644 templates/http-js-hono/content/webpack.config.js delete mode 100644 templates/http-js-hono/metadata/snippets/component.txt delete mode 100644 templates/http-js-hono/metadata/spin-template.toml delete mode 100644 templates/http-js/content/src/index.js create mode 100644 templates/http-js/content/src/index.js.tmpl delete mode 100644 templates/http-ts-hono/content/.gitignore delete mode 100644 templates/http-ts-hono/content/.npmrc delete mode 100644 templates/http-ts-hono/content/config/knitwit.json delete mode 100644 templates/http-ts-hono/content/package.json delete mode 100644 templates/http-ts-hono/content/spin.toml delete mode 100644 templates/http-ts-hono/content/src/index.ts delete mode 100644 templates/http-ts-hono/content/tsconfig.json delete mode 100644 templates/http-ts-hono/content/webpack.config.js delete mode 100644 templates/http-ts-hono/metadata/snippets/component.txt delete mode 100644 templates/http-ts-hono/metadata/spin-template.toml delete mode 100644 templates/http-ts/content/src/index.ts create mode 100644 templates/http-ts/content/src/index.ts.tmpl diff --git a/templates/http-js-hono/content/.gitignore b/templates/http-js-hono/content/.gitignore deleted file mode 100644 index 758762fd..00000000 --- a/templates/http-js-hono/content/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -node_modules -dist -target -.spin/ -build/ \ No newline at end of file diff --git a/templates/http-js-hono/content/.npmrc b/templates/http-js-hono/content/.npmrc deleted file mode 100644 index 408607a3..00000000 --- a/templates/http-js-hono/content/.npmrc +++ /dev/null @@ -1 +0,0 @@ -KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/http-js-hono/content/config/knitwit.json b/templates/http-js-hono/content/config/knitwit.json deleted file mode 100644 index a9375762..00000000 --- a/templates/http-js-hono/content/config/knitwit.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "version": 1, - "project": { - "worlds": [ - "spin-http" - ] - }, - "packages": {} -} \ No newline at end of file diff --git a/templates/http-js-hono/content/package.json b/templates/http-js-hono/content/package.json deleted file mode 100644 index d6e77802..00000000 --- a/templates/http-js-hono/content/package.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "{{project-name | kebab_case}}", - "version": "1.0.0", - "description": "{{project-description}}", - "main": "index.js", - "scripts": { - "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/{{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", - "@fermyon/knitwit": "0.3.0" - }, - "dependencies": { - "@fermyon/spin-sdk": "^3.0.0", - "hono": "^4.7.4" - } -} diff --git a/templates/http-js-hono/content/spin.toml b/templates/http-js-hono/content/spin.toml deleted file mode 100644 index 038370f3..00000000 --- a/templates/http-js-hono/content/spin.toml +++ /dev/null @@ -1,18 +0,0 @@ -spin_manifest_version = 2 - -[application] -authors = ["{{authors}}"] -description = "{{project-description}}" -name = "{{project-name}}" -version = "0.1.0" - -[[trigger.http]] -route = "{{http-path}}" -component = "{{project-name | kebab_case}}" - -[component.{{project-name | kebab_case}}] -source = "dist/{{project-name | kebab_case}}.wasm" -exclude_files = ["**/node_modules"] -[component.{{project-name | kebab_case}}.build] -command = ["npm install", "npm run build"] -watch = ["src/**/*.js"] \ No newline at end of file diff --git a/templates/http-js-hono/content/src/index.js b/templates/http-js-hono/content/src/index.js deleted file mode 100644 index 0f8ede02..00000000 --- a/templates/http-js-hono/content/src/index.js +++ /dev/null @@ -1,21 +0,0 @@ -// 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('/:name', (c) => { - return c.json({ message: `Hello, ${c.req.param('name')}` }) -}); - -app.fire(); \ No newline at end of file diff --git a/templates/http-js-hono/content/webpack.config.js b/templates/http-js-hono/content/webpack.config.js deleted file mode 100644 index 70ed621d..00000000 --- a/templates/http-js-hono/content/webpack.config.js +++ /dev/null @@ -1,26 +0,0 @@ -const path = require('path'); -const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") - -module.exports = { - entry: './src/index.js', - experiments: { - outputModule: true, - }, - output: { - path: path.resolve(__dirname, './build'), - filename: 'bundle.js', - module: true, - library: { - type: "module", - } - }, - plugins: [ - new SpinSdkPlugin() - ], - optimization: { - minimize: false - }, - performance: { - hints: false, - } -}; diff --git a/templates/http-js-hono/metadata/snippets/component.txt b/templates/http-js-hono/metadata/snippets/component.txt deleted file mode 100644 index 1e5388ae..00000000 --- a/templates/http-js-hono/metadata/snippets/component.txt +++ /dev/null @@ -1,11 +0,0 @@ -[[trigger.http]] -route = "{{http-path}}" -component = "{{project-name | kebab_case}}" - -[component.{{project-name | kebab_case}}] -source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" -allowed_outbound_hosts = [] - -[component.{{project-name | kebab_case}}.build] -command = "npm install && npm run build" -workdir = "{{ output-path }}" diff --git a/templates/http-js-hono/metadata/spin-template.toml b/templates/http-js-hono/metadata/spin-template.toml deleted file mode 100644 index bd69da48..00000000 --- a/templates/http-js-hono/metadata/spin-template.toml +++ /dev/null @@ -1,12 +0,0 @@ -manifest_version = "1" -id = "http-js-hono" -description = "HTTP request handler using JavaScript and Hono" - -[add_component] -skip_files = ["spin.toml"] -[add_component.snippets] -component = "component.txt" - -[parameters] -project-description = { type = "string", prompt = "Description", default = "" } -http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } diff --git a/templates/http-js/content/package.json b/templates/http-js/content/package.json index be018a35..18c0823f 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..d348be72 --- /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('/: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 universe")) + .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': 'application/json' + } + }) +} + +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..17d3289c 100644 --- a/templates/http-js/metadata/spin-template.toml +++ b/templates/http-js/metadata/spin-template.toml @@ -10,3 +10,4 @@ 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", pattern = "^(itty|hono|none)$" } diff --git a/templates/http-ts-hono/content/.gitignore b/templates/http-ts-hono/content/.gitignore deleted file mode 100644 index 758762fd..00000000 --- a/templates/http-ts-hono/content/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -node_modules -dist -target -.spin/ -build/ \ No newline at end of file diff --git a/templates/http-ts-hono/content/.npmrc b/templates/http-ts-hono/content/.npmrc deleted file mode 100644 index 408607a3..00000000 --- a/templates/http-ts-hono/content/.npmrc +++ /dev/null @@ -1 +0,0 @@ -KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/http-ts-hono/content/config/knitwit.json b/templates/http-ts-hono/content/config/knitwit.json deleted file mode 100644 index a9375762..00000000 --- a/templates/http-ts-hono/content/config/knitwit.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "version": 1, - "project": { - "worlds": [ - "spin-http" - ] - }, - "packages": {} -} \ No newline at end of file diff --git a/templates/http-ts-hono/content/package.json b/templates/http-ts-hono/content/package.json deleted file mode 100644 index c375a41c..00000000 --- a/templates/http-ts-hono/content/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "{{project-name | kebab_case}}", - "version": "1.0.0", - "description": "{{project-description}}", - "main": "index.js", - "scripts": { - "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/{{ 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", - "@fermyon/knitwit": "0.3.0" - }, - "dependencies": { - "@fermyon/spin-sdk": "^3.0.0", - "hono": "^4.7.4" - } -} diff --git a/templates/http-ts-hono/content/spin.toml b/templates/http-ts-hono/content/spin.toml deleted file mode 100644 index 82c26af2..00000000 --- a/templates/http-ts-hono/content/spin.toml +++ /dev/null @@ -1,18 +0,0 @@ -spin_manifest_version = 2 - -[application] -authors = ["{{authors}}"] -description = "{{project-description}}" -name = "{{project-name}}" -version = "0.1.0" - -[[trigger.http]] -route = "{{http-path}}" -component = "{{project-name | kebab_case}}" - -[component.{{project-name | kebab_case}}] -source = "dist/{{project-name | kebab_case}}.wasm" -exclude_files = ["**/node_modules"] -[component.{{project-name | kebab_case}}.build] -command = ["npm install", "npm run build"] -watch = ["src/**/*.ts"] \ No newline at end of file diff --git a/templates/http-ts-hono/content/src/index.ts b/templates/http-ts-hono/content/src/index.ts deleted file mode 100644 index 583af7ba..00000000 --- a/templates/http-ts-hono/content/src/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -// 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('/:name', (c: Context) => { - return c.json({ message: `Hello, ${c.req.param('name')}` }) -}); - -app.fire(); \ No newline at end of file diff --git a/templates/http-ts-hono/content/tsconfig.json b/templates/http-ts-hono/content/tsconfig.json deleted file mode 100644 index bcbd6f7f..00000000 --- a/templates/http-ts-hono/content/tsconfig.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "compilerOptions": { - "outDir": "./dist/", - "noImplicitAny": true, - "module": "es6", - "target": "es2020", - "jsx": "react", - "skipLibCheck": true, - "lib": [ - "ES2020", - "WebWorker" - ], - "allowJs": true, - "strict": true, - "noImplicitReturns": true, - "moduleResolution": "node" - }, - "include": [ - "src/**/*" - ] -} \ No newline at end of file diff --git a/templates/http-ts-hono/content/webpack.config.js b/templates/http-ts-hono/content/webpack.config.js deleted file mode 100644 index dac0a25f..00000000 --- a/templates/http-ts-hono/content/webpack.config.js +++ /dev/null @@ -1,38 +0,0 @@ -const path = require('path'); -const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") - -module.exports = { - entry: './src/index.ts', - experiments: { - outputModule: true, - }, - module: { - rules: [ - { - test: /\.tsx?$/, - use: 'ts-loader', - exclude: /node_modules/, - }, - ], - }, - resolve: { - extensions: ['.tsx', '.ts', '.js'], - }, - output: { - path: path.resolve(__dirname, './build'), - filename: 'bundle.js', - module: true, - library: { - type: "module", - } - }, - plugins: [ - new SpinSdkPlugin() - ], - optimization: { - minimize: false - }, - performance: { - hints: false, - } -}; diff --git a/templates/http-ts-hono/metadata/snippets/component.txt b/templates/http-ts-hono/metadata/snippets/component.txt deleted file mode 100644 index b8184acb..00000000 --- a/templates/http-ts-hono/metadata/snippets/component.txt +++ /dev/null @@ -1,11 +0,0 @@ -[[trigger.http]] -route = "{{http-path}}" -component = "{{project-name | kebab_case}}" - -[component.{{project-name | kebab_case}}] -source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" -allowed_outbound_hosts = [] - -[component.{{project-name | kebab_case}}.build] -command = "npm install && npm run build" -workdir = "{{ output-path }}" \ No newline at end of file diff --git a/templates/http-ts-hono/metadata/spin-template.toml b/templates/http-ts-hono/metadata/spin-template.toml deleted file mode 100644 index f0a99582..00000000 --- a/templates/http-ts-hono/metadata/spin-template.toml +++ /dev/null @@ -1,12 +0,0 @@ -manifest_version = "1" -id = "http-ts-hono" -description = "HTTP request handler using TypeScript and Hono" - -[add_component] -skip_files = ["spin.toml"] -[add_component.snippets] -component = "component.txt" - -[parameters] -project-description = { type = "string", prompt = "Description", default = "" } -http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } diff --git a/templates/http-ts/content/package.json b/templates/http-ts/content/package.json index d703050c..0f09b68f 100644 --- a/templates/http-ts/content/package.json +++ b/templates/http-ts/content/package.json @@ -1,25 +1,25 @@ { - "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": "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/{{ 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", + "@fermyon/knitwit": "0.3.0" + }, + "dependencies": { + "@fermyon/spin-sdk": "^3.0.0", + "itty-router": "^5.0.18" + } +} 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..44dd3451 --- /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('/: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': 'application/json' + } + }) +} + +//@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..934d51ad 100644 --- a/templates/http-ts/metadata/spin-template.toml +++ b/templates/http-ts/metadata/spin-template.toml @@ -10,3 +10,4 @@ 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", pattern = "^(itty|hono|none)$" } From f4c9cdd8ec8e23e225b02f3c40dd83632c730639 Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Thu, 20 Mar 2025 19:11:27 +0100 Subject: [PATCH 5/9] chore: unify response payload across all template configs Signed-off-by: Thorsten Hans --- templates/http-js/content/src/index.js.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/http-js/content/src/index.js.tmpl b/templates/http-js/content/src/index.js.tmpl index d348be72..7d795d33 100644 --- a/templates/http-js/content/src/index.js.tmpl +++ b/templates/http-js/content/src/index.js.tmpl @@ -31,7 +31,7 @@ let router = AutoRouter(); // 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("/", () => new Response("Hello, Spin!")) .get('/hello/:name', ({ name }) => `Hello, ${name}!`) addEventListener('fetch', (event) => { From 076c77b6088bd552d966f77bb95fd102a99edcee Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Fri, 11 Apr 2025 12:45:11 +0200 Subject: [PATCH 6/9] chore: use new select ui for http router Signed-off-by: Thorsten Hans --- templates/http-js/content/src/index.js.tmpl | 4 ++-- templates/http-js/metadata/spin-template.toml | 6 +++++- templates/http-ts/content/src/index.ts.tmpl | 6 +++--- templates/http-ts/metadata/spin-template.toml | 6 +++++- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/templates/http-js/content/src/index.js.tmpl b/templates/http-js/content/src/index.js.tmpl index 7d795d33..830d66cb 100644 --- a/templates/http-js/content/src/index.js.tmpl +++ b/templates/http-js/content/src/index.js.tmpl @@ -16,7 +16,7 @@ app.use(async (c, next) => { }) app.get('/', (c) => c.text('Hello, Spin!')); -app.get('/:name', (c) => { +app.get('/hello/:name', (c) => { return c.json({ message: `Hello, ${c.req.param('name')}` }) }); @@ -42,7 +42,7 @@ function handle(_request) { return new Response("Hello, Spin!", { status: 200, headers: { - 'content-type': 'application/json' + 'content-type': 'text/plain' } }) } diff --git a/templates/http-js/metadata/spin-template.toml b/templates/http-js/metadata/spin-template.toml index 17d3289c..1ba50b62 100644 --- a/templates/http-js/metadata/spin-template.toml +++ b/templates/http-js/metadata/spin-template.toml @@ -10,4 +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", pattern = "^(itty|hono|none)$" } +http-router = { type = "string", prompt = "HTTP Router", default = "itty", allowed_values = [ + "itty", + "hono", + "none", +] } diff --git a/templates/http-ts/content/src/index.ts.tmpl b/templates/http-ts/content/src/index.ts.tmpl index 44dd3451..3c8508de 100644 --- a/templates/http-ts/content/src/index.ts.tmpl +++ b/templates/http-ts/content/src/index.ts.tmpl @@ -17,8 +17,8 @@ app.use(async (c: Context, next: Next) => { }) app.get('/', (c: Context) => c.text('Hello, Spin!')); -app.get('/:name', (c: Context) => { - return c.json({ message: `Hello, ${c.req.param('name')}` }) +app.get('/hello/:name', (c: Context) => { + return c.json({ message: `Hello, ${c.req.param('name')}!` }) }); app.fire(); @@ -44,7 +44,7 @@ function handle(_request: Request): Response { return new Response("Hello, Spin!", { status: 200, headers: { - 'content-type': 'application/json' + 'content-type': 'text/plain' } }) } diff --git a/templates/http-ts/metadata/spin-template.toml b/templates/http-ts/metadata/spin-template.toml index 934d51ad..0d4e8bad 100644 --- a/templates/http-ts/metadata/spin-template.toml +++ b/templates/http-ts/metadata/spin-template.toml @@ -10,4 +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", pattern = "^(itty|hono|none)$" } +http-router = { type = "string", prompt = "HTTP Router", default = "itty", allowed_values = [ + "itty", + "hono", + "none", +] } From 5f8d5a78cc41d68a185a9a1cd3df338d1a7b653d Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Fri, 11 Apr 2025 12:45:35 +0200 Subject: [PATCH 7/9] chore: update GitHub Action to test all routers Signed-off-by: Thorsten Hans --- .github/workflows/test.yaml | 120 +++++++++++++++++------------------- 1 file changed, 58 insertions(+), 62 deletions(-) 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 From eac6e0d628697b79e8ce48333270ac0ec3abd69f Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Mon, 14 Apr 2025 09:34:45 +0200 Subject: [PATCH 8/9] chore: use single quotes in all http-* template source files Signed-off-by: Thorsten Hans --- templates/http-js/content/src/index.js.tmpl | 4 ++-- templates/http-ts/content/src/index.ts.tmpl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/http-js/content/src/index.js.tmpl b/templates/http-js/content/src/index.js.tmpl index 830d66cb..9669c87b 100644 --- a/templates/http-js/content/src/index.js.tmpl +++ b/templates/http-js/content/src/index.js.tmpl @@ -31,7 +31,7 @@ let router = AutoRouter(); // 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('/', () => new Response('Hello, Spin!')) .get('/hello/:name', ({ name }) => `Hello, ${name}!`) addEventListener('fetch', (event) => { @@ -39,7 +39,7 @@ addEventListener('fetch', (event) => { }); {% else %} function handle(_request) { - return new Response("Hello, Spin!", { + return new Response('Hello, Spin!', { status: 200, headers: { 'content-type': 'text/plain' diff --git a/templates/http-ts/content/src/index.ts.tmpl b/templates/http-ts/content/src/index.ts.tmpl index 3c8508de..bb3a9443 100644 --- a/templates/http-ts/content/src/index.ts.tmpl +++ b/templates/http-ts/content/src/index.ts.tmpl @@ -32,7 +32,7 @@ let router = AutoRouter(); // 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('/', () => new Response('Hello, Spin!')) .get('/hello/:name', ({ name }) => `Hello, ${name}!`) //@ts-ignore @@ -41,7 +41,7 @@ addEventListener('fetch', (event: FetchEvent) => { }); {% else %} function handle(_request: Request): Response { - return new Response("Hello, Spin!", { + return new Response('Hello, Spin!', { status: 200, headers: { 'content-type': 'text/plain' From 7e4cf81e9a9d889c874ea995de383a59ec5bc3c3 Mon Sep 17 00:00:00 2001 From: Thorsten Hans Date: Mon, 14 Apr 2025 12:52:40 +0200 Subject: [PATCH 9/9] chore: incorporate latest changes from main Signed-off-by: Thorsten Hans --- templates/http-js/content/package.json | 2 +- templates/http-ts/content/package.json | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/templates/http-js/content/package.json b/templates/http-js/content/package.json index 18c0823f..6bb8f845 100644 --- a/templates/http-js/content/package.json +++ b/templates/http-js/content/package.json @@ -23,6 +23,6 @@ "itty-router": "^5.0.18", {%- endcase %} "@spinframework/build-tools": "^1.0.1", - "@spinframework/wasi-http-proxy": "^1.0.0", + "@spinframework/wasi-http-proxy": "^1.0.0" } } diff --git a/templates/http-ts/content/package.json b/templates/http-ts/content/package.json index 0f09b68f..45f26a7b 100644 --- a/templates/http-ts/content/package.json +++ b/templates/http-ts/content/package.json @@ -4,7 +4,7 @@ "description": "{{project-description}}", "main": "index.js", "scripts": { - "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/{{ project-name | kebab_case }}.wasm", + "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": [], @@ -15,11 +15,16 @@ "ts-loader": "^9.4.1", "typescript": "^4.8.4", "webpack": "^5.74.0", - "webpack-cli": "^4.10.0", - "@fermyon/knitwit": "0.3.0" + "webpack-cli": "^4.10.0" }, "dependencies": { - "@fermyon/spin-sdk": "^3.0.0", - "itty-router": "^5.0.18" + {%- 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" } }