~benji/charms/precise/juju-gui/fix-cache-headers

« back to all changes in this revision

Viewing changes to server/guiserver/apps.py

  • Committer: Benji York
  • Date: 2013-07-31 22:08:34 UTC
  • mfrom: (60.13.9 noncharmers)
  • Revision ID: benji.york@canonical.com-20130731220834-hr78mvsri3ou0x59
mergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file is part of the Juju GUI, which lets users view and manage Juju
 
2
# environments within a graphical interface (https://launchpad.net/juju-gui).
 
3
# Copyright (C) 2013 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it under
 
6
# the terms of the GNU Affero General Public License version 3, as published by
 
7
# the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but WITHOUT
 
10
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 
11
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
# Affero General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Affero General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""Juju GUI server applications."""
 
18
 
 
19
import os
 
20
 
 
21
from tornado import web
 
22
from tornado.options import options
 
23
 
 
24
from guiserver import (
 
25
    auth,
 
26
    handlers,
 
27
)
 
28
 
 
29
 
 
30
def server():
 
31
    """Return the main server application.
 
32
 
 
33
    The server app is responsible for serving the WebSocket connection, the
 
34
    Juju GUI static files and the main index file for dynamic URLs.
 
35
    """
 
36
    # Set up static paths.
 
37
    guiroot = options.guiroot
 
38
    static_path = os.path.join(guiroot, 'juju-ui')
 
39
    # Set up the authentication backend.
 
40
    auth_backend = auth.get_backend(options.apiversion)
 
41
    # Set up handlers.
 
42
    server_handlers = [
 
43
        # Handle WebSocket connections.
 
44
        (r'^/ws$', handlers.WebSocketHandler, {'apiurl': options.apiurl}),
 
45
        # Handle static files.
 
46
        (r'^/juju-ui/(.*)', web.StaticFileHandler, {'path': static_path}),
 
47
        (r'^/(favicon\.ico)$', web.StaticFileHandler, {'path': guiroot}),
 
48
    ]
 
49
    if options.servetests:
 
50
        params = {'path': options.servetests, 'default_filename': 'index.html'}
 
51
        server_handlers.append(
 
52
            # Serve the Juju GUI tests.
 
53
            (r'^/test/(.*)', web.StaticFileHandler, params),
 
54
        )
 
55
    server_handlers.append(
 
56
        # Any other path is served by index.html.
 
57
        (r'^/(.*)', handlers.IndexHandler, {'path': guiroot}),
 
58
    )
 
59
    return web.Application(
 
60
        server_handlers, debug=options.debug, auth_backend=auth_backend)
 
61
 
 
62
 
 
63
def redirector():
 
64
    """Return the redirector application.
 
65
 
 
66
    The redirector app is responsible for redirecting HTTP traffic to HTTPS.
 
67
    """
 
68
    return web.Application([
 
69
        # Redirect all HTTP traffic to HTTPS.
 
70
        (r'.*', handlers.HttpsRedirectHandler),
 
71
    ], debug=options.debug)