File: multiapp.py

package info (click to toggle)
gunicorn 20.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,088 kB
  • sloc: python: 9,219; makefile: 161; xml: 73; sh: 40; javascript: 35
file content (58 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.
#
# Run this application with:
#
#   $ gunicorn multiapp:app
#
# And then visit:
#
#   http://127.0.0.1:8000/app1url
#   http://127.0.0.1:8000/app2url
#   http://127.0.0.1:8000/this_is_a_404
#

try:
    from routes import Mapper
except ImportError:
    print("This example requires Routes to be installed")

# Obviously you'd import your app callables
# from different places...
from test import app as app1
from test import app as app2


class Application(object):
    def __init__(self):
        self.map = Mapper()
        self.map.connect('app1', '/app1url', app=app1)
        self.map.connect('app2', '/app2url', app=app2)

    def __call__(self, environ, start_response):
        match = self.map.routematch(environ=environ)
        if not match:
            return self.error404(environ, start_response)
        return match[0]['app'](environ, start_response)

    def error404(self, environ, start_response):
        html = b"""\
        <html>
          <head>
            <title>404 - Not Found</title>
          </head>
          <body>
            <h1>404 - Not Found</h1>
          </body>
        </html>
        """
        headers = [
            ('Content-Type', 'text/html'),
            ('Content-Length', str(len(html)))
        ]
        start_response('404 Not Found', headers)
        return [html]

app = Application()