Using different frameworks depending on the hostname?

1 view
Skip to first unread message

Nickolas Daskalou

unread,
Jan 4, 2010, 10:15:59 PM1/4/10
to google-a...@googlegroups.com
This is probably a pure Python question.

Is it possible to use a different framework depending on what hostname the request was made to? For example:

api.mydomain.com (API service) -> webapp
admin.mydomain.com (Backend administration) -> app-engine-patch (AEP)

The data model needs to be the same for each framework (worst case would require a duplicated models.py file, which is ok).

We want AEP for its admin capabilities for admin.mydomain.com, but we want responses from api.mydomain.com to be as fast as possible so we'd prefer to use webapp, which responds faster than AEP on "cold" requests (requests where there is no app caching yet).

johnwlockwood

unread,
Jan 6, 2010, 11:01:11 AM1/6/10
to Google App Engine
you setup the app.yaml to send urls starting with /admin/ to a python
file that loads app-engine-patch and everything else to a main.py that
loads webapp.
something like:

- url: /admin/.*
script: app_engine_patch_loader.py
login: admin

- url: /.*
script: main.py
secure: never


Then in your django urls.py handle /admin

Nickolas Daskalou

unread,
Jan 6, 2010, 7:52:27 PM1/6/10
to google-a...@googlegroups.com
Thanks johnwlockwood. I ended up doing this:

app.yaml:
....

- url: /.*
  script: main.py
....

main.py:
import os
import re

regex_api_site = re.compile('^api\.(localhost|mydomain\.com)$')
regex_admin_site = re.compile('^admin\.(localhost|mydomain\.com)$')

handlers_map = [
                {'regex':regex_api_site,
                 'handler':'WEBAPP'},
                {'regex':regex_admin_site,
                 'handler':'APP_ENGINE_PATCH'},
                ]

def main():
  http_host = os.environ['SERVER_NAME']
 
handler = None
  for handler_info in handlers_map:
    if handler_info['regex'].match(http_host):
      if handler_info['handler'] == 'WEBAPP':
        # main_webapp.py is in the root application directory
        from main_webapp import main as handler
      elif handler_info['handler'] == 'APP_ENGINE_PATCH':
        from common.appenginepatch.main import main as
handler
      break
  if not
handler:
    import logging
    logging.critical('No handler found for HTTP host "%s"',http_host)
    return 1 # Not sure if this is the correct way to return "failure"
  return
handler()

if __name__ == '__main__':

  main()


This seems to work well, although I haven't tried sharing models yet.


2010/1/7 johnwlockwood <johnwl...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.




Reply all
Reply to author
Forward
0 new messages