Hi Kevin,

First, great work on TG.  It's great to see so many Python projects
being leveraged for their strengths.

Kevin Dangoor wrote:

[SNIP]

> 2) Static files and other filter configuration
>
> Static files are configured in the config file by specifying the path
> to apply the static filter to. The paths in these configurations would
> need to change to take into account where the root of the application
> is.
>
> This is tricky. The current version of the URL class computes the
> application root based on the path traversed to get there. When you
> instantiate blog1 above, there is no way for it to know that its path
> (for CherryPy purposes) is /users/blog1 and that /users/blog1/static
> should be configured for serving up static files.
>
> It's easy enough to tell CherryPy to use a static filter via
> _cpFilterList. The problem is that the static filter needs its
> configuration set up for the correct path. But that path is not known.
>
> You *could* just put the static paths into your final config file
> manually, but that's ugly. Plus, it doesn't work if the Blog instances
> are created dynamically (from a database, for example).
>
> If there is some reasonable way to figure out the path for the sake of
> the configuration, then this problem will go away. It makes me think
> of acquisition from Zope (you basically want to acquire the
> application root and its path).

Well, you could use the staticFilter.match config option and define a
regular expression that matches where you want to serve your static
files from.  Here is a silly example:

import cherrypy

class MainSite(object):
    @cherrypy.expose
    def index(self):
        return "<a href='users'>users</a>"

class UsersSite(object):
    @cherrypy.expose
    def index(self):
        return "add a username to the path"

    @cherrypy.expose
    def default(self, username):
        return "welcome to %s's site" % username

cherrypy.root = MainSite()
cherrypy.root.users = UsersSite()

cherrypy.config.update({'/users':
                        {'staticFilter.on':True,
                         'staticFilter.dir':'users',
                         'staticFilter.match':'users\/.*\/.+'}
                        })

cherrypy.server.start()


You will need to create a "users" folder at the same level that you
save that script and add a subfolder for each user.  Any static files
can be tossed in there. What this demonstrates (other than my lack of
regular expression skills) is how one could potentially access static
dirs for users that are dynamically generated.

A request for http://localhost:8080/users/kevin will return the generic
default method text.  It could instead serve some stuff up from a
database.  Anyhow, if a users/kevin/message.txt file were created,
http://localhost:8080/users/kevin/message.txt would return that file,
served by the staticfilter.

Just some thoughts.

Christian
http://www.dowski.com

Reply via email to