diff options
| author | akiyamn | 2023-10-04 23:29:19 +1100 |
|---|---|---|
| committer | akiyamn | 2023-10-04 23:29:19 +1100 |
| commit | a2d1c179f88e056265321b426a51907a3941044a (patch) | |
| tree | 3804227ee072265a1cf1121cc59eb1fbdc74a3e2 /src/index.ts | |
| parent | 4e87195739f2a5d9a05451b48773c8afdc680765 (diff) | |
| download | price-tracker-worker-a2d1c179f88e056265321b426a51907a3941044a.tar.gz price-tracker-worker-a2d1c179f88e056265321b426a51907a3941044a.zip | |
Kind of worksmaster
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/src/index.ts b/src/index.ts index 674339b..3008e35 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import { cacheAllPrices, ProductRequest } from "./cron"; /** * Welcome to Cloudflare Workers! * @@ -29,10 +30,27 @@ export interface Env { // MY_QUEUE: Queue; // // Example binding to a D1 Database. Learn more at https://developers.cloudflare.com/workers/platform/bindings/#d1-database-bindings - // DB: D1Database + DB: D1Database } export default { + + async fetch(request: Request, env: Env, ctx: ExecutionContext) { + const { pathname } = new URL(request.url); + switch (pathname) { + case "/api/history": + const { results } = await env.DB.prepare( + "SELECT * FROM Prices" + ) + .all(); + return Response.json(results); + case "/": + return new Response('🀄'); + default: + return new Response("404 💩", { status: 404 }) + } + }, + // The scheduled handler is invoked at the interval set in our wrangler.toml's // [[triggers]] configuration. async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> { @@ -40,11 +58,30 @@ export default { // publish to a Queue, query a D1 Database, and much more. // // We'll keep it simple and make an API call to a Cloudflare API: - let resp = await fetch('https://api.cloudflare.com/client/v4/ips'); - let wasSuccessful = resp.ok ? 'success' : 'fail'; + // let resp = await fetch('https://api.cloudflare.com/client/v4/ips'); + // let wasSuccessful = resp.ok ? 'success' : 'fail'; + + + + const products : ProductRequest[] = [ + {productId: "888140", companyName: "Woolies"}, + // {productId: "8150288", companyName: "Coles"}, + {productId: "581170", companyName: "Woolies"}, + // {productId: "4901345", companyName: "Coles"}, + // {productId: "165367", companyName: "Woolies"}, + // {productId: "5178633", companyName: "Coles"}, + // {productId: "165367", companyName: "Woolies"}, + // {productId: "5178633", companyName: "Coles"}, + // {productId: "778171", companyName: "Woolies"}, + // {productId: "2271188", companyName: "Coles"}, + // {productId: "829281", companyName: "Woolies"}, + // {productId: "2236788", companyName: "Coles"}, + ] + const success = await cacheAllPrices(env.DB, products) + console.log(`Fetching and caching ${products.length} products. Success: ${success}`) - // You could store this result in KV, write to a D1 Database, or publish to a Queue. - // In this template, we'll just log the result: - console.log(`trigger fired at ${event.cron}: ${wasSuccessful}`); + // // You could store this result in KV, write to a D1 Database, or publish to a Queue. + // // In this template, we'll just log the result: + // console.log(`trigger fired at ${event.cron}: ${ok}`); }, }; |
