Commit e25394b7 authored by Laurent Bachelier's avatar Laurent Bachelier

Add CLI option to set default environment values

This is especially useful with FORCE_SCRIPT_NAME.
parent eb968517
......@@ -437,14 +437,16 @@ class FileApp(PasteFileApp):
class Server(object):
def __init__(self, root=None):
def __init__(self, root=None, default_env=None):
"""
The optional root parameter is used to force a root directory.
If not present, the ASS2M_ROOT of environ (provided by the HTTP server)
will be used.
"""
self.root = root
self.butt = Butt(router=Router())
self.default_env = default_env or {}
if root:
self.default_env['ASS2M_ROOT'] = root
def bind(self, hostname, port):
httpserver.serve(self.process, host=hostname, port=str(port))
......@@ -453,8 +455,8 @@ class Server(object):
"""
WSGI interface
"""
if self.root:
environ.setdefault("ASS2M_ROOT", self.root)
for key, value in self.default_env.iteritems():
environ.setdefault(key, value)
try:
ctx = Context(self.butt, environ, start_response)
Dispatcher(ctx).dispatch()
......
......@@ -22,12 +22,34 @@
from ass2m.server import Server
import argparse
from os import getenv
def parse_env_arg(arg):
"""
Split VAR=val into (VAR, val). If no = is present, the value will be
taken from the system environment.
"""
arg = arg.split('=', 1)
key = arg[0]
if len(arg) > 1:
value = arg[1]
else:
value = getenv(arg[0])
return (key, value)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('rootdir', help='root directory')
parser.add_argument('-p', '--port', help='server port', type=int, default=8042)
parser.add_argument('-i', '--ip', help='server IP', default='0.0.0.0')
parser.add_argument('rootdir', help="root directory")
parser.add_argument('-p', '--port', type=int, default=8042,
help="server port")
parser.add_argument('-i', '--ip', default='0.0.0.0',
help="server IP")
parser.add_argument('-e', '--env', action='append',
metavar="VAR=val", help="environment default")
args = parser.parse_args()
server = Server(args.rootdir)
env = dict([parse_env_arg(kv) for kv in args.env])
server = Server(args.rootdir, default_env=env)
server.bind(args.ip, args.port)
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