Commit | Line | Data |
---|---|---|
8e1e744d | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
e5572c60 ML |
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 | ||
571198c9 | 17 | import os |
ec97c937 | 18 | import logging |
31a8ff42 | 19 | |
3d914332 E |
20 | from mediagoblin.routing import get_url_map |
21 | from mediagoblin.tools.routing import endpoint_to_controller | |
7742dcc1 | 22 | |
f1d06e1d | 23 | from werkzeug.wrappers import Request |
e5e2c5e7 | 24 | from werkzeug.exceptions import HTTPException |
fd61aac7 | 25 | from werkzeug.routing import RequestRedirect |
19baab1b | 26 | from werkzeug.wsgi import SharedDataMiddleware |
31a8ff42 | 27 | |
7742dcc1 | 28 | from mediagoblin import meddleware, __version__ |
26583b2c | 29 | from mediagoblin.db.util import check_db_up_to_date |
c7424612 | 30 | from mediagoblin.tools import common, session, translate, template |
785b287f | 31 | from mediagoblin.tools.response import render_http_exception |
828fc630 | 32 | from mediagoblin.tools.theme import register_themes |
152a3bfa | 33 | from mediagoblin.tools import request as mg_request |
c81186dd | 34 | from mediagoblin.media_types.tools import media_type_warning |
6e7ce8d1 | 35 | from mediagoblin.mg_globals import setup_globals |
073b61fe | 36 | from mediagoblin.init.celery import setup_celery_from_config |
29b6f917 | 37 | from mediagoblin.init.plugins import setup_plugins |
50854db0 | 38 | from mediagoblin.init import (get_jinja_loader, get_staticdirector, |
6ef75af5 | 39 | setup_global_and_app_config, setup_locales, setup_workbench, setup_database, |
9e1fa239 | 40 | setup_storage) |
c5d8d301 | 41 | from mediagoblin.tools.pluginapi import PluginManager, hook_transform |
5907154a | 42 | from mediagoblin.tools.crypto import setup_crypto |
c9dec8b3 | 43 | from mediagoblin.auth.tools import check_auth_enabled, no_auth_logout |
90e342f9 | 44 | |
31a8ff42 | 45 | |
ec97c937 E |
46 | _log = logging.getLogger(__name__) |
47 | ||
48 | ||
8e1e744d | 49 | class MediaGoblinApp(object): |
31a8ff42 | 50 | """ |
3f5cf663 CAW |
51 | WSGI application of MediaGoblin |
52 | ||
53 | ... this is the heart of the program! | |
31a8ff42 | 54 | """ |
3f5cf663 CAW |
55 | def __init__(self, config_path, setup_celery=True): |
56 | """ | |
57 | Initialize the application based on a configuration file. | |
58 | ||
59 | Arguments: | |
60 | - config_path: path to the configuration file we're opening. | |
61 | - setup_celery: whether or not to setup celery during init. | |
62 | (Note: setting 'celery_setup_elsewhere' also disables | |
63 | setting up celery.) | |
64 | """ | |
ec97c937 | 65 | _log.info("GNU MediaGoblin %s main server starting", __version__) |
3f369674 | 66 | _log.debug("Using config file %s", config_path) |
3f5cf663 CAW |
67 | ############## |
68 | # Setup config | |
69 | ############## | |
70 | ||
71 | # Open and setup the config | |
fe289be4 | 72 | global_config, app_config = setup_global_and_app_config(config_path) |
3f5cf663 | 73 | |
c81186dd RE |
74 | media_type_warning() |
75 | ||
5907154a E |
76 | setup_crypto() |
77 | ||
3f5cf663 CAW |
78 | ########################################## |
79 | # Setup other connections / useful objects | |
80 | ########################################## | |
81 | ||
b0ee3aae E |
82 | # Setup Session Manager, not needed in celery |
83 | self.session_manager = session.SessionManager() | |
84 | ||
6ef75af5 SS |
85 | # load all available locales |
86 | setup_locales() | |
87 | ||
29b6f917 WKG |
88 | # Set up plugins -- need to do this early so that plugins can |
89 | # affect startup. | |
90 | _log.info("Setting up plugins.") | |
91 | setup_plugins() | |
92 | ||
3f5cf663 | 93 | # Set up the database |
4a698535 | 94 | self.db = setup_database(app_config['run_migrations']) |
ff94114c | 95 | |
26583b2c | 96 | # Quit app if need to run dbupdate |
31f8909f CAW |
97 | ## NOTE: This is currently commented out due to session errors.. |
98 | ## We'd like to re-enable! | |
99 | # check_db_up_to_date() | |
26583b2c | 100 | |
828fc630 | 101 | # Register themes |
975be468 | 102 | self.theme_registry, self.current_theme = register_themes(app_config) |
828fc630 | 103 | |
5afdd7a1 | 104 | # Get the template environment |
42ef819c | 105 | self.template_loader = get_jinja_loader( |
3b47da8e | 106 | app_config.get('local_templates'), |
8545dd50 | 107 | self.current_theme, |
05e007c1 | 108 | PluginManager().get_template_paths() |
8545dd50 | 109 | ) |
0c8a30e6 | 110 | |
744f1c83 RE |
111 | # Check if authentication plugin is enabled and respond accordingly. |
112 | self.auth = check_auth_enabled() | |
1bce0c15 RE |
113 | if not self.auth: |
114 | app_config['allow_comments'] = False | |
744f1c83 | 115 | |
5afdd7a1 | 116 | # Set up storage systems |
dccef262 | 117 | self.public_store, self.queue_store = setup_storage() |
5afdd7a1 CAW |
118 | |
119 | # set up routing | |
48cf435d | 120 | self.url_map = get_url_map() |
31a8ff42 | 121 | |
582c4d5f | 122 | # set up staticdirector tool |
c85c9dc7 | 123 | self.staticdirector = get_staticdirector(app_config) |
3f5cf663 CAW |
124 | |
125 | # Setup celery, if appropriate | |
126 | if setup_celery and not app_config.get('celery_setup_elsewhere'): | |
d9a31a39 | 127 | if os.environ.get('CELERY_ALWAYS_EAGER', 'false').lower() == 'true': |
3f5cf663 CAW |
128 | setup_celery_from_config( |
129 | app_config, global_config, | |
130 | force_celery_always_eager=True) | |
131 | else: | |
132 | setup_celery_from_config(app_config, global_config) | |
133 | ||
134 | ####################################################### | |
135 | # Insert appropriate things into mediagoblin.mg_globals | |
136 | # | |
df9809c2 CAW |
137 | # certain properties need to be accessed globally eg from |
138 | # validators, etc, which might not access to the request | |
139 | # object. | |
3f5cf663 CAW |
140 | ####################################################### |
141 | ||
243c3843 | 142 | setup_globals(app=self) |
1fd97db3 CAW |
143 | |
144 | # Workbench *currently* only used by celery, so this only | |
145 | # matters in always eager mode :) | |
7664b4db | 146 | setup_workbench() |
df9809c2 | 147 | |
ce5ae8da CAW |
148 | # instantiate application meddleware |
149 | self.meddleware = [common.import_component(m)(self) | |
150 | for m in meddleware.ENABLED_MEDDLEWARE] | |
0c8a30e6 | 151 | |
e824570a | 152 | def call_backend(self, environ, start_response): |
31a8ff42 | 153 | request = Request(environ) |
0c8a30e6 | 154 | |
726896b6 | 155 | # Compatibility with django, use request.args preferrably |
f1d06e1d | 156 | request.GET = request.args |
f1d06e1d | 157 | |
582c4d5f | 158 | ## Routing / controller loading stuff |
7742dcc1 | 159 | map_adapter = self.url_map.bind_to_environ(request.environ) |
31a8ff42 | 160 | |
05788ef4 E |
161 | # By using fcgi, mediagoblin can run under a base path |
162 | # like /mediagoblin/. request.path_info contains the | |
163 | # path inside mediagoblin. If the something needs the | |
164 | # full path of the current page, that should include | |
165 | # the basepath. | |
166 | # Note: urlgen and routes are fine! | |
f1d06e1d | 167 | request.full_path = environ["SCRIPT_NAME"] + request.path |
05788ef4 E |
168 | # python-routes uses SCRIPT_NAME. So let's use that too. |
169 | # The other option would be: | |
170 | # request.full_path = environ["SCRIPT_URL"] | |
171 | ||
871fc591 | 172 | # Fix up environ for urlgen |
d23d4b23 | 173 | # See bug: https://bitbucket.org/bbangert/routes/issue/55/cache_hostinfo-breaks-on-https-off |
871fc591 E |
174 | if environ.get('HTTPS', '').lower() == 'off': |
175 | environ.pop('HTTPS') | |
176 | ||
3d0557bf | 177 | ## Attach utilities to the request object |
3d0557bf | 178 | # Do we really want to load this via middleware? Maybe? |
b0ee3aae | 179 | session_manager = self.session_manager |
c7424612 | 180 | request.session = session_manager.load_session_from_cookie(request) |
3d0557bf CAW |
181 | # Attach self as request.app |
182 | # Also attach a few utilities from request.app for convenience? | |
183 | request.app = self | |
0c8a30e6 | 184 | |
3d0557bf CAW |
185 | request.db = self.db |
186 | request.staticdirect = self.staticdirector | |
187 | ||
1ec7ff2a JW |
188 | request.locale = translate.get_locale_from_request(request) |
189 | request.template_env = template.get_jinja_env( | |
190 | self.template_loader, request.locale) | |
7742dcc1 JW |
191 | |
192 | def build_proxy(endpoint, **kw): | |
193 | try: | |
194 | qualified = kw.pop('qualified') | |
195 | except KeyError: | |
196 | qualified = False | |
197 | ||
198 | return map_adapter.build( | |
199 | endpoint, | |
200 | values=dict(**kw), | |
201 | force_external=qualified) | |
202 | ||
203 | request.urlgen = build_proxy | |
204 | ||
5101c469 | 205 | # Log user out if authentication_disabled |
c9dec8b3 RE |
206 | no_auth_logout(request) |
207 | ||
8ce8faaf RE |
208 | mg_request.setup_user_in_request(request) |
209 | ||
f7a5c7c7 | 210 | request.controller_name = None |
1ec7ff2a | 211 | try: |
05501c57 | 212 | found_rule, url_values = map_adapter.match(return_rule=True) |
1ec7ff2a | 213 | request.matchdict = url_values |
fd61aac7 SS |
214 | except RequestRedirect as response: |
215 | # Deal with 301 responses eg due to missing final slash | |
216 | return response(environ, start_response) | |
1ec7ff2a | 217 | except HTTPException as exc: |
785b287f SS |
218 | # Stop and render exception |
219 | return render_http_exception( | |
220 | request, exc, | |
221 | exc.get_description(environ))(environ, start_response) | |
1ec7ff2a | 222 | |
05501c57 | 223 | controller = endpoint_to_controller(found_rule) |
98dacfe6 | 224 | # Make a reference to the controller's symbolic name on the request... |
38103094 | 225 | # used for lazy context modification |
98dacfe6 | 226 | request.controller_name = found_rule.endpoint |
91cf6738 NY |
227 | |
228 | # pass the request through our meddleware classes | |
785b287f SS |
229 | try: |
230 | for m in self.meddleware: | |
231 | response = m.process_request(request, controller) | |
232 | if response is not None: | |
233 | return response(environ, start_response) | |
234 | except HTTPException as e: | |
235 | return render_http_exception( | |
236 | request, e, | |
237 | e.get_description(environ))(environ, start_response) | |
91cf6738 | 238 | |
b1fbf67e CAW |
239 | request = hook_transform("modify_request", request) |
240 | ||
31a8ff42 CAW |
241 | request.start_response = start_response |
242 | ||
785b287f SS |
243 | # get the Http response from the controller |
244 | try: | |
245 | response = controller(request) | |
246 | except HTTPException as e: | |
247 | response = render_http_exception( | |
248 | request, e, e.get_description(environ)) | |
0c8a30e6 | 249 | |
785b287f SS |
250 | # pass the response through the meddlewares |
251 | try: | |
252 | for m in self.meddleware[::-1]: | |
253 | m.process_response(request, response) | |
254 | except HTTPException as e: | |
6a28bc4e | 255 | response = render_http_exception( |
785b287f | 256 | request, e, e.get_description(environ)) |
0c8a30e6 | 257 | |
b0ee3aae E |
258 | session_manager.save_session_to_cookie(request.session, |
259 | request, response) | |
c7424612 | 260 | |
e824570a E |
261 | return response(environ, start_response) |
262 | ||
263 | def __call__(self, environ, start_response): | |
264 | ## If more errors happen that look like unclean sessions: | |
265 | # self.db.check_session_clean() | |
266 | ||
2bc8ff0d | 267 | try: |
e824570a E |
268 | return self.call_backend(environ, start_response) |
269 | finally: | |
270 | # Reset the sql session, so that the next request | |
271 | # gets a fresh session | |
2bc8ff0d | 272 | self.db.reset_after_request() |
31a8ff42 CAW |
273 | |
274 | ||
5784c4e9 | 275 | def paste_app_factory(global_config, **app_config): |
91903aa6 CAW |
276 | configs = app_config['config'].split() |
277 | mediagoblin_config = None | |
278 | for config in configs: | |
279 | if os.path.exists(config) and os.access(config, os.R_OK): | |
280 | mediagoblin_config = config | |
281 | break | |
282 | ||
283 | if not mediagoblin_config: | |
284 | raise IOError("Usable mediagoblin config not found.") | |
19baab1b | 285 | del app_config['config'] |
91903aa6 CAW |
286 | |
287 | mgoblin_app = MediaGoblinApp(mediagoblin_config) | |
19baab1b BP |
288 | mgoblin_app.call_backend = SharedDataMiddleware(mgoblin_app.call_backend, |
289 | exports=app_config) | |
c5d8d301 | 290 | mgoblin_app = hook_transform('wrap_wsgi', mgoblin_app) |
f3f53028 | 291 | |
c4d71564 | 292 | return mgoblin_app |
227a81b5 CAW |
293 | |
294 | ||
295 | def paste_server_selector(wsgi_app, global_config=None, **app_config): | |
296 | """ | |
297 | Select between gunicorn and paste depending on what ia available | |
298 | """ | |
299 | # See if we can import the gunicorn server... | |
300 | # otherwise we'll use the paste server | |
301 | try: | |
302 | import gunicorn | |
303 | except ImportError: | |
304 | gunicorn = None | |
305 | ||
306 | if gunicorn is None: | |
307 | # use paste | |
308 | from paste.httpserver import server_runner | |
309 | ||
310 | cleaned_app_config = dict( | |
311 | [(key, app_config[key]) | |
312 | for key in app_config | |
313 | if key in ["host", "port", "handler", "ssl_pem", "ssl_context", | |
314 | "server_version", "protocol_version", "start_loop", | |
315 | "daemon_threads", "socket_timeout", "use_threadpool", | |
316 | "threadpool_workers", "threadpool_options", | |
317 | "request_queue_size"]]) | |
318 | ||
319 | return server_runner(wsgi_app, global_config, **cleaned_app_config) | |
320 | else: | |
321 | # use gunicorn | |
322 | from gunicorn.app.pasterapp import PasterServerApplication | |
323 | return PasterServerApplication(wsgi_app, global_config, **app_config) |