Commit ad44d528 authored by Robin Schoonover's avatar Robin Schoonover
Browse files

Add --script-name flag.

parent ac811ed3
......@@ -8,6 +8,7 @@ def main():
parser.add_argument('--host', default='localhost')
parser.add_argument('--port', default=8080, type=int)
parser.add_argument('--call', action='store_true', dest='call_app')
parser.add_argument('--script-name')
parser.add_argument('wsgiapp')
args = parser.parse_args()
......@@ -15,7 +16,8 @@ def main():
server = Server(args.wsgiapp,
host=args.host,
port=args.port,
call_app=args.call_app)
call_app=args.call_app,
script_name=args.script_name)
server.run_forever()
if __name__=='__main__':
......
......@@ -17,13 +17,31 @@ class FailedApp(object):
start_response('500 Internal Server Error', [('Content-Type', 'text/plain')])
return [b"Failed: ", self.msg.encode('utf-8'), b"\n"]
class Server(object):
def __init__(self, app, host="0.0.0.0", port=80, call_app=False):
class ScriptNameApp(object):
def __init__(self, app, script_name):
self.app = app
self.host = host
self.port = port
self.call_app = call_app
self.script_name = script_name
def __call__(self, environ, start_response):
sn = environ.get('SCRIPT_NAME', '')
pi = environ.get('PATH_INFO', '')
if pi.startswith(self.script_name):
environ['SCRIPT_NAME'] = sn + self.script_name
environ['PATH_INFO'] = pi[len(self.script_name):]
return self.app(environ, start_response)
else:
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return [
b'Path does not match script_name (',
self.script_name.encode('utf-8'),
b')\n'
]
class Server(object):
def __init__(self, app, **params):
self.app = app
self.params = params
self.last_start = 0
def run_forever(self):
......@@ -46,22 +64,21 @@ class Server(object):
# restarting too fast, delay
time.sleep(1)
params = dict(self.params)
params['app'] = self.app
self.last_start = now
proc = multiprocessing.Process(target=ServerChild.launch, kwargs={
'call_app' : self.call_app,
'app' : self.app,
'host' : self.host,
'port' : self.port
})
proc = multiprocessing.Process(target=ServerChild.launch, kwargs=params)
proc.start()
return proc
class ServerChild(object):
def __init__(self, app, host, port, call_app):
def __init__(self, app, host, port, call_app, script_name):
self.app = app
self.call_app = call_app
self.host = host
self.port = port
self.script_name = script_name
self.module_times = {}
......@@ -103,6 +120,9 @@ class ServerChild(object):
log.exception("Error while calling app")
return FailedApp("Call failed (%s)"%(exc,))
if self.script_name:
app = ScriptNameApp(app, self.script_name)
return app
def server_init(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment