Merge remote branch 'remotes/elrond/dev/init'
[mediagoblin.git] / mediagoblin / app.py
CommitLineData
8e1e744d 1# GNU MediaGoblin -- federated, autonomous media hosting
e5572c60
ML
2# Copyright (C) 2011 Free Software Foundation, Inc
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 17import os
31a8ff42
CAW
18import urllib
19
31a8ff42 20import routes
b61874b2 21from webob import Request, exc
31a8ff42 22
c85c9dc7 23from mediagoblin import routing, util, storage
6e7ce8d1 24from mediagoblin.mg_globals import setup_globals
073b61fe 25from mediagoblin.init.celery import setup_celery_from_config
fe289be4 26from mediagoblin.init import get_jinja_loader, get_staticdirector, \
3f4b5e4a 27 setup_global_and_app_config, setup_workbench, setup_database
90e342f9 28
31a8ff42 29
8e1e744d 30class MediaGoblinApp(object):
31a8ff42 31 """
3f5cf663
CAW
32 WSGI application of MediaGoblin
33
34 ... this is the heart of the program!
31a8ff42 35 """
3f5cf663
CAW
36 def __init__(self, config_path, setup_celery=True):
37 """
38 Initialize the application based on a configuration file.
39
40 Arguments:
41 - config_path: path to the configuration file we're opening.
42 - setup_celery: whether or not to setup celery during init.
43 (Note: setting 'celery_setup_elsewhere' also disables
44 setting up celery.)
45 """
46 ##############
47 # Setup config
48 ##############
49
50 # Open and setup the config
fe289be4 51 global_config, app_config = setup_global_and_app_config(config_path)
3f5cf663
CAW
52
53 ##########################################
54 # Setup other connections / useful objects
55 ##########################################
56
57 # Set up the database
3f4b5e4a 58 self.connection, self.db = setup_database()
ff94114c 59
5afdd7a1 60 # Get the template environment
42ef819c 61 self.template_loader = get_jinja_loader(
3f5cf663 62 app_config.get('user_template_path'))
5afdd7a1
CAW
63
64 # Set up storage systems
3c7d11ff 65 self.public_store = storage.storage_system_from_config(
3f5cf663 66 app_config, 'publicstore')
3c7d11ff 67 self.queue_store = storage.storage_system_from_config(
3f5cf663 68 app_config, 'queuestore')
5afdd7a1
CAW
69
70 # set up routing
0f63a944 71 self.routing = routing.get_mapper()
31a8ff42 72
582c4d5f 73 # set up staticdirector tool
c85c9dc7 74 self.staticdirector = get_staticdirector(app_config)
3f5cf663
CAW
75
76 # Setup celery, if appropriate
77 if setup_celery and not app_config.get('celery_setup_elsewhere'):
78 if os.environ.get('CELERY_ALWAYS_EAGER'):
79 setup_celery_from_config(
80 app_config, global_config,
81 force_celery_always_eager=True)
82 else:
83 setup_celery_from_config(app_config, global_config)
84
85 #######################################################
86 # Insert appropriate things into mediagoblin.mg_globals
87 #
df9809c2
CAW
88 # certain properties need to be accessed globally eg from
89 # validators, etc, which might not access to the request
90 # object.
3f5cf663
CAW
91 #######################################################
92
df9809c2 93 setup_globals(
becb77ee 94 app=self,
df9809c2 95 public_store=self.public_store,
7664b4db 96 queue_store=self.queue_store)
1fd97db3
CAW
97
98 # Workbench *currently* only used by celery, so this only
99 # matters in always eager mode :)
7664b4db 100 setup_workbench()
df9809c2 101
31a8ff42
CAW
102 def __call__(self, environ, start_response):
103 request = Request(environ)
104 path_info = request.path_info
582c4d5f
CAW
105
106 ## Routing / controller loading stuff
0f63a944 107 route_match = self.routing.match(path_info)
31a8ff42
CAW
108
109 # No matching page?
110 if route_match is None:
111 # Try to do see if we have a match with a trailing slash
112 # added and if so, redirect
113 if not path_info.endswith('/') \
114 and request.method == 'GET' \
0f63a944 115 and self.routing.match(path_info + '/'):
31a8ff42
CAW
116 new_path_info = path_info + '/'
117 if request.GET:
118 new_path_info = '%s?%s' % (
119 new_path_info, urllib.urlencode(request.GET))
1bb0fdf2 120 redirect = exc.HTTPFound(location=new_path_info)
31a8ff42
CAW
121 return request.get_response(redirect)(environ, start_response)
122
123 # Okay, no matches. 404 time!
124 return exc.HTTPNotFound()(environ, start_response)
125
cb8ea0fe 126 controller = util.import_component(route_match['controller'])
31a8ff42
CAW
127 request.start_response = start_response
128
582c4d5f 129 ## Attach utilities to the request object
31a8ff42 130 request.matchdict = route_match
0f63a944 131 request.urlgen = routes.URLGenerator(self.routing, environ)
7846e406 132 # Do we really want to load this via middleware? Maybe?
14ba9383 133 request.session = request.environ['beaker.session']
0dd65945
CAW
134 # Attach self as request.app
135 # Also attach a few utilities from request.app for convenience?
136 request.app = self
0e0e3d9a
CAW
137 request.locale = util.get_locale_from_request(request)
138
139 request.template_env = util.get_jinja_env(
140 self.template_loader, request.locale)
0dd65945
CAW
141 request.db = self.db
142 request.staticdirect = self.staticdirector
31a8ff42 143
ddff7cce
CAW
144 util.setup_user_in_request(request)
145
31a8ff42
CAW
146 return controller(request)(environ, start_response)
147
148
5784c4e9 149def paste_app_factory(global_config, **app_config):
3f5cf663 150 mgoblin_app = MediaGoblinApp(app_config['config'])
b61874b2 151
c4d71564 152 return mgoblin_app