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