From 9108f0ca7efbfac54f5d2a7ba1f1953d2f0676c7 Mon Sep 17 00:00:00 2001 From: Lakshan Perera Date: Tue, 29 Nov 2022 11:31:43 +1100 Subject: [PATCH 1/2] fix: improve oak server example to add routes --- .../supabase/functions/oak-server/index.ts | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/edge-functions/supabase/functions/oak-server/index.ts b/examples/edge-functions/supabase/functions/oak-server/index.ts index c2891bd4e75..97bc7fdb169 100644 --- a/examples/edge-functions/supabase/functions/oak-server/index.ts +++ b/examples/edge-functions/supabase/functions/oak-server/index.ts @@ -1,13 +1,22 @@ -import { Application } from 'https://deno.land/x/oak/mod.ts' +import { Application, Router } from 'https://deno.land/x/oak/mod.ts' + +const router = new Router() +router + // Note: path will be prefixed with function name + .get('/oak-server', (context) => { + context.response.body = 'This is an example Oak server running on Edge Functions!' + }) + .post('/oak-server/greet', (context) => { + // Note: request body will be streamed to the function as chunks, set limit to 0 to fully read it. + const result = context.request.body({ type: 'json', limit: 0 }) + const body = await result.value + const name = body.name || 'you' + + context.response.body = { msg: `Hey ${name}!` } + }) const app = new Application() - -app.use(async (ctx) => { - const result = ctx.request.body({ type: 'json', limit: 0 }) - const body = await result.value - const name = body.name || 'you' - - ctx.response.body = { msg: `Hey ${name}!` } -}) +app.use(router.routes()) +app.use(router.allowedMethods()) await app.listen({ port: 8000 }) From 7df6ad35ceb7fc42a6bcd5b9f9121d12db63725c Mon Sep 17 00:00:00 2001 From: Lakshan Perera Date: Tue, 29 Nov 2022 17:01:21 +1100 Subject: [PATCH 2/2] added an endpoint for showing how to redirect --- examples/edge-functions/supabase/functions/oak-server/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/edge-functions/supabase/functions/oak-server/index.ts b/examples/edge-functions/supabase/functions/oak-server/index.ts index 97bc7fdb169..8e96d1f0033 100644 --- a/examples/edge-functions/supabase/functions/oak-server/index.ts +++ b/examples/edge-functions/supabase/functions/oak-server/index.ts @@ -14,6 +14,9 @@ router context.response.body = { msg: `Hey ${name}!` } }) + .get('/oak-server/redirect', (context) => { + context.response.redirect('https://www.example.com') + }) const app = new Application() app.use(router.routes())