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