summaryrefslogtreecommitdiff
path: root/src/worker.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/worker.ts')
-rw-r--r--src/worker.ts43
1 files changed, 8 insertions, 35 deletions
diff --git a/src/worker.ts b/src/worker.ts
index 5eb9d5d..52e9f5e 100644
--- a/src/worker.ts
+++ b/src/worker.ts
@@ -8,46 +8,19 @@ interface Env {
}
class CounterDurableObject extends DurableObject<Env> {
- constructor(ctx: DurableObjectState, env: Env) {
- super(ctx, env);
- }
-
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
- const pathname = url.pathname;
-
- if (pathname === '/increment') {
- const currentValue = (await this.ctx.storage.get<number>('counter')) || 0;
- const newValue = currentValue + 1;
- await this.ctx.storage.put('counter', newValue);
-
- return new Response(JSON.stringify({
- counter: newValue,
- timestamp: new Date().toISOString()
- }), {
- headers: { 'Content-Type': 'application/json' }
- });
- }
- if (pathname === '/get') {
- const currentValue = (await this.ctx.storage.get<number>('counter')) || 0;
- return new Response(JSON.stringify({
- counter: currentValue,
- timestamp: new Date().toISOString()
- }), {
- headers: { 'Content-Type': 'application/json' }
- });
+ if (url.pathname === '/get') {
+ const count = (await this.ctx.storage.get<number>('count')) || 0;
+ return new Response(count.toString());
}
- if (pathname === '/reset') {
- await this.ctx.storage.put('counter', 0);
- return new Response(JSON.stringify({
- counter: 0,
- message: 'Counter reset',
- timestamp: new Date().toISOString()
- }), {
- headers: { 'Content-Type': 'application/json' }
- });
+ if (url.pathname === '/increment') {
+ const count = (await this.ctx.storage.get<number>('count')) || 0;
+ const newCount = count + 1;
+ await this.ctx.storage.put('count', newCount);
+ return new Response(newCount.toString());
}
return new Response('Not found', { status: 404 });