Merge branch 'master' into joar-skip_transcoding
[mediagoblin.git] / mediagoblin / app.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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 os
18 import logging
19
20 from mediagoblin.routing import get_url_map
21 from mediagoblin.tools.routing import endpoint_to_controller
22
23 from werkzeug.wrappers import Request
24 from werkzeug.exceptions import HTTPException
25 from werkzeug.routing import RequestRedirect
26
27 from mediagoblin import meddleware, __version__
28 from mediagoblin.tools import common, translate, template
29 from mediagoblin.tools.response import render_http_exception
30 from mediagoblin.tools.theme import register_themes
31 from mediagoblin.tools import request as mg_request
32 from mediagoblin.mg_globals import setup_globals
33 from mediagoblin.init.celery import setup_celery_from_config
34 from mediagoblin.init.plugins import setup_plugins
35 from mediagoblin.init import (get_jinja_loader, get_staticdirector,
36 setup_global_and_app_config, setup_locales, setup_workbench, setup_database,
37 setup_storage, setup_beaker_cache)
38 from mediagoblin.tools.pluginapi import PluginManager
39
40
41 _log = logging.getLogger(__name__)
42
43
44 class MediaGoblinApp(object):
45 """
46 WSGI application of MediaGoblin
47
48 ... this is the heart of the program!
49 """
50 def __init__(self, config_path, setup_celery=True):
51 """
52 Initialize the application based on a configuration file.
53
54 Arguments:
55 - config_path: path to the configuration file we're opening.
56 - setup_celery: whether or not to setup celery during init.
57 (Note: setting 'celery_setup_elsewhere' also disables
58 setting up celery.)
59 """
60 _log.info("GNU MediaGoblin %s main server starting", __version__)
61 _log.debug("Using config file %s", config_path)
62 ##############
63 # Setup config
64 ##############
65
66 # Open and setup the config
67 global_config, app_config = setup_global_and_app_config(config_path)
68
69 ##########################################
70 # Setup other connections / useful objects
71 ##########################################
72
73 # load all available locales
74 setup_locales()
75
76 # Set up plugins -- need to do this early so that plugins can
77 # affect startup.
78 _log.info("Setting up plugins.")
79 setup_plugins()
80
81 # Set up the database
82 self.db = setup_database()
83
84 # Register themes
85 self.theme_registry, self.current_theme = register_themes(app_config)
86
87 # Get the template environment
88 self.template_loader = get_jinja_loader(
89 app_config.get('local_templates'),
90 self.current_theme,
91 PluginManager().get_template_paths()
92 )
93
94 # Set up storage systems
95 self.public_store, self.queue_store = setup_storage()
96
97 # set up routing
98 self.url_map = get_url_map()
99
100 # set up staticdirector tool
101 self.staticdirector = get_staticdirector(app_config)
102
103 # set up caching
104 self.cache = setup_beaker_cache()
105
106 # Setup celery, if appropriate
107 if setup_celery and not app_config.get('celery_setup_elsewhere'):
108 if os.environ.get('CELERY_ALWAYS_EAGER', 'false').lower() == 'true':
109 setup_celery_from_config(
110 app_config, global_config,
111 force_celery_always_eager=True)
112 else:
113 setup_celery_from_config(app_config, global_config)
114
115 #######################################################
116 # Insert appropriate things into mediagoblin.mg_globals
117 #
118 # certain properties need to be accessed globally eg from
119 # validators, etc, which might not access to the request
120 # object.
121 #######################################################
122
123 setup_globals(app=self)
124
125 # Workbench *currently* only used by celery, so this only
126 # matters in always eager mode :)
127 setup_workbench()
128
129 # instantiate application meddleware
130 self.meddleware = [common.import_component(m)(self)
131 for m in meddleware.ENABLED_MEDDLEWARE]
132
133 def call_backend(self, environ, start_response):
134 request = Request(environ)
135
136 # Compatibility with django, use request.args preferrably
137 request.GET = request.args
138
139 ## Routing / controller loading stuff
140 map_adapter = self.url_map.bind_to_environ(request.environ)
141
142 # By using fcgi, mediagoblin can run under a base path
143 # like /mediagoblin/. request.path_info contains the
144 # path inside mediagoblin. If the something needs the
145 # full path of the current page, that should include
146 # the basepath.
147 # Note: urlgen and routes are fine!
148 request.full_path = environ["SCRIPT_NAME"] + request.path
149 # python-routes uses SCRIPT_NAME. So let's use that too.
150 # The other option would be:
151 # request.full_path = environ["SCRIPT_URL"]
152
153 # Fix up environ for urlgen
154 # See bug: https://bitbucket.org/bbangert/routes/issue/55/cache_hostinfo-breaks-on-https-off
155 if environ.get('HTTPS', '').lower() == 'off':
156 environ.pop('HTTPS')
157
158 ## Attach utilities to the request object
159 # Do we really want to load this via middleware? Maybe?
160 request.session = request.environ['beaker.session']
161 # Attach self as request.app
162 # Also attach a few utilities from request.app for convenience?
163 request.app = self
164
165 request.db = self.db
166 request.staticdirect = self.staticdirector
167
168 request.locale = translate.get_locale_from_request(request)
169 request.template_env = template.get_jinja_env(
170 self.template_loader, request.locale)
171
172 def build_proxy(endpoint, **kw):
173 try:
174 qualified = kw.pop('qualified')
175 except KeyError:
176 qualified = False
177
178 return map_adapter.build(
179 endpoint,
180 values=dict(**kw),
181 force_external=qualified)
182
183 request.urlgen = build_proxy
184
185 mg_request.setup_user_in_request(request)
186
187 try:
188 found_rule, url_values = map_adapter.match(return_rule=True)
189 request.matchdict = url_values
190 except RequestRedirect as response:
191 # Deal with 301 responses eg due to missing final slash
192 return response(environ, start_response)
193 except HTTPException as exc:
194 # Stop and render exception
195 return render_http_exception(
196 request, exc,
197 exc.get_description(environ))(environ, start_response)
198
199 controller = endpoint_to_controller(found_rule)
200
201 # pass the request through our meddleware classes
202 try:
203 for m in self.meddleware:
204 response = m.process_request(request, controller)
205 if response is not None:
206 return response(environ, start_response)
207 except HTTPException as e:
208 return render_http_exception(
209 request, e,
210 e.get_description(environ))(environ, start_response)
211
212 request.start_response = start_response
213
214 # get the Http response from the controller
215 try:
216 response = controller(request)
217 except HTTPException as e:
218 response = render_http_exception(
219 request, e, e.get_description(environ))
220
221 # pass the response through the meddlewares
222 try:
223 for m in self.meddleware[::-1]:
224 m.process_response(request, response)
225 except HTTPException as e:
226 response = render_http_exeption(
227 request, e, e.get_description(environ))
228
229 return response(environ, start_response)
230
231 def __call__(self, environ, start_response):
232 ## If more errors happen that look like unclean sessions:
233 # self.db.check_session_clean()
234
235 try:
236 return self.call_backend(environ, start_response)
237 finally:
238 # Reset the sql session, so that the next request
239 # gets a fresh session
240 self.db.reset_after_request()
241
242
243 def paste_app_factory(global_config, **app_config):
244 configs = app_config['config'].split()
245 mediagoblin_config = None
246 for config in configs:
247 if os.path.exists(config) and os.access(config, os.R_OK):
248 mediagoblin_config = config
249 break
250
251 if not mediagoblin_config:
252 raise IOError("Usable mediagoblin config not found.")
253
254 mgoblin_app = MediaGoblinApp(mediagoblin_config)
255
256 return mgoblin_app