-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): add proxy counter app
- Loading branch information
Showing
8 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
``` | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
``` | ||
npm run deploy | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "hono-do-example-proxy-counter", | ||
"private": true, | ||
"version": "0.0.4", | ||
"scripts": { | ||
"lint": "eslint --fix --ext .ts,.tsx src", | ||
"lint:check": "eslint --ext .ts,.tsx src", | ||
"format": "prettier --write \"src/**/*.{ts,tsx}\"", | ||
"format:check": "prettier --check \"src/**/*.{ts,tsx}\"", | ||
"dev": "wrangler dev src/index.ts --port 3000", | ||
"deploy": "wrangler deploy --minify src/index.ts" | ||
}, | ||
"dependencies": { | ||
"hono": "^3.6.0", | ||
"hono-do": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20230821.0", | ||
"wrangler": "^3.7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { generateHonoObject } from "hono-do"; | ||
|
||
export const Counter = generateHonoObject("/counter", async (app, state) => { | ||
const { storage } = state; | ||
let value = (await storage.get<number>("value")) ?? 0; | ||
|
||
app.post("/increment", (c) => { | ||
storage.put("value", value++); | ||
return c.text(value.toString()); | ||
}); | ||
|
||
app.post("/decrement", (c) => { | ||
storage.put("value", value--); | ||
return c.text(value.toString()); | ||
}); | ||
|
||
app.get("/", (c) => { | ||
return c.text(value.toString()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Hono } from "hono"; | ||
|
||
import { Counter } from "./counter"; | ||
import { Template } from "./template"; | ||
|
||
const app = new Hono(); | ||
|
||
app.use("/counter/*", Counter.byName("COUNTER", "counter")); | ||
|
||
app.get("/", (c) => { | ||
return c.html(Template); | ||
}); | ||
|
||
export default app; | ||
export * from "./counter"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export const Template = /*html*/ ` | ||
<html> | ||
<body> | ||
<h1>Counter</h1> | ||
<p>Current value: <span id="value"></span></p> | ||
<button id="increment">Increment</button> | ||
<button id="decrement">Decrement</button> | ||
<script> | ||
const value = document.getElementById("value"); | ||
const increment = document.getElementById("increment"); | ||
const decrement = document.getElementById("decrement"); | ||
const updateValue = async () => { | ||
const res = await fetch("/counter"); | ||
value.innerText = await res.text(); | ||
} | ||
increment.addEventListener("click", async () => { | ||
await fetch("/counter/increment", { method: "POST" }); | ||
await updateValue(); | ||
}) | ||
decrement.addEventListener("click", async () => { | ||
await fetch("/counter/decrement", { method: "POST" }); | ||
await updateValue(); | ||
}) | ||
updateValue(); | ||
</script> | ||
</body> | ||
</html> | ||
`.trim(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"lib": [ | ||
"esnext" | ||
], | ||
"types": [ | ||
"@cloudflare/workers-types" | ||
], | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "hono/jsx" | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name = "proxy-counter" | ||
compatibility_date = "2023-01-01" | ||
|
||
[durable_objects] | ||
bindings = [{ name = "COUNTER", class_name = "Counter" }] | ||
|
||
[[migrations]] | ||
tag = "v1" | ||
new_classes = ["Counter"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.