Skip to content

Commit

Permalink
Try to load an app by name when mounts are empty
Browse files Browse the repository at this point in the history
If a config file does not specify a mounts dictionary, greins will now
try to load the application by looking for a module whose name matches
the config file's name and looking for a symbol within called
'application' that is a callable WSGI handler.

This change means that developers can use this naming convention to
seamlessly import apps into greins without changing any files.
  • Loading branch information
Randall Leeds committed Oct 11, 2011
1 parent dbdbe01 commit 3d9dd6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions README.rst
Expand Up @@ -54,9 +54,15 @@ are valid in these configuration files and work as in Gunicorn_. Other options,
such as logging and worker configuration, are ignored and should be configured such as logging and worker configuration, are ignored and should be configured
globally for the Greins application. globally for the Greins application.


As a convenience, a file name ``myapp`` which does not define any ``mounts``
will try to import ``myapp:application`` and mount it underneath the path
``/myapp``.

It should be possible to write an application for Gunicorn_ and then place It should be possible to write an application for Gunicorn_ and then place
the application's configuration inside the configuration directory for Greins a Gunicorn_ configuration file inside the configuration directory for Greins
to begin using it within Greins immediately. to begin using it within Greins immediately, provided that the config file name
matches a module in the python path which exports ``application`` as a WSGI_
callable.


Examples Examples
++++++++ ++++++++
Expand Down
6 changes: 6 additions & 0 deletions greins/app.py
Expand Up @@ -8,6 +8,7 @@


from gunicorn.app.wsgiapp import WSGIApplication from gunicorn.app.wsgiapp import WSGIApplication
from gunicorn.config import make_settings from gunicorn.config import make_settings
from gunicorn.util import import_app


from greins.reloader import Reloader from greins.reloader import Reloader
from greins.router import Router from greins.router import Router
Expand Down Expand Up @@ -88,6 +89,11 @@ def load_file(self, cf):
self.logger.info("Loading configuration for %s" % cf_name) self.logger.info("Loading configuration for %s" % cf_name)
execfile(cf, cfg, cfg) execfile(cf, cfg, cfg)


# By default, try to mount the application by name
if not cfg['mounts']:
app_name, ext = os.path.splitext(os.path.basename(cf))
cfg['mounts'][app_name] = import_app(app_name)

# Load all the mount points # Load all the mount points
for r, a in cfg['mounts'].iteritems(): for r, a in cfg['mounts'].iteritems():
# Capture the handler in a closure # Capture the handler in a closure
Expand Down

0 comments on commit 3d9dd6f

Please sign in to comment.