Apologies, in trying to zero in on my description, I've not only
become obscure, but also come up with answers and more questions along
the way!
Now that I have a better handle on how I want to use my cherrypy-based
server and the details of my web app code, I've realized that I don't
need to specify static directories in the configuration (more on that
later).
I've definitely taken the time to understand how cherrypy maps paths
to the instances I provide (a graph of objects). But of course, this
presumes that you have defined this graph somewhere. Which won't at
all be the case for me as that implies some sort of map that needs to
be maintained. Knowing how cherrypy checks its options before giving
a 404, what I appear to want is a single class which will perform all
my standard response-building operations based on the URL.
Understanding that this is what I wanted was likely my first point of
confusion as I'm new to cherrypy. I'm sure where we can all see where
this is going now though, I'm making an MVC! *groan*
What I've gotten started with so far is:
o Devising my own "web app" structure and starting my web server in
the web app's directory.
o Adding the "Controllers" subdirectory of the cwd to the python
search path.
o Adding a "Static" subdirectory of the cwd to be hosted as "http://
host/Static".
o Create the handler class which is an object with a default(self,
*args, **kwargs) method defined. I assign this as the root to capture
all unhandled requests.
o In my handler, I can infer everything about the path requested.
- Load the according module in the "Controllers" subdirectory,
containing a like-named class and instantiate the class.
- Use a similar method to pick a template (once I've settled
on a templating engine).
- Invoke the method on the class, passing a reference to the
template and database.
- Invoke the template's render method to generate output.
- (gracefully handle missing controllers or templates allowing
for template-less or controller-less responses)
So I guess my current questions now are:
o How do I instruct cherrypy through code (and not its configuration
file) to statically host a directory in it's entirety?
o Is there a way to tell cherrypy to continue it's normal routine for
handling on one of my controller classes that have been dynamically
loaded above? Or am I forced to sniff out and invoke the methods
myself at this point (not a pain, just avoiding reinventing the
wheel)?
I'll try to preempt at least two questions...
o With dynamic module loading, I'm restricting it to modules found in
the Controllers dir, so hopfully this doesn't raise anyones' security
hairs. It will always be jailed to "./Controllers/Hahaha.py".
o "Why?" or "Why not use rails?" For the purposes of what I plan on
using this application engine for, I'm interested in using zodb, which
is python only. Coming from PHP, cherrypy seemed like a natural fit
for me given it's very tight abstraction of the HTTP request/response
lifecycle. I'm simply trying to shore it up with an easy and portable
web application structure based on all the different components I'm
gluing together.
Actually, to be honest, I'm floored by how potent cherrypy is in this
regard. If my current approach is sane, cherrypy makes URL rewriters
look as complex and nuanced as JCL! ;)
If I've missed anything, I'll post it after this response. Hopefully
this gives you a better idea of what I've got swirling around in my
head!
On Apr 20, 4:16 am, steve <
st...@lonetwin.net> wrote:
> Hi,
> On 04/19/2010 11:16 PM, Omega wrote:> I don't think the 404 I'm getting is browser related, both Chromium
> > and Firefox are giving me the same result. I do have firebug
> > installed, but nothing seems amiss here with the browsers. Looking at
> > cherrypy's debug outout, I can see that it is emitting the 404:
>
> > 127.0.0.1 - - [19/Apr/2010:12:39:49] "GET /CSS HTTP/1.1" 404 1096 ""
> > "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/533.6 (KHTML,
> > like Gecko) Chrome/5.0.379.0 Safari/533.6"
>
> > As I'm still new to cherrypy, I'm not sure if the error it's giving is
> > meaningful or not? It's saying the path "/CSS" was not found, but as
> > I've defined a config file, this doesn't make sense.
>
> What happens when you request for the file within '/CSS' rather than '/CSS' itself ?
>
> I am willing to bet that you will get that file. By default, cherrypy will not
> serve up the directory index if you request for a staticdir path (how is it
> supposed to know that's what it is supposed to do ?). If you do need directory
> indexes here is how to do it:
>
>
http://tools.cherrypy.org/wiki/staticdirindex
>
> An alternate way would be to define a 'default' method:
http://www.cherrypy.org/wiki/CherryPyTutorial#Partialmatchesandthedef...