From e85a4891ac516d0bf0192716fff08b60e25dcef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 22 May 2018 23:44:10 +0200 Subject: [PATCH] Serve index.html for static directory This allows to have shorter sao URL because it is without index.html. Also this adds the support of static web root when launching WSGI app from a different WSGI server. issue6677 review45501002 --- bin/trytond | 4 ---- trytond/protocols/dispatcher.py | 6 ------ trytond/wsgi.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/bin/trytond b/bin/trytond index e0e84386a1..d6b743834a 100755 --- a/bin/trytond +++ b/bin/trytond @@ -30,9 +30,6 @@ with commandline.pidfile(options): for name in options.database_names: Pool(name).init() hostname, port = split_netloc(config.get('web', 'listen')) - static_files = { - '/': config.get('web', 'root'), - } certificate = config.get('ssl', 'certificate') privatekey = config.get('ssl', 'privatekey') if certificate or privatekey: @@ -47,6 +44,5 @@ with commandline.pidfile(options): run_simple(hostname, port, app, threaded=True, extra_files=extra_files, - static_files=static_files, ssl_context=ssl_context, use_reloader=options.dev) diff --git a/trytond/protocols/dispatcher.py b/trytond/protocols/dispatcher.py index 78021909c7..a18d876f84 100644 --- a/trytond/protocols/dispatcher.py +++ b/trytond/protocols/dispatcher.py @@ -4,7 +4,6 @@ import logging import pydoc -from werkzeug.utils import redirect from werkzeug.exceptions import abort from sql import Table @@ -82,11 +81,6 @@ def root(request, *args): return methods[request.rpc_method](request, *request.rpc_params) -@app.route('/', methods=['GET']) -def home(request): - return redirect('/index.html') # XXX find a better way - - def db_exist(request, database_name): Database = backend.get('Database') try: diff --git a/trytond/wsgi.py b/trytond/wsgi.py index e3b102aa2a..796d4d0ec7 100644 --- a/trytond/wsgi.py +++ b/trytond/wsgi.py @@ -2,6 +2,7 @@ # this repository contains the full copyright notices and license terms. import httplib import logging +import os import sys import traceback @@ -9,6 +10,7 @@ from werkzeug.wrappers import Response from werkzeug.routing import Map, Rule from werkzeug.exceptions import abort, HTTPException, InternalServerError from werkzeug.contrib.fixers import ProxyFix +from werkzeug.wsgi import SharedDataMiddleware import wrapt @@ -118,7 +120,34 @@ class TrytondWSGI(object): def __call__(self, environ, start_response): return self.wsgi_app(environ, start_response) + +class SharedDataMiddlewareIndex(SharedDataMiddleware): + def __call__(self, environ, start_response): + if environ['REQUEST_METHOD'] not in {'GET', 'HEAD'}: + return self.app(environ, start_response) + return super(SharedDataMiddlewareIndex, self).__call__( + environ, start_response) + + def get_directory_loader(self, directory): + def loader(path): + if path is not None: + path = os.path.join(directory, path) + else: + path = directory + if os.path.isdir(path): + path = os.path.join(path, 'index.html') + if os.path.isfile(path): + return os.path.basename(path), self._opener(path) + return None, None + return loader + + app = TrytondWSGI() +if config.get('web', 'root'): + static_files = { + '/': config.get('web', 'root'), + } + app.wsgi_app = SharedDataMiddlewareIndex(app.wsgi_app, static_files) num_proxies = config.getint('web', 'num_proxies') if num_proxies: app.wsgi_app = ProxyFix(app.wsgi_app, num_proxies=num_proxies)