Make certain bits of info accessable as global variables from anywhere
[mediagoblin.git] / mediagoblin / app.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU 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 import urllib
18
19 import routes
20 import mongokit
21 from webob import Request, exc
22
23 from mediagoblin import routing, util, models, storage, staticdirect
24 from mediagoblin.globals import setup_globals
25
26
27 class Error(Exception): pass
28 class ImproperlyConfigured(Error): pass
29
30
31 class MediaGoblinApp(object):
32 """
33 Really basic wsgi app using routes and WebOb.
34 """
35 def __init__(self, connection, database_path,
36 public_store, queue_store,
37 staticdirector,
38 user_template_path=None):
39 # Get the template environment
40 self.template_env = util.get_jinja_env(user_template_path)
41
42 # Set up storage systems
43 self.public_store = public_store
44 self.queue_store = queue_store
45
46 # Set up database
47 self.connection = connection
48 self.db = connection[database_path]
49 models.register_models(connection)
50
51 # set up routing
52 self.routing = routing.get_mapper()
53
54 # set up staticdirector tool
55 self.staticdirector = staticdirector
56
57 # certain properties need to be accessed globally eg from
58 # validators, etc, which might not access to the request
59 # object.
60 setup_globals(
61 db_connection=connection,
62 database=self.db,
63 public_store=self.public_store,
64 queue_store=self.queue_store)
65
66 def __call__(self, environ, start_response):
67 request = Request(environ)
68 path_info = request.path_info
69
70 ## Routing / controller loading stuff
71 route_match = self.routing.match(path_info)
72
73 # No matching page?
74 if route_match is None:
75 # Try to do see if we have a match with a trailing slash
76 # added and if so, redirect
77 if not path_info.endswith('/') \
78 and request.method == 'GET' \
79 and self.routing.match(path_info + '/'):
80 new_path_info = path_info + '/'
81 if request.GET:
82 new_path_info = '%s?%s' % (
83 new_path_info, urllib.urlencode(request.GET))
84 redirect = exc.HTTPTemporaryRedirect(location=new_path_info)
85 return request.get_response(redirect)(environ, start_response)
86
87 # Okay, no matches. 404 time!
88 return exc.HTTPNotFound()(environ, start_response)
89
90 controller = util.import_component(route_match['controller'])
91 request.start_response = start_response
92
93 ## Attach utilities to the request object
94 request.matchdict = route_match
95 request.urlgen = routes.URLGenerator(self.routing, environ)
96 # Do we really want to load this via middleware? Maybe?
97 request.session = request.environ['beaker.session']
98 # Attach self as request.app
99 # Also attach a few utilities from request.app for convenience?
100 request.app = self
101 request.template_env = self.template_env
102 request.db = self.db
103 request.staticdirect = self.staticdirector
104
105 util.setup_user_in_request(request)
106
107 return controller(request)(environ, start_response)
108
109
110 def paste_app_factory(global_config, **kw):
111 # Get the database connection
112 connection = mongokit.Connection(
113 kw.get('db_host'), kw.get('db_port'))
114
115 # Set up the storage systems.
116 public_store = storage.storage_system_from_paste_config(
117 kw, 'publicstore')
118 queue_store = storage.storage_system_from_paste_config(
119 kw, 'queuestore')
120
121 # Set up the staticdirect system
122 if kw.has_key('direct_remote_path'):
123 staticdirector = staticdirect.RemoteStaticDirect(
124 kw['direct_remote_path'].strip())
125 elif kw.has_key('direct_remote_paths'):
126 staticdirector = staticdirect.MultiRemoteStaticDirect(
127 dict([line.strip().split(' ', 1)
128 for line in kw['direct_remote_paths'].strip().splitlines()]))
129 else:
130 raise ImproperlyConfigured(
131 "One of direct_remote_path or direct_remote_paths must be provided")
132
133 mgoblin_app = MediaGoblinApp(
134 connection, kw.get('db_name', 'mediagoblin'),
135 public_store=public_store, queue_store=queue_store,
136 staticdirector=staticdirector,
137 user_template_path=kw.get('local_templates'))
138
139 return mgoblin_app