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 |
7742dcc1 | 24 | from werkzeug.exceptions import HTTPException, NotFound |
fd61aac7 | 25 | from werkzeug.routing import RequestRedirect |
31a8ff42 | 26 | |
7742dcc1 | 27 | from mediagoblin import meddleware, __version__ |
c0022ecc | 28 | from mediagoblin.tools import common, 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, |
0533f117 | 37 | setup_storage, setup_beaker_cache) |
05e007c1 | 38 | from mediagoblin.tools.pluginapi import PluginManager |
90e342f9 | 39 | |
31a8ff42 | 40 | |
ec97c937 E |
41 | _log = logging.getLogger(__name__) |
42 | ||
43 | ||
8e1e744d | 44 | class MediaGoblinApp(object): |
31a8ff42 | 45 | """ |
3f5cf663 CAW |
46 | WSGI application of MediaGoblin |
47 | ||
48 | ... this is the heart of the program! | |
31a8ff42 | 49 | """ |
3f5cf663 CAW |
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 | """ | |
ec97c937 | 60 | _log.info("GNU MediaGoblin %s main server starting", __version__) |
3f369674 | 61 | _log.debug("Using config file %s", config_path) |
3f5cf663 CAW |
62 | ############## |
63 | # Setup config | |
64 | ############## | |
65 | ||
66 | # Open and setup the config | |
fe289be4 | 67 | global_config, app_config = setup_global_and_app_config(config_path) |
3f5cf663 CAW |
68 | |
69 | ########################################## | |
70 | # Setup other connections / useful objects | |
71 | ########################################## | |
72 | ||
6ef75af5 SS |
73 | # load all available locales |
74 | setup_locales() | |
75 | ||
29b6f917 WKG |
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 | ||
3f5cf663 | 81 | # Set up the database |
bc142abc | 82 | self.db = setup_database() |
ff94114c | 83 | |
828fc630 | 84 | # Register themes |
975be468 | 85 | self.theme_registry, self.current_theme = register_themes(app_config) |
828fc630 | 86 | |
5afdd7a1 | 87 | # Get the template environment |
42ef819c | 88 | self.template_loader = get_jinja_loader( |
3b47da8e | 89 | app_config.get('local_templates'), |
8545dd50 | 90 | self.current_theme, |
05e007c1 | 91 | PluginManager().get_template_paths() |
8545dd50 | 92 | ) |
0c8a30e6 | 93 | |
5afdd7a1 | 94 | # Set up storage systems |
dccef262 | 95 | self.public_store, self.queue_store = setup_storage() |
5afdd7a1 CAW |
96 | |
97 | # set up routing | |
48cf435d | 98 | self.url_map = get_url_map() |
31a8ff42 | 99 | |
582c4d5f | 100 | # set up staticdirector tool |
c85c9dc7 | 101 | self.staticdirector = get_staticdirector(app_config) |
3f5cf663 | 102 | |
0533f117 CAW |
103 | # set up caching |
104 | self.cache = setup_beaker_cache() | |
105 | ||
3f5cf663 CAW |
106 | # Setup celery, if appropriate |
107 | if setup_celery and not app_config.get('celery_setup_elsewhere'): | |
d9a31a39 | 108 | if os.environ.get('CELERY_ALWAYS_EAGER', 'false').lower() == 'true': |
3f5cf663 CAW |
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 | # | |
df9809c2 CAW |
118 | # certain properties need to be accessed globally eg from |
119 | # validators, etc, which might not access to the request | |
120 | # object. | |
3f5cf663 CAW |
121 | ####################################################### |
122 | ||
243c3843 | 123 | setup_globals(app=self) |
1fd97db3 CAW |
124 | |
125 | # Workbench *currently* only used by celery, so this only | |
126 | # matters in always eager mode :) | |
7664b4db | 127 | setup_workbench() |
df9809c2 | 128 | |
ce5ae8da CAW |
129 | # instantiate application meddleware |
130 | self.meddleware = [common.import_component(m)(self) | |
131 | for m in meddleware.ENABLED_MEDDLEWARE] | |
0c8a30e6 | 132 | |
e824570a | 133 | def call_backend(self, environ, start_response): |
31a8ff42 | 134 | request = Request(environ) |
0c8a30e6 | 135 | |
726896b6 | 136 | # Compatibility with django, use request.args preferrably |
f1d06e1d | 137 | request.GET = request.args |
f1d06e1d | 138 | |
582c4d5f | 139 | ## Routing / controller loading stuff |
7742dcc1 | 140 | map_adapter = self.url_map.bind_to_environ(request.environ) |
31a8ff42 | 141 | |
05788ef4 E |
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! | |
f1d06e1d | 148 | request.full_path = environ["SCRIPT_NAME"] + request.path |
05788ef4 E |
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 | ||
871fc591 | 153 | # Fix up environ for urlgen |
d23d4b23 | 154 | # See bug: https://bitbucket.org/bbangert/routes/issue/55/cache_hostinfo-breaks-on-https-off |
871fc591 E |
155 | if environ.get('HTTPS', '').lower() == 'off': |
156 | environ.pop('HTTPS') | |
157 | ||
3d0557bf | 158 | ## Attach utilities to the request object |
3d0557bf CAW |
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 | |
0c8a30e6 | 164 | |
3d0557bf CAW |
165 | request.db = self.db |
166 | request.staticdirect = self.staticdirector | |
167 | ||
1ec7ff2a JW |
168 | request.locale = translate.get_locale_from_request(request) |
169 | request.template_env = template.get_jinja_env( | |
170 | self.template_loader, request.locale) | |
7742dcc1 JW |
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 | ||
1ec7ff2a JW |
185 | mg_request.setup_user_in_request(request) |
186 | ||
187 | try: | |
05501c57 | 188 | found_rule, url_values = map_adapter.match(return_rule=True) |
1ec7ff2a | 189 | request.matchdict = url_values |
fd61aac7 SS |
190 | except RequestRedirect as response: |
191 | # Deal with 301 responses eg due to missing final slash | |
192 | return response(environ, start_response) | |
1ec7ff2a | 193 | except HTTPException as exc: |
785b287f SS |
194 | # Stop and render exception |
195 | return render_http_exception( | |
196 | request, exc, | |
197 | exc.get_description(environ))(environ, start_response) | |
1ec7ff2a | 198 | |
05501c57 | 199 | controller = endpoint_to_controller(found_rule) |
91cf6738 NY |
200 | |
201 | # pass the request through our meddleware classes | |
785b287f SS |
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) | |
91cf6738 | 211 | |
31a8ff42 CAW |
212 | request.start_response = start_response |
213 | ||
785b287f SS |
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)) | |
0c8a30e6 | 220 | |
785b287f SS |
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)) | |
0c8a30e6 | 228 | |
e824570a E |
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 | ||
2bc8ff0d | 235 | try: |
e824570a E |
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 | |
2bc8ff0d | 240 | self.db.reset_after_request() |
31a8ff42 CAW |
241 | |
242 | ||
5784c4e9 | 243 | def paste_app_factory(global_config, **app_config): |
91903aa6 CAW |
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) | |
b61874b2 | 255 | |
c4d71564 | 256 | return mgoblin_app |