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