appengine safer dispatch.yaml routing to modules / services - google-app-engine

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.

Related

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

GAE dispatch.yaml path rules not working

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

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

redirect all requests from one domain to another with Google App Engine but keep static routing rules in yaml

I have a GAE app serving static files defined by rules in the yaml file under two different domain names as configured in DNS, an old one and a new one, but otherwise it's the same content served for each. I'd like to redirect requests from the old domain to the new domain. I've seen this question, but that loses the ability to use the static asset handlers in the yaml from what I can tell, and would have to set up static asset serving explicitly in my main.py I think. Is there a simple way (ideally in the yaml file itself) to do a redirect when the hostname is the old domain, but keep my static file rules in place for the new domain?
Update
Here's a complete solution that I ended up using:
### dispatch.yaml ###
dispatch:
- url: "*my.domain/*"
module: redirect-module
### redirector.yaml ###
module: redirect-module
runtime: python27
threadsafe: true
api_version: 1
skip_files:
- ^(?!redirector.py$)
handlers:
# Redirect everything via our redirector
- url: /.*
script: redirector.app
### redirector.py ###
import webapp2
def get_redirect_uri(handler, *args, **kwargs):
return 'https://my.domain/' + kwargs.get('path')
app = webapp2.WSGIApplication([
webapp2.Route('/<path:.*>', webapp2.RedirectHandler, defaults={'_uri': get_redirect_uri}),
], debug=False)
Some extra docs:
https://cloud.google.com/appengine/docs/python/modules/routing#routing_with_a_dispatch_file
AFAIK you can't do redirection for the static assets, since GAE serves them directly according to the .yaml file rules, without even hitting your app code.
You could add a module (let's call it redirect-module for example) to your app, route ALL old domain URLs to it using a dispatcher file and use a dynamic handler in this module to redirect URLs to the new domain equivalents, along the lines suggested in the answers to the question you referenced. The new domain requests will continue to work unmodified, served either as static assets or the existing module(s) of your app. The dispatch.yaml file would look like this:
application: your-app-name
dispatch:
- url: "your.old.domain.com/*"
module: redirect-module
Another thought that comes to mind (I didn't actually do this, so I'm unsure if it would address your problem) is to avoid the redirect altogether and instead of mapping your app to 2 different domains map it only to the new domain and make the old domain a DNS CNAME/alias to the new domain.

AppEngine modules on custom domain

I plan to make a main website and a blog.
www.domain-name.com will be my main website and blog.domain-name.com
will be my blog.
I am able to achieve this on my appspot domain, but on my custom domain, it doesn't work.
https://blog-dot-domainname.appspot.com/ is showing the correct
webpage.
Currently blog.domain-name.com shows the exact same thing as www.domain-name.com.
Here's what I did:
dispatch.yaml file
dispatch:
# Default module serves the typical web resources and all static resources.
- url: "*/favicon.ico"
module: default
- url: "domain-name.com"
module: default
- url: "blog.domain-name.com/*"
module: blog
I also setup two custom domain names on the AppEngine settings:
CUSTOM DOMAIN NAMES domain-name.com blog.domain-name.com
I am using Namecheap and I setup the required CNAME alias for my bog subdomain.

Resources