1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
20 from mediagoblin
.routing
import get_url_map
21 from mediagoblin
.tools
.routing
import endpoint_to_controller
23 from werkzeug
.wrappers
import Request
24 from werkzeug
.exceptions
import HTTPException
25 from werkzeug
.routing
import RequestRedirect
26 from werkzeug
.wsgi
import SharedDataMiddleware
28 from mediagoblin
import meddleware
, __version__
29 from mediagoblin
.db
.util
import check_db_up_to_date
30 from mediagoblin
.tools
import common
, session
, translate
, template
31 from mediagoblin
.tools
.response
import render_http_exception
32 from mediagoblin
.tools
.theme
import register_themes
33 from mediagoblin
.tools
import request
as mg_request
34 from mediagoblin
.media_types
.tools
import media_type_warning
35 from mediagoblin
.mg_globals
import setup_globals
36 from mediagoblin
.init
.celery
import setup_celery_from_config
37 from mediagoblin
.init
.plugins
import setup_plugins
38 from mediagoblin
.init
import (get_jinja_loader
, get_staticdirector
,
39 setup_global_and_app_config
, setup_locales
, setup_workbench
, setup_database
,
41 from mediagoblin
.tools
.pluginapi
import PluginManager
, hook_transform
42 from mediagoblin
.tools
.crypto
import setup_crypto
43 from mediagoblin
.auth
.tools
import check_auth_enabled
, no_auth_logout
46 _log
= logging
.getLogger(__name__
)
49 class MediaGoblinApp(object):
51 WSGI application of MediaGoblin
53 ... this is the heart of the program!
55 def __init__(self
, config_path
, setup_celery
=True):
57 Initialize the application based on a configuration file.
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
65 _log
.info("GNU MediaGoblin %s main server starting", __version__
)
66 _log
.debug("Using config file %s", config_path
)
71 # Open and setup the config
72 global_config
, app_config
= setup_global_and_app_config(config_path
)
78 ##########################################
79 # Setup other connections / useful objects
80 ##########################################
82 # Setup Session Manager, not needed in celery
83 self
.session_manager
= session
.SessionManager()
85 # load all available locales
88 # Set up plugins -- need to do this early so that plugins can
90 _log
.info("Setting up plugins.")
94 self
.db
= setup_database(app_config
['run_migrations'])
96 # Quit app if need to run dbupdate
100 self
.theme_registry
, self
.current_theme
= register_themes(app_config
)
102 # Get the template environment
103 self
.template_loader
= get_jinja_loader(
104 app_config
.get('local_templates'),
106 PluginManager().get_template_paths()
109 # Check if authentication plugin is enabled and respond accordingly.
110 self
.auth
= check_auth_enabled()
112 app_config
['allow_comments'] = False
114 # Set up storage systems
115 self
.public_store
, self
.queue_store
= setup_storage()
118 self
.url_map
= get_url_map()
120 # set up staticdirector tool
121 self
.staticdirector
= get_staticdirector(app_config
)
123 # Setup celery, if appropriate
124 if setup_celery
and not app_config
.get('celery_setup_elsewhere'):
125 if os
.environ
.get('CELERY_ALWAYS_EAGER', 'false').lower() == 'true':
126 setup_celery_from_config(
127 app_config
, global_config
,
128 force_celery_always_eager
=True)
130 setup_celery_from_config(app_config
, global_config
)
132 #######################################################
133 # Insert appropriate things into mediagoblin.mg_globals
135 # certain properties need to be accessed globally eg from
136 # validators, etc, which might not access to the request
138 #######################################################
140 setup_globals(app
=self
)
142 # Workbench *currently* only used by celery, so this only
143 # matters in always eager mode :)
146 # instantiate application meddleware
147 self
.meddleware
= [common
.import_component(m
)(self
)
148 for m
in meddleware
.ENABLED_MEDDLEWARE
]
150 def call_backend(self
, environ
, start_response
):
151 request
= Request(environ
)
153 # Compatibility with django, use request.args preferrably
154 request
.GET
= request
.args
156 ## Routing / controller loading stuff
157 map_adapter
= self
.url_map
.bind_to_environ(request
.environ
)
159 # By using fcgi, mediagoblin can run under a base path
160 # like /mediagoblin/. request.path_info contains the
161 # path inside mediagoblin. If the something needs the
162 # full path of the current page, that should include
164 # Note: urlgen and routes are fine!
165 request
.full_path
= environ
["SCRIPT_NAME"] + request
.path
166 # python-routes uses SCRIPT_NAME. So let's use that too.
167 # The other option would be:
168 # request.full_path = environ["SCRIPT_URL"]
170 # Fix up environ for urlgen
171 # See bug: https://bitbucket.org/bbangert/routes/issue/55/cache_hostinfo-breaks-on-https-off
172 if environ
.get('HTTPS', '').lower() == 'off':
175 ## Attach utilities to the request object
176 # Do we really want to load this via middleware? Maybe?
177 session_manager
= self
.session_manager
178 request
.session
= session_manager
.load_session_from_cookie(request
)
179 # Attach self as request.app
180 # Also attach a few utilities from request.app for convenience?
184 request
.staticdirect
= self
.staticdirector
186 request
.locale
= translate
.get_locale_from_request(request
)
187 request
.template_env
= template
.get_jinja_env(
188 self
.template_loader
, request
.locale
)
190 def build_proxy(endpoint
, **kw
):
192 qualified
= kw
.pop('qualified')
196 return map_adapter
.build(
199 force_external
=qualified
)
201 request
.urlgen
= build_proxy
203 # Log user out if authentication_disabled
204 no_auth_logout(request
)
206 mg_request
.setup_user_in_request(request
)
208 request
.controller_name
= None
210 found_rule
, url_values
= map_adapter
.match(return_rule
=True)
211 request
.matchdict
= url_values
212 except RequestRedirect
as response
:
213 # Deal with 301 responses eg due to missing final slash
214 return response(environ
, start_response
)
215 except HTTPException
as exc
:
216 # Stop and render exception
217 return render_http_exception(
219 exc
.get_description(environ
))(environ
, start_response
)
221 controller
= endpoint_to_controller(found_rule
)
222 # Make a reference to the controller's symbolic name on the request...
223 # used for lazy context modification
224 request
.controller_name
= found_rule
.endpoint
226 # pass the request through our meddleware classes
228 for m
in self
.meddleware
:
229 response
= m
.process_request(request
, controller
)
230 if response
is not None:
231 return response(environ
, start_response
)
232 except HTTPException
as e
:
233 return render_http_exception(
235 e
.get_description(environ
))(environ
, start_response
)
237 request
= hook_transform("modify_request", request
)
239 request
.start_response
= start_response
241 # get the Http response from the controller
243 response
= controller(request
)
244 except HTTPException
as e
:
245 response
= render_http_exception(
246 request
, e
, e
.get_description(environ
))
248 # pass the response through the meddlewares
250 for m
in self
.meddleware
[::-1]:
251 m
.process_response(request
, response
)
252 except HTTPException
as e
:
253 response
= render_http_exception(
254 request
, e
, e
.get_description(environ
))
256 session_manager
.save_session_to_cookie(request
.session
,
259 return response(environ
, start_response
)
261 def __call__(self
, environ
, start_response
):
262 ## If more errors happen that look like unclean sessions:
263 # self.db.check_session_clean()
266 return self
.call_backend(environ
, start_response
)
268 # Reset the sql session, so that the next request
269 # gets a fresh session
270 self
.db
.reset_after_request()
273 def paste_app_factory(global_config
, **app_config
):
274 configs
= app_config
['config'].split()
275 mediagoblin_config
= None
276 for config
in configs
:
277 if os
.path
.exists(config
) and os
.access(config
, os
.R_OK
):
278 mediagoblin_config
= config
281 if not mediagoblin_config
:
282 raise IOError("Usable mediagoblin config not found.")
283 del app_config
['config']
285 mgoblin_app
= MediaGoblinApp(mediagoblin_config
)
286 mgoblin_app
.call_backend
= SharedDataMiddleware(mgoblin_app
.call_backend
,
288 mgoblin_app
= hook_transform('wrap_wsgi', mgoblin_app
)