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