add global pet counter to site :3

This commit is contained in:
zyl 2024-06-25 16:31:26 -07:00
parent 0e444ec503
commit 29dcdc85cb
No known key found for this signature in database
14 changed files with 3677 additions and 0 deletions

33
cf/src/index.ts Normal file
View file

@ -0,0 +1,33 @@
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Bind resources to your worker in `wrangler.toml`. After adding bindings, a type definition for the
* `Env` object can be regenerated with `npm run cf-typegen`.
*
* Learn more at https://developers.cloudflare.com/workers/
*/
import { Hono } from 'hono';
import { cors } from 'hono/cors';
const app = new Hono<{ Bindings: Env }>();
app.use('/api/*', cors());
app.get('/api/pet', async (c) => {
return c.json({
count: (await c.env.DB.prepare('SELECT count FROM pets').first())!.count,
});
});
app.post('/api/pet', async (c) => {
return c.json({
count: (await c.env.DB.prepare('UPDATE pets SET count = count + 1 RETURNING count').first())!.count,
});
});
export default app satisfies ExportedHandler<Env>;