sholladay / pogo

Server framework for Deno
Mozilla Public License 2.0
482 stars 32 forks source link

Can I use Pogo with Deno Deploy? #62

Closed brianbienvenu closed 2 years ago

brianbienvenu commented 3 years ago

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?

sholladay commented 3 years ago

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.

twlite commented 3 years ago

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
    }));
});
sholladay commented 3 years ago

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()?

patlehmann1 commented 3 years ago

@sholladay I'll look into it.

AlexW00 commented 2 years ago

Doesn't work.

CleanShot 2022-01-20 at 19 21 57@2x
sholladay commented 2 years ago

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/react@17.0.2";

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.