Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make ms configure woome automatically

  • Loading branch information...
commit 9f9713b0bced0ae23b2376f4720043a8ad3ffccc 1 parent 3e7368d
nferrier authored
Showing with 76 additions and 29 deletions.
  1. +76 −29 ms.py
105 ms.py
View
@@ -1,59 +1,106 @@
+"""
+MultiServer
+
+This is a simple wsgi handler for Spawning that does virtual host
+handling across a directory of python modules.
+
+This can use a config variable wsgi_path to find the modules. The path
+can be specified in a config file: ~/.mswsgi.conf
+
+ [Server]
+ wsgi_path = ...
+
+The python module loaded is right now, always: server.spawnwoome
+"""
import re
import os
import traceback
from os.path import join as joinpath
-PATH="/home/app/hgrepos/repos"
-
def dispatch(path, environ, start_response):
"""Dispatch the wsgi call to the specified directory"""
- #import pdb
- #pdb.set_trace()
import sys
sys.path += [joinpath(path, "woome")]
- wsgihandler_maker = __import__('server.spawnwoome')
- app = wsgihandler_maker.spawnwoome.getapp()
- return app(environ, start_response)
-
-def multiwsgidispatch(environ, start_response):
- host = environ["HTTP_HOST"]
- targetpart = host.split(".")[0]
- # Make a regex that will match against targets
- target_re = re.compile("%s.*" % re.sub("-", "[_-]", targetpart))
- for e in os.listdir(PATH):
- if target_re.match(e):
- try:
- return dispatch(joinpath(PATH, e), environ, start_response)
- except Exception,e:
- print e
- traceback.print_exc()
- start_response('500 Error', [('content-type', 'text/html')])
- return ["<p>Error: %s</p>" % e]
-
-
- # Otherwise it's an error
- start_response('500 Error', [('content-type', 'text/html')])
- return ["<p>No target found for %s</p>" % host]
+
+ # We have to do this ONLY because I want to monkeypatch settings
+ import config
+ config.setup_django_env()
+ import settings
+ try:
+ sys.path = [settings.DJANGO_PATH_DIR] + sys.path
+ except AttributeError:
+ pass
+
+ from os.path import abspath, basename, dirname
+ reponame = basename(path).replace("_", "-")
+
+ settings.STATIC_URL = 'http://%s.repos.dev.woome.com' % reponame
+ settings.IMG_URL = 'http://%s.repos.dev.woome.com' % reponame
+
+ import server.spawnwoome
+ wsgi_handler = server.spawnwoome.getapp()
+ return wsgi_handler(environ, start_response)
+
+def multiwsgidispatch(conf):
+ """Get a wsgi handler to do multiple dispatch.
+
+ The handler uses the Host header to try to find a matching wsgi
+ instance in the config['wsgi_path']
+ """
+ # Capture the config
+ config = conf
+ def wsgi_dispatcher(environ, start_response):
+ """Virtual host WSGI dispatcher"""
+ wsgi_path = config.get("wsgi_path")
+ host = environ["HTTP_HOST"]
+ targetpart = host.split(".")[0]
+ # Make a regex that will match against targets
+ target_re = re.compile("%s.*" % re.sub("-", "[_-]", targetpart))
+ for entry in os.listdir(wsgi_path):
+ if target_re.match(entry):
+ try:
+ path = joinpath(wsgi_path, entry)
+ return dispatch(path, environ, start_response)
+ except Exception,e:
+ start_response('500 Error', [('content-type', 'text/html')])
+ return ["<p>Error: %s</p>" % e]
+
+ # Otherwise it's an error
+ start_response('500 Error', [('content-type', 'text/html')])
+ return ["<p>No target found for %s</p>" % host]
+
+ return wsgi_dispatcher
+
+
+# Config stuff
+from ConfigParser import ConfigParser
+from os.path import expanduser
### Spawning stuff
### Start this under spawning like:
### spawn -p 8110 -f ms.spawning_config_factory none
-### this interface needs significant work
def app_factory(conf):
- return multiwsgidispatch
+ return multiwsgidispatch(conf)
def spawning_config_factory(args):
"""A Spawning config factory"""
+ conf = ConfigParser()
+ try:
+ conf.read(expanduser("~/.mswsgi.conf"))
+ except:
+ pass
+
return {
'args': args,
'host': args.get('host'),
'port': args.get('port'),
'app_factory': "ms.app_factory",
'app': "",
+ 'wsgi_path': conf.get("Server", "wsgi_path"),
'deadman_timeout': 10,
'num_processes': 4,
}
Please sign in to comment.
Something went wrong with that request. Please try again.