Announcing Motoko Server

Probably the most convenient approach would be to make the getRequests map able to store a new type like

{
    #sync: HttpFunction;
    #async: HttpAsyncFunction
}

and then make the handler able to process the response accordingly

cool, cheers, I’ll give this a shot :slight_smile:

Hi @kpeacock , I aimed to implement this approach here: Allow adding async requests by patnorris · Pull Request #2 · patnorris/server · GitHub does this look similar to what you had in mind?

That looks good! I do wish we had words that were more clearly dissimilar than asynchronous and synchronous for the variants.

Come to think of it, apart from it being a breaking change, is there any compelling reason to avoid just making the functions async across the board? That would probably lead to a little less boilerplate

Here’s a PR that I’ve tested with it!

Thank you, I think this looks good :slight_smile: I agree it makes sense to have it all async, simplifies the code a lot. Maybe even the http_request? For consistency but I think then it could also be made a query call (if that’s desired)

Letting http_request serve queries is definitely a feature worth preserving, when the cache has a value. I’ve got some build complications with some dependencies, but I’ll ship this once I get it working

2 Likes

I have a rookie question: This Motoko Server can be utilized for a use case involving RFC 7636: Proof Key for Code Exchange, right?

I’ve released server 0.3.0 with the new async compatible pattern!

Requests with this version will be formatted like this:

server.get(
  "/",
  func(req : Request, res : ResponseClass) : async Response {
    res.send({
      status_code = 200;
      headers = [("Content-Type", "text/html")];
      body = Text.encodeUtf8(
        "<html><body><h1>hello world</h1></body></html>"
      );
      streaming_strategy = null;
      cache_strategy = #default;
    });
  },
);

Also, cache_strategy is a required field officially.

Important note - certified responses are currently broken on local deployment as of dfx 0.15.1. They are working on mainnet, however, and are working in the latest beta for 0.15.2-beta.2

8 Likes

Really working great, I have my MVC up and running :smiley:

2 Likes

Awesome, thank you and congrats :slight_smile: I think the code looks clear and should be easy to work with!

1 Like