|
1 | | -import { serve } from 'https://deno.land/[email protected]/http/server.ts'; |
| 1 | +import { serve } from "https://deno.land/[email protected]/http/server.ts"; |
2 | 2 |
|
3 | | -console.log('main function started'); |
| 3 | +console.log("main function started"); |
4 | 4 |
|
5 | 5 | serve(async (req: Request) => { |
6 | | - const url = new URL(req.url); |
7 | | - const { pathname } = url; |
| 6 | + const url = new URL(req.url); |
| 7 | + const { pathname } = url; |
8 | 8 |
|
9 | | - // handle health checks |
10 | | - if (pathname === '/_internal/health') { |
11 | | - return new Response( |
12 | | - JSON.stringify({ 'message': 'ok' }), |
13 | | - { status: 200, headers: { 'Content-Type': 'application/json' } }, |
14 | | - ); |
15 | | - } |
| 9 | + // handle health checks |
| 10 | + if (pathname === "/_internal/health") { |
| 11 | + return new Response( |
| 12 | + JSON.stringify({ "message": "ok" }), |
| 13 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 14 | + ); |
| 15 | + } |
16 | 16 |
|
17 | | - const path_parts = pathname.split('/'); |
18 | | - const service_name = path_parts[1]; |
| 17 | + const path_parts = pathname.split("/"); |
| 18 | + const service_name = path_parts[1]; |
19 | 19 |
|
20 | | - if (!service_name || service_name === '') { |
21 | | - const error = { msg: 'missing function name in request' }; |
22 | | - return new Response( |
23 | | - JSON.stringify(error), |
24 | | - { status: 400, headers: { 'Content-Type': 'application/json' } }, |
25 | | - ); |
26 | | - } |
| 20 | + if (!service_name || service_name === "") { |
| 21 | + const error = { msg: "missing function name in request" }; |
| 22 | + return new Response( |
| 23 | + JSON.stringify(error), |
| 24 | + { status: 400, headers: { "Content-Type": "application/json" } }, |
| 25 | + ); |
| 26 | + } |
27 | 27 |
|
28 | | - const servicePath = `./examples/${service_name}`; |
29 | | - console.error(`serving the request with ${servicePath}`); |
| 28 | + const servicePath = `./examples/${service_name}`; |
| 29 | + console.error(`serving the request with ${servicePath}`); |
30 | 30 |
|
31 | | - const createWorker = async () => { |
32 | | - const memoryLimitMb = 150; |
33 | | - const workerTimeoutMs = 10 * 1000; |
34 | | - const noModuleCache = false; |
35 | | - const customModuleRoot = null; |
36 | | - // you can provide an import map inline |
37 | | - // const inlineImportMap = { |
38 | | - // imports: { |
39 | | - // "std/": "https://deno.land/[email protected]/", |
40 | | - // "cors": "./examples/_shared/cors.ts" |
41 | | - // } |
42 | | - // } |
43 | | - // const importMapPath = `data:${encodeURIComponent(JSON.stringify(importMap))}?${encodeURIComponent('/home/deno/functions/test')}`; |
44 | | - const importMapPath = null; |
45 | | - const envVarsObj = Deno.env.toObject(); |
46 | | - const envVars = Object.keys(envVarsObj).map((k) => [k, envVarsObj[k]]); |
47 | | - const forceCreate = false; |
| 31 | + const createWorker = async () => { |
| 32 | + const memoryLimitMb = 150; |
| 33 | + const workerTimeoutMs = 5 * 60 * 1000; |
| 34 | + const noModuleCache = false; |
| 35 | + const customModuleRoot = null; |
| 36 | + // you can provide an import map inline |
| 37 | + // const inlineImportMap = { |
| 38 | + // imports: { |
| 39 | + // "std/": "https://deno.land/[email protected]/", |
| 40 | + // "cors": "./examples/_shared/cors.ts" |
| 41 | + // } |
| 42 | + // } |
| 43 | + // const importMapPath = `data:${encodeURIComponent(JSON.stringify(importMap))}?${encodeURIComponent('/home/deno/functions/test')}`; |
| 44 | + const importMapPath = null; |
| 45 | + const envVarsObj = Deno.env.toObject(); |
| 46 | + const envVars = Object.keys(envVarsObj).map((k) => [k, envVarsObj[k]]); |
| 47 | + const forceCreate = false; |
48 | 48 |
|
49 | | - return await EdgeRuntime.userWorkers.create({ |
50 | | - servicePath, |
51 | | - memoryLimitMb, |
52 | | - workerTimeoutMs, |
53 | | - noModuleCache, |
54 | | - importMapPath, |
55 | | - envVars, |
56 | | - forceCreate, |
57 | | - customModuleRoot, |
58 | | - }); |
59 | | - }; |
| 49 | + return await EdgeRuntime.userWorkers.create({ |
| 50 | + servicePath, |
| 51 | + memoryLimitMb, |
| 52 | + workerTimeoutMs, |
| 53 | + noModuleCache, |
| 54 | + importMapPath, |
| 55 | + envVars, |
| 56 | + forceCreate, |
| 57 | + customModuleRoot, |
| 58 | + }); |
| 59 | + }; |
60 | 60 |
|
61 | | - const callWorker = async () => { |
62 | | - try { |
63 | | - // If a worker for the given service path already exists, |
64 | | - // it will be reused by default. |
65 | | - // Update forceCreate option in createWorker to force create a new worker for each request. |
66 | | - const worker = await createWorker(); |
67 | | - return await worker.fetch(req); |
68 | | - } catch (e) { |
69 | | - console.error(e); |
70 | | - const error = { msg: e.toString() }; |
71 | | - return new Response( |
72 | | - JSON.stringify(error), |
73 | | - { status: 500, headers: { 'Content-Type': 'application/json' } }, |
74 | | - ); |
75 | | - } |
76 | | - }; |
| 61 | + const callWorker = async () => { |
| 62 | + try { |
| 63 | + // If a worker for the given service path already exists, |
| 64 | + // it will be reused by default. |
| 65 | + // Update forceCreate option in createWorker to force create a new worker for each request. |
| 66 | + const worker = await createWorker(); |
| 67 | + return await worker.fetch(req); |
| 68 | + } catch (e) { |
| 69 | + console.error(e); |
| 70 | + const error = { msg: e.toString() }; |
| 71 | + return new Response( |
| 72 | + JSON.stringify(error), |
| 73 | + { status: 500, headers: { "Content-Type": "application/json" } }, |
| 74 | + ); |
| 75 | + } |
| 76 | + }; |
77 | 77 |
|
78 | | - return callWorker(); |
| 78 | + return callWorker(); |
79 | 79 | }); |
0 commit comments