73 lines
1.7 KiB
TypeScript
73 lines
1.7 KiB
TypeScript
import type {
|
|
ExecutionContext,
|
|
D1Database,
|
|
DurableObjectNamespace,
|
|
} from '@cloudflare/workers-types'
|
|
import { CacheCreator } from './src/jobs/cache-creator'
|
|
import {
|
|
handleNotifQueue,
|
|
handleOrderPlacedQueue,
|
|
handleOrderCancelledQueue,
|
|
} from './src/lib/queue-consumer'
|
|
|
|
export { CacheCreator }
|
|
|
|
export default {
|
|
async fetch(
|
|
request: Request,
|
|
env: Record<string, string> & {
|
|
DB?: D1Database
|
|
CACHE_CREATOR: DurableObjectNamespace
|
|
NOTIF_QUEUE: {
|
|
send: (message: unknown) => Promise<void>
|
|
}
|
|
ORDER_PLACED_QUEUE: {
|
|
send: (message: unknown) => Promise<void>
|
|
}
|
|
ORDER_CANCELLED_QUEUE: {
|
|
send: (message: unknown) => Promise<void>
|
|
}
|
|
},
|
|
ctx: ExecutionContext
|
|
) {
|
|
;(globalThis as any).ENV = env
|
|
const { createApp } = await import('./src/app')
|
|
const { initDb } = await import('./src/dbService')
|
|
if (env.DB) {
|
|
initDb(env.DB)
|
|
}
|
|
const app = createApp()
|
|
return app.fetch(request, env, ctx)
|
|
},
|
|
async queue(
|
|
batch: any,
|
|
env: Record<string, string> & {
|
|
NOTIF_QUEUE: {
|
|
send: (message: unknown) => Promise<void>
|
|
}
|
|
ORDER_PLACED_QUEUE: {
|
|
send: (message: unknown) => Promise<void>
|
|
}
|
|
ORDER_CANCELLED_QUEUE: {
|
|
send: (message: unknown) => Promise<void>
|
|
}
|
|
}
|
|
) {
|
|
if (batch?.queue === 'notif_queue') {
|
|
handleNotifQueue(batch)
|
|
return
|
|
}
|
|
|
|
if (batch?.queue === 'order_placed_queue') {
|
|
await handleOrderPlacedQueue(batch)
|
|
return
|
|
}
|
|
|
|
if (batch?.queue === 'order_cancelled_queue') {
|
|
await handleOrderCancelledQueue(batch)
|
|
return
|
|
}
|
|
|
|
handleNotifQueue(batch)
|
|
},
|
|
}
|