summaryrefslogtreecommitdiff
path: root/searx/webapp.py
diff options
context:
space:
mode:
Diffstat (limited to 'searx/webapp.py')
-rw-r--r--searx/webapp.py68
1 files changed, 65 insertions, 3 deletions
diff --git a/searx/webapp.py b/searx/webapp.py
index 3ef5a72c8..fb7157b47 100644
--- a/searx/webapp.py
+++ b/searx/webapp.py
@@ -279,6 +279,12 @@ def render(template_name, override_theme=None, **kwargs):
if x != 'general'
and x in nonblocked_categories)
+ if 'all_categories' not in kwargs:
+ kwargs['all_categories'] = ['general']
+ kwargs['all_categories'].extend(x for x in
+ sorted(categories.keys())
+ if x != 'general')
+
if 'selected_categories' not in kwargs:
kwargs['selected_categories'] = []
for arg in request.args:
@@ -286,11 +292,13 @@ def render(template_name, override_theme=None, **kwargs):
c = arg.split('_', 1)[1]
if c in categories:
kwargs['selected_categories'].append(c)
+
if not kwargs['selected_categories']:
cookie_categories = request.cookies.get('categories', '').split(',')
for ccateg in cookie_categories:
if ccateg in categories:
kwargs['selected_categories'].append(ccateg)
+
if not kwargs['selected_categories']:
kwargs['selected_categories'] = ['general']
@@ -623,6 +631,24 @@ def preferences():
resp.set_cookie('theme', theme, max_age=cookie_max_age)
return resp
+
+ # stats for preferences page
+ stats = {}
+
+ for c in categories:
+ for e in categories[c]:
+ stats[e.name] = {'time': None,
+ 'warn_timeout': False,
+ 'warn_time': False}
+ if e.timeout > settings['server']['request_timeout']:
+ stats[e.name]['warn_timeout'] = True
+
+ for engine_stat in get_engines_stats()[0][1]:
+ stats[engine_stat.get('name')]['time'] = round(engine_stat.get('avg'), 3)
+ if engine_stat.get('avg') > settings['server']['request_timeout']:
+ stats[engine_stat.get('name')]['warn_time'] = True
+ # end of stats
+
return render('preferences.html',
locales=settings['locales'],
current_locale=get_locale(),
@@ -630,6 +656,7 @@ def preferences():
image_proxy=image_proxy,
language_codes=language_codes,
engines_by_category=categories,
+ stats=stats,
blocked_engines=blocked_engines,
autocomplete_backends=autocomplete_backends,
shortcuts={y: x for x, y in engine_shortcuts.items()},
@@ -670,7 +697,7 @@ def image_proxy():
return '', 400
if not resp.headers.get('content-type', '').startswith('image/'):
- logger.debug('image-proxy: wrong content-type: {0}'.format(resp.get('content-type')))
+ logger.debug('image-proxy: wrong content-type: {0}'.format(resp.headers.get('content-type')))
return '', 400
img = ''
@@ -754,10 +781,45 @@ def run():
)
-application = app
+class ReverseProxyPathFix(object):
+ '''Wrap the application in this middleware and configure the
+ front-end server to add these headers, to let you quietly bind
+ this to a URL other than / and to an HTTP scheme that is
+ different than what is used locally.
+
+ http://flask.pocoo.org/snippets/35/
+
+ In nginx:
+ location /myprefix {
+ proxy_pass http://127.0.0.1:8000;
+ proxy_set_header Host $host;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Scheme $scheme;
+ proxy_set_header X-Script-Name /myprefix;
+ }
+
+ :param app: the WSGI application
+ '''
+ def __init__(self, app):
+ self.app = app
-app.wsgi_app = ProxyFix(application.wsgi_app)
+ def __call__(self, environ, start_response):
+ script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
+ if script_name:
+ environ['SCRIPT_NAME'] = script_name
+ path_info = environ['PATH_INFO']
+ if path_info.startswith(script_name):
+ environ['PATH_INFO'] = path_info[len(script_name):]
+ scheme = environ.get('HTTP_X_SCHEME', '')
+ if scheme:
+ environ['wsgi.url_scheme'] = scheme
+ return self.app(environ, start_response)
+
+
+application = app
+# patch app to handle non root url-s behind proxy & wsgi
+app.wsgi_app = ReverseProxyPathFix(ProxyFix(application.wsgi_app))
if __name__ == "__main__":
run()