GAE dispatch.yaml path rules not working - google-app-engine

I've got a dispatch.yaml like
application: myapp
dispatch:
- url: "www.myapp.com/*"
module: frontend
- url: "www.myapp.com/api/*"
module: api
- url: "foo.myapp.com/*"
module: foo
The foo route works just fine, but requests to www.myapp.com/api/whatever get routed to the frontend app. I can't get a request to hit the api module, as evidenced by the logs.
Why doesn't my dispatch rule work?

Order matters in dispatch.yaml. GAE uses the first rule that gets matched. Requests to www.myapp.com/api/whatever match the www.myapp.com/* wildcard, so the frontend module is used for those.
Switch the order of those rules to read:
dispatch:
- url: "www.myapp.com/api/*"
module: api
- url: "www.myapp.com/*"
module: frontend

Related

Mapping a custom domain route to an application in App Engine

I have a custom domain with a route that I'd like to map to my React application (create-react-app production build being served on App engine). When I map the custom domain, none of my static files are found however. I've tried updating the handlers for the app but I'm getting the same error (404 on loading css, js, manifest). My dispatch.yaml is
dispatch:
- url: "example.service.org/test"
service: my-service
Accessing example.service.org/test correctly populates the page title but none of the assets load. How do I update my handlers to serve the files correctly?
If this is working in your appspot.com domain means that your static routes works fine.
I think that the issue is caused because your service doesn't know how to address this path
/test/static
I think that are multiple solutions to this issue:
1.- Map the domain with the service without /test
dispatch:
- url: "example.service.org"
service: my-service
with this workaround if you need a test url you can add an test subdomain in order to have 2 services one in prod and another in dev/test for example
dispatch:
- url: "example.service.org"
service: my-service
- url: "test.example.service.org"
service: test-my-service
2.- define in your app.yaml this handlers
handlers:
- url: /static
static_dir: static/
- url: /test/static
static_dir: static/

Mapping subdomain to a service in Google App Engine project

I have a Google App Engine project with following yaml file
handlers:
- url: /web/.*
script: web_server.app
- url: /api/.*
script: rest_server.app
How do I make sure subdomain, of a domain I own, be served by rest_server.app script.
Example: If I own example.com
I want example.com to be served by web_server.app, and api.example.com to be served by rest_server.app
Is it possible to do that using Google App Engine.
Example:
handlers:
- url: example.com/.*
script: web_server.app
- url: api.example.com/.*
script: rest_server.app
Request routing in the app.yaml can not be used to route based on the URL's domain name, see the url table row in the Handlers element doc section.
Because of that you can't really have a single module/service serving your app while simultaneously stripping the filepath portion of the URL you're currently used in your handlers' url configs for routing requests to one script or the other.
You can obtain what you desire by splitting your app into 2 separate services/modules, each handling one script. One of the modules has to be the default module, I'd make the web one the default.
A dispatch.yaml file would be used to route requests to their respective modules based on the URL hostname.
The web.yaml file would contain:
module: default
handlers:
- url: /.*
script: web_server.app
The rest.yaml file would contain:
module: rest
handlers:
- url: /.*
script: rest_server.app
In the dispatch.yaml file you only need routes for the non-default module(s), requests matching no routes are by default routed to the defaut module:
- url: "api.example.com/*"
module: rest
You can find a more complete example here: https://stackoverflow.com/a/34111170/4495081
You'd then map both your example.com naked domain and api.example.com subdomain to your app. Follow the Adding a custom domain for your application procedure, paying extra attention to the sections which are slightly different when configuring a naked domain vs a subdomain. See also https://stackoverflow.com/a/36317462/4495081
There is one problem, tho - dispatch.yaml routing based on hostnames doesn't work with the local development server, the requests destined for the rest module would actually go to the default module.
A simpler workaround would be to direct the rest module clients to the actual localhost:PORT URL where the local devserver's rest module listens (displayed in the terminal at dev server startup), instead.
This might not be possible in all cases or for all apps. For example it's an issue if the app makes cross-module requests with auto-generated URLs.
In such cases, to work around it you can temporarily insert a small path portion in the rest.yaml URL, only during testing on the local dev server the rest module (you'd need matching changes on the client side and/or the cross-module URL generation logic):
module: rest
handlers:
- url: /api/.*
script: rest_server.app
And then you can add a dispatch.yaml rule that is not host-based and would also with the local dev server. This can be left in there permanently, it doesn't hurt if/when deployed in production when the temporary rest.yaml change is reversed:
- url: "api.example.com/*"
module: rest
- url: "*/api/*"
module: rest

appengine safer dispatch.yaml routing to modules / services

This is a basic sense check question as i'm deploying a few new modules:
dispatch.yaml:
application: my-app
# No version required; this does routing independent of version.
dispatch:
# Default module serves the typical web resources and all static resources.
- url: "*/favicon.ico"
module: default
# Default module serves simple hostname request.
- url: "simple-sample.appspot.com/"
module: default
# Send all mobile traffic to the mobile frontend.
- url: "*/mobile/*"
module: mobile-frontend
# Send all work to the one static backend.
- url: "*/work/*"
module: static-backend
Wouldn't it be safer to put "*.com/mobile/*" instead of "*/mobile/*" ? In case other modules could use /mobile/ in their urls somewhere and accidentally get routed to mobile-frontend?
What if I have domain names other than .com e.g. .io?
Yes, it could be considered safer in the way you're looking at it.
For the additional .io (or other) domains you can add a rule for each of those suffixes:
- url: "*.com/mobile/*"
module: mobile-frontend
- url: "*.io/mobile/*"
module: mobile-frontend
Side note: you don't actually need to specify the rules for the default module - all requests not matching any rules in the dispatch files are by default routed to the default module, making such rules redundant. You can test this by making a request not matching any of your dispatch.yaml rules and watching the default module's logs.

Is there any way to route sub-domain to module in Google Appengine

I configured my dispatch.yaml file to route /admin to Admin module. But I want to route my sub-domain to route the Admin module.
My dispatch.yaml file contains the following code
- url: "*/admin"
module: admin
- url: "*/admin/*"
module: admin
In admin module
app = webapp2.WSGIApplication([
routes.DomainRoute('admin.knowyouroffers.in', [
webapp2.Route('/', handler=Admin, name='home'),
]),debug=settings.debug)
I tried the following code in dispatch.yaml. But It couldn't work
- url: "admin.knowyouroffers.in/*"
module: admin
You don't need to specify the domain in the admin module, that code gets invoked only after the request has been already routed to the module following the dispatch.yaml rules. Plain path routing in the module is enough and it also allows you to test your code before deploying in production (which otherwise is the only place where the domain would be filled in properly to match the routing rules).
Check your request logs in all modules to debug routing. See also this Q&A:
Appengine: Routing with dispatch.yaml

Google App Engine Static Frontend Instance Does Not Start

My static front-end instance (B1 instance_class) does not start when I make a URL request to it. Any idea? I had not touched my Google App Engine project for the past 4 weeks and suddenly it doesn't work any more.
When I try to access the url, I get 404 Not Found problem. No instance of the static front-end module starts.
If my dispatch.yaml is as follows, if I make the following call : www.jitai-api.appspot.com/jawbone_work, it should be routed to the static-frontend module, right? This doesn't seem to happen.
application: jitai-api
dispatch:
- url: "jitai-api.appspot.com/"
module: default
- url: "jitai-api.appspot.com/jawbone_work*"
module: static-frontend
- url: "jitai-api.appspot.com/google_work*"
module: static-frontend
Your dispatch rules match the hostname jitai-api.appspot.com, not www.jitai-api.appspot.com. Try defining them like this:
application: jitai-api
dispatch:
- url: "*/jawbone_work*"
module: static-frontend
- url: "*/google_work*"
module: static-frontend
- url: "*/"
module: default
From the docs
# Matches the hostname 'customer1.myapp.com', but not '1.customer1.myapp.com.
- url: "customer1.myapp.com/*"
module: static-backend

Resources