1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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/>.
21 from webob
import Request
, exc
23 from mediagoblin
import routing
, util
, models
, storage
, staticdirect
24 from mediagoblin
.globals import setup_globals
27 class Error(Exception): pass
28 class ImproperlyConfigured(Error
): pass
31 class MediaGoblinApp(object):
33 Really basic wsgi app using routes and WebOb.
35 def __init__(self
, connection
, database_path
,
36 public_store
, queue_store
,
38 user_template_path
=None):
39 # Get the template environment
40 self
.template_env
= util
.get_jinja_env(user_template_path
)
42 # Set up storage systems
43 self
.public_store
= public_store
44 self
.queue_store
= queue_store
47 self
.connection
= connection
48 self
.db
= connection
[database_path
]
49 models
.register_models(connection
)
52 self
.routing
= routing
.get_mapper()
54 # set up staticdirector tool
55 self
.staticdirector
= staticdirector
57 # certain properties need to be accessed globally eg from
58 # validators, etc, which might not access to the request
61 db_connection
=connection
,
63 public_store
=self
.public_store
,
64 queue_store
=self
.queue_store
)
66 def __call__(self
, environ
, start_response
):
67 request
= Request(environ
)
68 path_info
= request
.path_info
70 ## Routing / controller loading stuff
71 route_match
= self
.routing
.match(path_info
)
74 if route_match
is None:
75 # Try to do see if we have a match with a trailing slash
76 # added and if so, redirect
77 if not path_info
.endswith('/') \
78 and request
.method
== 'GET' \
79 and self
.routing
.match(path_info
+ '/'):
80 new_path_info
= path_info
+ '/'
82 new_path_info
= '%s?%s' % (
83 new_path_info
, urllib
.urlencode(request
.GET
))
84 redirect
= exc
.HTTPTemporaryRedirect(location
=new_path_info
)
85 return request
.get_response(redirect
)(environ
, start_response
)
87 # Okay, no matches. 404 time!
88 return exc
.HTTPNotFound()(environ
, start_response
)
90 controller
= util
.import_component(route_match
['controller'])
91 request
.start_response
= start_response
93 ## Attach utilities to the request object
94 request
.matchdict
= route_match
95 request
.urlgen
= routes
.URLGenerator(self
.routing
, environ
)
96 # Do we really want to load this via middleware? Maybe?
97 request
.session
= request
.environ
['beaker.session']
98 # Attach self as request.app
99 # Also attach a few utilities from request.app for convenience?
101 request
.template_env
= self
.template_env
103 request
.staticdirect
= self
.staticdirector
105 util
.setup_user_in_request(request
)
107 return controller(request
)(environ
, start_response
)
110 def paste_app_factory(global_config
, **kw
):
111 # Get the database connection
112 connection
= mongokit
.Connection(
113 kw
.get('db_host'), kw
.get('db_port'))
115 # Set up the storage systems.
116 public_store
= storage
.storage_system_from_paste_config(
118 queue_store
= storage
.storage_system_from_paste_config(
121 # Set up the staticdirect system
122 if kw
.has_key('direct_remote_path'):
123 staticdirector
= staticdirect
.RemoteStaticDirect(
124 kw
['direct_remote_path'].strip())
125 elif kw
.has_key('direct_remote_paths'):
126 staticdirector
= staticdirect
.MultiRemoteStaticDirect(
127 dict([line
.strip().split(' ', 1)
128 for line
in kw
['direct_remote_paths'].strip().splitlines()]))
130 raise ImproperlyConfigured(
131 "One of direct_remote_path or direct_remote_paths must be provided")
133 mgoblin_app
= MediaGoblinApp(
134 connection
, kw
.get('db_name', 'mediagoblin'),
135 public_store
=public_store
, queue_store
=queue_store
,
136 staticdirector
=staticdirector
,
137 user_template_path
=kw
.get('local_templates'))