pogo icon indicating copy to clipboard operation
pogo copied to clipboard

Can I use Pogo with Deno Deploy?

Open brianbienvenu opened this issue 5 years ago • 6 comments

I see that Deno have released their own serverless compute platform, Deno Deploy. I'd like to use the Pogo for routing on it.

I don't see any comments about Pogo support for Deno Deploy in the deployment docs: https://github.com/sholladay/pogo/blob/master/docs/deployment.md

Can I get this to work today? Is it on a to-do list?

brianbienvenu avatar Apr 25 '21 09:04 brianbienvenu

Hi @brianbienvenu, great question. Pogo is written in a way that should make this pretty easy. Though, I haven't tried it.

You should be able to set up a Pogo app as normal and then in your Deno Deploy request handler, call server.inject() instead of server.start(), to have Pogo process the request.

Next, you'll just need to create a utility function that can convert a Deno Deploy request event into a request object of the shape expected by Deno's std/http server, since that's what server.inject() expects as input. And then you'll need another utility function that can convert a Pogo response to an object that is compatible with Deno Deploy's respondWith() method.

It would be nice to make this even simpler by updating server.inject() to use web standard Request and Response objects, the same as Deno Deploy does. Then to maintain compatibility with std/http, we could do some conversion in the server.respond() method, which is used internally by server.start().

PR welcome for any improvements to server.inject() or the documentation, etc.

sholladay avatar Apr 25 '21 13:04 sholladay

I was able to do it using:

addEventListener("fetch", async (event) => {
    const response = await server.inject(event.request);
    event.respondWith(new Response(response.body, { // for some reason, it throws error with pogo's response object
        headers: response.headers,
        status: response.status
    }));
});

twlite avatar Aug 12 '21 05:08 twlite

Now that Deno’s native server API is stable, we can update Pogo to use it instead of std/http. That will involve some changes to the request and response implementation. Then Deno Deploy will be supported out of the box without any additional work.

Anyone want to give it a try, replacing std/http with Deno.serveHttp()?

sholladay avatar Aug 25 '21 00:08 sholladay

@sholladay I'll look into it.

patlehmann1 avatar Aug 27 '21 00:08 patlehmann1

Doesn't work. CleanShot 2022-01-20 at 19 21 57@2x

AlexW00 avatar Jan 20 '22 18:01 AlexW00

Hi, all! The update to Pogo's internals to use the native APIs and web standard Request / Response objects is nearly complete. See the native-http branch and PR #68 for details. I have tested it with Deno Deploy and it seems to be working well.

Here is a working example you can deploy. Note the use of the native-http branch. Also, the as h in the second import is needed if you want to use JSX on Deno Deploy... for some reason, their compilation options for JSX syntax are different than the default that Deno itself uses.

import * as pogo from 'https://github.com/sholladay/pogo/raw/native-http/main.ts';
import { createElement as h } from "https://esm.sh/[email protected]";

const server = pogo.server();

server.router.get('/', () => {
    return <h1>Hello, World!</h1>;
});

server.start();

It would be awesome if y'all could give it a spin and let me know about any problems.

sholladay avatar Mar 29 '22 17:03 sholladay