Eclectic Media Git klaus / 44156df
add support for suburi, close #16 This allows you do add HTTP headers or alternatively use the new --prefix=/foo/ flag to signal klaus you want to serve on this given sub url. If you are using nginx, see http://flask.pocoo.org/snippets/35/ for a complete setup. posativ 8 years ago
3 changed file(s) with 34 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
2121 help="bind to host/address (default: 127.0.0.1)"),
2222 make_option("-p", "--port", type=int, default=8080, dest="port",
2323 help="webserver port (default: 8080)"),
24 make_option("--prefix", type=str, default='/', dest="prefix",
25 help="serve on given sub uri"),
2426 make_option("-r", "--use-reloader", action="store_true", dest="reloader",
2527 help=SUPPRESS_HELP, default=False),
2628 ]
3941 print '%r: Not a git repository' % path
4042 args.remove(path)
4143
42 app = make_app(args)
44 app = make_app(args, options.prefix)
4345
4446 if bjoern and not options.use_reloader:
4547 run(app, options.interface, options.port)
3434 Rule('/<repo>/tree/<commit_id>/', defaults={'path': ''}, endpoint='history'),
3535 Rule('/<repo>/tree/<commit_id>/<path:path>', endpoint='history')
3636 ])
37
38
39 class SubUri(object):
40 """Wrap the application in this middleware to let you quietly bind
41 this to a URL other than / and to an HTTP scheme that is different
42 than what is used locally. If you don't send HTTP_X_SCRIPT_NAME,
43 you can optionally use ``klaus --prefix=/myprefix/``.
44
45 -- via http://flask.pocoo.org/snippets/35/
46 """
47 def __init__(self, app, prefix):
48 self.app = app
49 self.prefix = prefix
50
51 def __call__(self, environ, start_response):
52 script_name = environ.get('HTTP_X_SCRIPT_NAME', self.prefix)
53 if script_name:
54 environ['SCRIPT_NAME'] = script_name
55 path_info = environ['PATH_INFO']
56 if path_info.startswith(script_name):
57 environ['PATH_INFO'] = path_info[len(script_name):]
58
59 scheme = environ.get('HTTP_X_SCHEME', '')
60 if scheme:
61 environ['wsgi.url_scheme'] = scheme
62 return self.app(environ, start_response)
3763
3864
3965 class Klaus(object):
78104 return self.wsgi_app(environ, start_response)
79105
80106
81 def make_app(repos):
107 def make_app(repos, prefix='/'):
82108
83109 repos = dict(
84110 (repo.rstrip(os.sep).split(os.sep)[-1].replace('.git', ''), repo)
96122 app.jinja_env.filters['is_image'] = guess_is_image
97123 app.jinja_env.filters['shorten_author'] = extract_author_name
98124
125 app.wsgi_app = SubUri(app.wsgi_app, prefix=prefix)
99126 app = SharedDataMiddleware(app, {
100127 '/static/': os.path.join(os.path.dirname(__file__), 'static/')
101128 })
44 <script src=/static/line-highlighter.js></script>
55
66 <header>
7 <a href=/>{{ environ.HTTP_HOST }}</a>
7 <a href={{ environ.SCRIPT_NAME }}>
8 {{ environ.HTTP_HOST ~ environ.SCRIPT_NAME.rstrip('/') }}
9 </a>
810 <span class=breadcrumbs>{% block breadcrumbs %}{% endblock %}</span>
911 {% block extra_header %}{% endblock %}
1012 </header>