-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { serve } from 'https://deno.land/[email protected]/http/server.ts'; | ||
|
||
console.log('main function started'); | ||
|
||
serve(async (req: Request) => { | ||
const url = new URL(req.url); | ||
const { pathname } = url; | ||
const path_parts = pathname.split('/'); | ||
const service_name = path_parts[1]; | ||
|
||
if (!service_name || service_name === '') { | ||
const error = { msg: 'missing function name in request' }; | ||
return new Response( | ||
JSON.stringify(error), | ||
{ status: 400, headers: { 'Content-Type': 'application/json' } }, | ||
); | ||
} | ||
|
||
const servicePath = `./test_cases/${service_name}`; | ||
console.error(`serving the request with ${servicePath}`); | ||
|
||
const createWorker = async () => { | ||
const memoryLimitMb = 150; | ||
const workerTimeoutMs = 1 * 60 * 1000; | ||
const noModuleCache = false; | ||
const importMapPath = null; | ||
const envVarsObj = Deno.env.toObject(); | ||
const envVars = Object.keys(envVarsObj).map((k) => [k, envVarsObj[k]]); | ||
|
||
return await EdgeRuntime.userWorkers.create({ | ||
servicePath, | ||
memoryLimitMb, | ||
workerTimeoutMs, | ||
noModuleCache, | ||
importMapPath, | ||
envVars, | ||
}); | ||
}; | ||
|
||
const callWorker = async () => { | ||
try { | ||
const worker = await createWorker(); | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
controller.abort(); | ||
return await worker.fetch(req, { signal }); | ||
} catch (e) { | ||
console.error(e); | ||
const error = { msg: e.toString() }; | ||
return new Response( | ||
JSON.stringify(error), | ||
{ status: 500, headers: { 'Content-Type': 'application/json' } }, | ||
); | ||
} | ||
}; | ||
|
||
return callWorker(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters