Sunday, December 27, 2015

Multi-module dispatcher for AppEngine Development Server

The great thing about AppEngine is that you can test and use almost all features during development locally. However there's one really important thing missing for everybody that is using modules - as suggested by the official documentation: accessing all your modules via the same port, where requests are routed according to your configuration in dispatch.xml. This feature is not available locally: "All dispatch files are ignored when running the development server. The only way to target instances is through their ports."

That makes some tasks really complicated, like sending requests from a web client. Usually you send the request to the same server hosting the resources, but with a multi-module setup that does not work locally, because the module you want to access is hosted under another port than the one serving your web client's resources. At first we adapted our code accordingly, which included a really complicated Grunt-configuration to change server URLs in JavaScript accordingly. Eventually we hit a roadblock because cookies set by one module where not visible for requests sent to another module (because they act as different hostnames). The solution we came up with is a proxy server in front of your modules which routes requests like AppEngine does when deployed.

In order to achieve this, we're using some grunt-plugins:


Now call "grunt dispatch" and your server is ready to go at localhost:9000. All requests with a URL matching "/api" (e.g. "localhost:9000/api/hello") are routed to localhost:8081 and all the rest goes to localhost:8080. As you can see this does not read your actual configuration from dispatch.xml, but it's really easy to set up anyway (same logic applies, just JSON instead of XML).

The same applies to Python users with dispatch.yaml of course...