static content serving

35 views
Skip to first unread message

Diez B. Roggisch

unread,
Jul 9, 2008, 10:32:31 AM7/9/08
to turbogea...@googlegroups.com
Hi,

can somebody give me a hint on how static content is served? I need to "embed"
the TG2-app into an existing wsgi-app below a certain path - and obviously
serving e.g. the CSS fails miserably. There was the old way of using tg.url
and server.webpath (or such) - any such facility available?

Diez

Mark Ramm

unread,
Jul 9, 2008, 11:50:43 AM7/9/08
to turbogea...@googlegroups.com
We're using the cascade app from paste:

http://pythonpaste.org/modules/cascade.html

Along with the paste StaticURLParser and StaticJavascripts (from pylons)

It's configured like this.

So configuring this should be as simple as setting up the proper info
in the ini file.

javascripts_app = StaticJavascripts()
static_app = StaticURLParser(config['pylons.paths']['static_files'])
app = Cascade([static_app, javascripts_app, app])

Let me know if we need to expose some more configuration information
to the user app, all of this was changed sort-of-recently when we
cleaned up middleware.py.

--
Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog

aspineux

unread,
Jul 9, 2008, 2:58:42 PM7/9/08
to TurboGears Trunk
It looks about the same as tg1, looks in controllers.py for tg.url

def url(tgpath, tgparams=None, **kw):
"""
url() re-implementation from TG1
"""

if not isinstance(tgpath, basestring):
tgpath = "/".join(list(tgpath))
if tgpath.startswith("/"):
app_root =
pylons.request.application_url[len(pylons.request.host_url):]
tgpath = app_root + tgpath
tgpath = pylons.config.get("server.webpath", "") + tgpath
result = tgpath
else:
result = tgpath
....

>
> Diez

Diez B. Roggisch

unread,
Jul 10, 2008, 5:31:21 AM7/10/08
to turbogea...@googlegroups.com
On Wednesday 09 July 2008 17:50:43 Mark Ramm wrote:
> We're using the cascade app from paste:
>
> http://pythonpaste.org/modules/cascade.html
>
> Along with the paste StaticURLParser and StaticJavascripts (from pylons)
>
> It's configured like this.
>
> So configuring this should be as simple as setting up the proper info
> in the ini file.
>
> javascripts_app = StaticJavascripts()
> static_app =
> StaticURLParser(config['pylons.paths']['static_files']) app =
> Cascade([static_app, javascripts_app, app])
>
> Let me know if we need to expose some more configuration information
> to the user app, all of this was changed sort-of-recently when we
> cleaned up middleware.py.

Ahh! That's interesting - and explains some stuff I've seen while
source-level-debugging. The Cascade is interersting for me...

And of course the current implementation is a bit warty::

# Static files (If running in production, and Apache or another web
# server is handling this static content, remove the following 3
lines)


javascripts_app = StaticJavascripts()
static_app = StaticURLParser(config['pylons.paths']['static_files'])
app = Cascade([static_app, javascripts_app, app])

return app

Obviously we want that configurable.

I will open a ticket & try and install TG2 from SVN, so that I can propose a
solution for this. I guess the best solution would be to have the above code
in <myapplication>/controllers/__init__.py or some such location - or the
config/middleware.py, that might even be better.

Diez

Diez B. Roggisch

unread,
Jul 10, 2008, 5:40:37 AM7/10/08
to turbogea...@googlegroups.com
On Thursday 10 July 2008 11:31:21 Diez B. Roggisch wrote:
> On Wednesday 09 July 2008 17:50:43 Mark Ramm wrote:
> > We're using the cascade app from paste:
> >
> > http://pythonpaste.org/modules/cascade.html
> >
> > Along with the paste StaticURLParser and StaticJavascripts (from pylons)
> >
> > It's configured like this.
> >
> > So configuring this should be as simple as setting up the proper info
> > in the ini file.
> >
> > javascripts_app = StaticJavascripts()
> > static_app =
> > StaticURLParser(config['pylons.paths']['static_files']) app =
> > Cascade([static_app, javascripts_app, app])
> >
> > Let me know if we need to expose some more configuration information
> > to the user app, all of this was changed sort-of-recently when we
> > cleaned up middleware.py.
>

I created a ticket:

http://trac.turbogears.org/ticket/1898#preview

Diez

Diez B. Roggisch

unread,
Jul 10, 2008, 6:30:13 AM7/10/08
to turbogea...@googlegroups.com

The question is: where to set this? I tried

[server:main]

in development.ini, and it puked on me about an unknown argument.

I then stuffed into

[app:main]
server.webpath = /collaborate

also in development.ini - but the access above reading "server.webpath"
doesn't work for me then. The value appears as this:

pylons.config.app_conf.get('server.webpath')

So... where is that supposed to be, and is this a bug?

Diez

aspineux

unread,
Jul 10, 2008, 7:18:50 AM7/10/08
to TurboGears Trunk
Try them all :-)


>
> Diez- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

Diez B. Roggisch

unread,
Jul 10, 2008, 9:23:27 AM7/10/08
to turbogea...@googlegroups.com

Ok, I created a patch for the ticket. It includes rewrites of tg.middleware an
middleware.py_tmpl (so that the app decides on the static rewriting), and
tg.url-enclosing of hopefully all relevant template-urls.

Before I attach that, I have some questions I need to have answered:

- where does the sever.webpath config option actually go? Currently, I've put
it as this in my development.ini::

[app:main]
use = egg:AuthenticationTest
full_stack = true
#lang = ru
cache_dir = %(here)s/data
beaker.session.key = authenticationtest
beaker.session.secret = somesecret
server.webpath = /collaborate

It is accessed using

pylons.config['app_cfg']['sever.webpath']

- how is server.webpath supposed to be used? The question arises because of
the following considerations:

1) if the TG2-app is one of possibly several below a WSGI-app "mounted" to
server.webpath, we need to rewrite the PATH_INFO prior to all requests to
remove the server.webpath. I've done that, and it makes additionally
adjustments to tg.ext.repoze.who necessary (done them also) because of the
generated /login and related urls. These need to be prepended with
server.webpath.

2) if the TG2-app is below server.webpath in an Apache-environment, it
*might* be possible to use mod_rewrite to strip of the path-prefix for us,
thus only the generated urls must

I've done everything for 1). If 2) is an actual scenario we want to work with,
it might be necessary to introduce another config-variable called
server.strip_webpath or so, that controls the adjustment of PATH_INFO.

Diez


Mark Ramm

unread,
Aug 10, 2008, 2:22:45 PM8/10/08
to turbogea...@googlegroups.com
Hmm, sorry I missed this earlier.

I think that we should be relying on SCRIPT_NAME in the environ being
set properly, and it looks like I need to patch tg.url to make that
work right.

When serving behing apache or another server that does the right
thing, the SCRIPT_NAME should automatically be set correctly. When
it's not we'll need to use some little middleware that sets it right
before we get into the tg2 app.

Inside a TG2 app, we can use request.application_url to get the URL
including the host name and SCRIPT_NAME but with no PATH_INFO or query
string.

--

Reply all
Reply to author
Forward
0 new messages