diff --git a/templates/http-js/content/.npmrc b/templates/http-js/content/.npmrc new file mode 100644 index 00000000..408607a3 --- /dev/null +++ b/templates/http-js/content/.npmrc @@ -0,0 +1 @@ +KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/http-js/content/knitwit.json b/templates/http-js/content/config/knitwit.json similarity index 100% rename from templates/http-js/content/knitwit.json rename to templates/http-js/content/config/knitwit.json diff --git a/templates/http-js/content/package.json b/templates/http-js/content/package.json index 0b3c8f2f..6a10fd51 100644 --- a/templates/http-js/content/package.json +++ b/templates/http-js/content/package.json @@ -4,9 +4,9 @@ "description": "{{project-description}}", "main": "index.js", "scripts": { - "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/{{project-name | kebab_case}}.wasm {% if enable-aot == 'y' or enable-aot == 'Y' %}--aot{% endif %}", + "build": "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", - "postinstall": "knitwit" + "postinstall": "knitwit --out-dir build/wit/knitwit --out-world combined" }, "keywords": [], "author": "", @@ -14,9 +14,10 @@ "devDependencies": { "mkdirp": "^3.0.1", "webpack": "^5.74.0", - "webpack-cli": "^4.10.0" + "webpack-cli": "^4.10.0", + "@fermyon/knitwit": "0.3.0" }, "dependencies": { - "@fermyon/spin-sdk": "^2.3.0" + "@fermyon/spin-sdk": "^2.4.0" } } \ No newline at end of file diff --git a/templates/http-js/content/spin.toml b/templates/http-js/content/spin.toml index e387fe73..662274cd 100644 --- a/templates/http-js/content/spin.toml +++ b/templates/http-js/content/spin.toml @@ -11,8 +11,8 @@ route = "{{http-path}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "target/{{project-name | kebab_case}}.wasm" +source = "dist/{{project-name | kebab_case}}.wasm" exclude_files = ["**/node_modules"] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" watch = ["src/**/*.ts", "package.json"] \ No newline at end of file diff --git a/templates/http-js/content/src/index.js b/templates/http-js/content/src/index.js index d6683528..2b4007cd 100644 --- a/templates/http-js/content/src/index.js +++ b/templates/http-js/content/src/index.js @@ -1,6 +1,19 @@ -import { ResponseBuilder } from "@fermyon/spin-sdk"; +import { ResponseBuilder, Router } from "@fermyon/spin-sdk"; + +let router = Router(); +// Modify this route or add additional ones to implement the component's API: +router.get("/hello/:name", (metadata, req, res) => { handleHelloRoute(req, res, metadata.params.name) }); +// Default route that will be called for any routes not handled above: +router.all("*", (_, req, res) => { notFound(req, res) }); + +async function handleHelloRoute(req, res, name) { + res.send(`hello ${name}`); +} +async function notFound(req, res) { + res.status(404); + res.send("not found"); +} export async function handler(req, res) { - console.log(req); - res.send("hello universe"); + await router.handleRequest(req, res); } diff --git a/templates/http-js/content/webpack.config.js b/templates/http-js/content/webpack.config.js index 9f86f7be..9bdc87a9 100644 --- a/templates/http-js/content/webpack.config.js +++ b/templates/http-js/content/webpack.config.js @@ -7,8 +7,8 @@ module.exports = { outputModule: true, }, output: { - path: path.resolve(__dirname, './'), - filename: 'dist.js', + path: path.resolve(__dirname, './build'), + filename: 'bundle.js', module: true, library: { type: "module", diff --git a/templates/http-js/metadata/snippets/component.txt b/templates/http-js/metadata/snippets/component.txt index 6130fc89..1e5388ae 100644 --- a/templates/http-js/metadata/snippets/component.txt +++ b/templates/http-js/metadata/snippets/component.txt @@ -3,9 +3,9 @@ route = "{{http-path}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "{{ output-path }}/target/{{project-name | kebab_case}}.wasm" +source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" allowed_outbound_hosts = [] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" workdir = "{{ output-path }}" diff --git a/templates/http-js/metadata/spin-template.toml b/templates/http-js/metadata/spin-template.toml index 228c88eb..a138db92 100644 --- a/templates/http-js/metadata/spin-template.toml +++ b/templates/http-js/metadata/spin-template.toml @@ -10,4 +10,3 @@ component = "component.txt" [parameters] project-description = { type = "string", prompt = "Description", default = "" } http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } -enable-aot = { type = "string", prompt = "Enable AoT Compilation [y/N]", default = "N", pattern = "^[yYnN]$" } diff --git a/templates/http-ts/content/.npmrc b/templates/http-ts/content/.npmrc new file mode 100644 index 00000000..408607a3 --- /dev/null +++ b/templates/http-ts/content/.npmrc @@ -0,0 +1 @@ +KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/http-ts/content/knitwit.json b/templates/http-ts/content/config/knitwit.json similarity index 100% rename from templates/http-ts/content/knitwit.json rename to templates/http-ts/content/config/knitwit.json diff --git a/templates/http-ts/content/package.json b/templates/http-ts/content/package.json index 05cd0142..db46c37f 100644 --- a/templates/http-ts/content/package.json +++ b/templates/http-ts/content/package.json @@ -4,9 +4,9 @@ "description": "{{project-description}}", "main": "index.js", "scripts": { - "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/{{ project-name | kebab_case }}.wasm {% if enable-aot == 'y' or enable-aot == 'Y' %}--aot{% endif %}", + "build": "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", - "postinstall": "knitwit" + "postinstall": "knitwit --out-dir build/wit/knitwit --out-world combined" }, "keywords": [], "author": "", @@ -16,9 +16,10 @@ "ts-loader": "^9.4.1", "typescript": "^4.8.4", "webpack": "^5.74.0", - "webpack-cli": "^4.10.0" + "webpack-cli": "^4.10.0", + "@fermyon/knitwit": "0.3.0" }, "dependencies": { - "@fermyon/spin-sdk": "^2.3.0" + "@fermyon/spin-sdk": "^2.4.0" } } \ No newline at end of file diff --git a/templates/http-ts/content/spin.toml b/templates/http-ts/content/spin.toml index e387fe73..662274cd 100644 --- a/templates/http-ts/content/spin.toml +++ b/templates/http-ts/content/spin.toml @@ -11,8 +11,8 @@ route = "{{http-path}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "target/{{project-name | kebab_case}}.wasm" +source = "dist/{{project-name | kebab_case}}.wasm" exclude_files = ["**/node_modules"] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" watch = ["src/**/*.ts", "package.json"] \ No newline at end of file diff --git a/templates/http-ts/content/src/index.ts b/templates/http-ts/content/src/index.ts index 61144ef2..28398622 100644 --- a/templates/http-ts/content/src/index.ts +++ b/templates/http-ts/content/src/index.ts @@ -1,6 +1,19 @@ -import { ResponseBuilder } from "@fermyon/spin-sdk"; +import { ResponseBuilder, Router } from "@fermyon/spin-sdk"; + +let router = Router(); +// Modify this route or add additional ones to implement the component's API: +router.get("/hello/:name", (metadata, req, res) => { handleHelloRoute(req, res, metadata.params.name) }); +// Default route that will be called for any routes not handled above: +router.all("*", (_, req, res) => { notFound(req, res) }); + +async function handleHelloRoute(req: Request, res: ResponseBuilder, name: string) { + res.send(`hello ${name}`); +} +async function notFound(req: Request, res: ResponseBuilder) { + res.status(404); + res.send("not found"); +} export async function handler(req: Request, res: ResponseBuilder) { - console.log(req); - res.send("hello universe"); + await router.handleRequest(req, res); } diff --git a/templates/http-ts/content/webpack.config.js b/templates/http-ts/content/webpack.config.js index 6b624f55..8d1d5a2f 100644 --- a/templates/http-ts/content/webpack.config.js +++ b/templates/http-ts/content/webpack.config.js @@ -19,8 +19,8 @@ module.exports = { extensions: ['.tsx', '.ts', '.js'], }, output: { - path: path.resolve(__dirname, './'), - filename: 'dist.js', + path: path.resolve(__dirname, './build'), + filename: 'bundle.js', module: true, library: { type: "module", diff --git a/templates/http-ts/metadata/snippets/component.txt b/templates/http-ts/metadata/snippets/component.txt index 73f0fda8..b8184acb 100644 --- a/templates/http-ts/metadata/snippets/component.txt +++ b/templates/http-ts/metadata/snippets/component.txt @@ -3,9 +3,9 @@ route = "{{http-path}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "{{ output-path }}/target/{{project-name | kebab_case}}.wasm" +source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" allowed_outbound_hosts = [] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" workdir = "{{ output-path }}" \ No newline at end of file diff --git a/templates/http-ts/metadata/spin-template.toml b/templates/http-ts/metadata/spin-template.toml index 7eb9d34a..c468b052 100644 --- a/templates/http-ts/metadata/spin-template.toml +++ b/templates/http-ts/metadata/spin-template.toml @@ -10,4 +10,3 @@ component = "component.txt" [parameters] project-description = { type = "string", prompt = "Description", default = "" } http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" } -enable-aot = { type = "string", prompt = "Enable AoT Compilation [y/N]", default = "N", pattern = "^[yYnN]$" } diff --git a/templates/redis-js/content/.npmrc b/templates/redis-js/content/.npmrc new file mode 100644 index 00000000..408607a3 --- /dev/null +++ b/templates/redis-js/content/.npmrc @@ -0,0 +1 @@ +KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/redis-js/content/knitwit.json b/templates/redis-js/content/config/knitwit.json similarity index 100% rename from templates/redis-js/content/knitwit.json rename to templates/redis-js/content/config/knitwit.json diff --git a/templates/redis-js/content/package.json b/templates/redis-js/content/package.json index 0b3c8f2f..6a10fd51 100644 --- a/templates/redis-js/content/package.json +++ b/templates/redis-js/content/package.json @@ -4,9 +4,9 @@ "description": "{{project-description}}", "main": "index.js", "scripts": { - "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/{{project-name | kebab_case}}.wasm {% if enable-aot == 'y' or enable-aot == 'Y' %}--aot{% endif %}", + "build": "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", - "postinstall": "knitwit" + "postinstall": "knitwit --out-dir build/wit/knitwit --out-world combined" }, "keywords": [], "author": "", @@ -14,9 +14,10 @@ "devDependencies": { "mkdirp": "^3.0.1", "webpack": "^5.74.0", - "webpack-cli": "^4.10.0" + "webpack-cli": "^4.10.0", + "@fermyon/knitwit": "0.3.0" }, "dependencies": { - "@fermyon/spin-sdk": "^2.3.0" + "@fermyon/spin-sdk": "^2.4.0" } } \ No newline at end of file diff --git a/templates/redis-js/content/spin.toml b/templates/redis-js/content/spin.toml index 723b5507..83514a3a 100644 --- a/templates/redis-js/content/spin.toml +++ b/templates/redis-js/content/spin.toml @@ -14,8 +14,8 @@ channel = "{{redis-channel}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "target/{{project-name | kebab_case}}.wasm" +source = "dist/{{project-name | kebab_case}}.wasm" exclude_files = ["**/node_modules"] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" watch = ["src/**/*.ts", "package.json"] diff --git a/templates/redis-js/content/webpack.config.js b/templates/redis-js/content/webpack.config.js index f9c536db..ab19bf51 100644 --- a/templates/redis-js/content/webpack.config.js +++ b/templates/redis-js/content/webpack.config.js @@ -7,8 +7,8 @@ module.exports = { outputModule: true, }, output: { - path: path.resolve(__dirname, './'), - filename: 'dist.js', + path: path.resolve(__dirname, './build'), + filename: 'bundle.js', module: true, library: { type: "module", diff --git a/templates/redis-js/metadata/snippets/component.txt b/templates/redis-js/metadata/snippets/component.txt index a70eaa0d..7cfe976c 100644 --- a/templates/redis-js/metadata/snippets/component.txt +++ b/templates/redis-js/metadata/snippets/component.txt @@ -3,8 +3,8 @@ channel = "{{redis-channel}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "{{ output-path }}/target/{{project-name | kebab_case}}.wasm" +source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" allowed_outbound_hosts = [] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" workdir = "{{ output-path }}" \ No newline at end of file diff --git a/templates/redis-js/metadata/spin-template.toml b/templates/redis-js/metadata/spin-template.toml index 6bef03a6..232ef7d0 100644 --- a/templates/redis-js/metadata/spin-template.toml +++ b/templates/redis-js/metadata/spin-template.toml @@ -17,4 +17,3 @@ skip_snippets = ["application_trigger"] project-description = { type = "string", prompt = "Description", default = "" } redis-address = { type = "string", prompt = "Redis address", default = "redis://localhost:6379" } redis-channel = { type = "string", prompt = "Redis channel" } -enable-aot = { type = "string", prompt = "Enable AoT Compilation [y/N]", default = "N", pattern = "^[yYnN]$" } diff --git a/templates/redis-ts/content/.npmrc b/templates/redis-ts/content/.npmrc new file mode 100644 index 00000000..408607a3 --- /dev/null +++ b/templates/redis-ts/content/.npmrc @@ -0,0 +1 @@ +KNITWIT_SOURCE=./config/knitwit.json \ No newline at end of file diff --git a/templates/redis-ts/content/knitwit.json b/templates/redis-ts/content/config/knitwit.json similarity index 100% rename from templates/redis-ts/content/knitwit.json rename to templates/redis-ts/content/config/knitwit.json diff --git a/templates/redis-ts/content/package.json b/templates/redis-ts/content/package.json index 8f6d3744..29dc8477 100644 --- a/templates/redis-ts/content/package.json +++ b/templates/redis-ts/content/package.json @@ -4,9 +4,9 @@ "description": "{{project-description}}", "main": "index.js", "scripts": { - "build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -d combined-wit -n combined -o target/{{project-name | kebab_case}}.wasm {% if enable-aot == 'y' or enable-aot == 'Y' %}--aot{% endif %}", + "build": "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", - "postinstall": "knitwit" + "postinstall": "knitwit --out-dir build/wit/knitwit --out-world combined" }, "keywords": [], "author": "", @@ -16,9 +16,10 @@ "ts-loader": "^9.4.1", "typescript": "^4.8.4", "webpack": "^5.74.0", - "webpack-cli": "^4.10.0" + "webpack-cli": "^4.10.0", + "@fermyon/knitwit": "0.3.0" }, "dependencies": { - "@fermyon/spin-sdk": "^2.3.0" + "@fermyon/spin-sdk": "^2.4.0" } } \ No newline at end of file diff --git a/templates/redis-ts/content/spin.toml b/templates/redis-ts/content/spin.toml index 723b5507..83514a3a 100644 --- a/templates/redis-ts/content/spin.toml +++ b/templates/redis-ts/content/spin.toml @@ -14,8 +14,8 @@ channel = "{{redis-channel}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "target/{{project-name | kebab_case}}.wasm" +source = "dist/{{project-name | kebab_case}}.wasm" exclude_files = ["**/node_modules"] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" watch = ["src/**/*.ts", "package.json"] diff --git a/templates/redis-ts/content/webpack.config.js b/templates/redis-ts/content/webpack.config.js index 80e4bdb9..af891fd5 100644 --- a/templates/redis-ts/content/webpack.config.js +++ b/templates/redis-ts/content/webpack.config.js @@ -19,8 +19,8 @@ module.exports = { extensions: ['.tsx', '.ts', '.js'], }, output: { - path: path.resolve(__dirname, './'), - filename: 'dist.js', + path: path.resolve(__dirname, './build'), + filename: 'bundle.js', module: true, library: { type: "module", diff --git a/templates/redis-ts/metadata/snippets/component.txt b/templates/redis-ts/metadata/snippets/component.txt index a70eaa0d..7cfe976c 100644 --- a/templates/redis-ts/metadata/snippets/component.txt +++ b/templates/redis-ts/metadata/snippets/component.txt @@ -3,8 +3,8 @@ channel = "{{redis-channel}}" component = "{{project-name | kebab_case}}" [component.{{project-name | kebab_case}}] -source = "{{ output-path }}/target/{{project-name | kebab_case}}.wasm" +source = "{{ output-path }}/dist/{{project-name | kebab_case}}.wasm" allowed_outbound_hosts = [] [component.{{project-name | kebab_case}}.build] -command = "npm run build" +command = "npm install && npm run build" workdir = "{{ output-path }}" \ No newline at end of file diff --git a/templates/redis-ts/metadata/spin-template.toml b/templates/redis-ts/metadata/spin-template.toml index ca5cbdc0..710c69e4 100644 --- a/templates/redis-ts/metadata/spin-template.toml +++ b/templates/redis-ts/metadata/spin-template.toml @@ -17,4 +17,3 @@ skip_snippets = ["application_trigger"] project-description = { type = "string", prompt = "Description", default = "" } redis-address = { type = "string", prompt = "Redis address", default = "redis://localhost:6379" } redis-channel = { type = "string", prompt = "Redis channel" } -enable-aot = { type = "string", prompt = "Enable AoT Compilation [y/N]", default = "N", pattern = "^[yYnN]$" }