From c9e7a731db04c11b84bb357ac78405fa2d91a585 Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Wed, 30 Aug 2023 15:24:17 -0400 Subject: add support for reverse-proxy and virtual path --- instance/config.py | 3 +++ instance/sqlite.py | 3 +++ newspipe/bootstrap.py | 19 +++++++++++++++++++ newspipe/controllers/user.py | 2 +- newspipe/templates/article.html | 2 +- newspipe/templates/duplicates.html | 6 +++--- newspipe/templates/history.html | 2 +- newspipe/templates/home.html | 10 +++++----- newspipe/templates/layout.html | 2 +- newspipe/templates/login.html | 2 +- newspipe/templates/management.html | 2 +- 11 files changed, 39 insertions(+), 14 deletions(-) diff --git a/instance/config.py b/instance/config.py index af5fe9b9..bfb60538 100644 --- a/instance/config.py +++ b/instance/config.py @@ -10,6 +10,9 @@ PORT = 5000 DEBUG = True API_ROOT = "/api/v2.0" +# Optional, and useful if you are using a reverse proxy with this virtual path prefix +#PREFIX = "/newspipe" + CSRF_ENABLED = True SECRET_KEY = "LCx3BchmHRxFzkEv4BqQJyeXRLXenf" SECURITY_PASSWORD_SALT = "L8gTsyrpRQEF8jNWQPyvRfv7U5kJkD" diff --git a/instance/sqlite.py b/instance/sqlite.py index abde387a..dcc2bd46 100644 --- a/instance/sqlite.py +++ b/instance/sqlite.py @@ -10,6 +10,9 @@ PORT = 5000 DEBUG = True API_ROOT = "/api/v2.0" +# Optional, and useful if you are using a reverse proxy with this virtual path prefix +#PREFIX = "/newspipe" + CSRF_ENABLED = True SECRET_KEY = "LCx3BchmHRxFzkEv4BqQJyeXRLXenf" SECURITY_PASSWORD_SALT = "L8gTsyrpRQEF8jNWQPyvRfv7U5kJkD" diff --git a/newspipe/bootstrap.py b/newspipe/bootstrap.py index 12044916..1c5c967d 100644 --- a/newspipe/bootstrap.py +++ b/newspipe/bootstrap.py @@ -44,6 +44,14 @@ def set_logging( handler.setLevel(log_level) logger.setLevel(log_level) +class ReverseProxied(object): + def __init__(self, app, script_name): + self.app = app + self.script_name = script_name + + def __call__(self, environ, start_response): + environ['SCRIPT_NAME'] = self.script_name + return self.app(environ, start_response) # Create Flask application application = Flask(__name__, instance_relative_config=True) @@ -63,6 +71,11 @@ else: set_logging(application.config["LOG_PATH"]) +_prefix = "" +if "PREFIX" in application.config: + application.wsgi_app = ReverseProxied(application.wsgi_app, script_name=application.config["PREFIX"]) + _prefix = application.config["PREFIX"] + db = SQLAlchemy(application) migrate = Migrate(application, db) @@ -100,3 +113,9 @@ application.jinja_env.filters["datetime"] = format_datetime application.jinja_env.filters["datetimeformat"] = datetimeformat # inject application in Jinja env application.jinja_env.globals["application"] = application + +@application.context_processor +def utility_processor(): + def prefix(): + return _prefix.rstrip("/") + return dict(prefix=prefix) diff --git a/newspipe/controllers/user.py b/newspipe/controllers/user.py index 37af3215..c073ee5f 100644 --- a/newspipe/controllers/user.py +++ b/newspipe/controllers/user.py @@ -125,7 +125,7 @@ class LdapuserController(object): namelist = [] try: query = dns.resolver.query(f"_ldap._tcp.{domain}","SRV") - except dns.resolver.NXDOMAIN: + except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): # no records exist that match the request, so we were probably given a specific hostname, and an empty query will trigger the logic below that will add the original domain to the list. query = [] for i in query: diff --git a/newspipe/templates/article.html b/newspipe/templates/article.html index 5eff64e7..256b7166 100644 --- a/newspipe/templates/article.html +++ b/newspipe/templates/article.html @@ -4,7 +4,7 @@

{{ article.title|safe }}

-

{{ _('from') }} {{ article.source.title }}

+

{{ _('from') }} {{ article.source.title }}

{% if article.like %} diff --git a/newspipe/templates/duplicates.html b/newspipe/templates/duplicates.html index 38dc52b1..d1382268 100644 --- a/newspipe/templates/duplicates.html +++ b/newspipe/templates/duplicates.html @@ -1,7 +1,7 @@ {% extends "layout.html" %} {% block content %}
-

{{ _('Duplicates in the feed') }} {{ feed.title }}.

+

{{ _('Duplicates in the feed') }} {{ feed.title }}.

@@ -19,8 +19,8 @@ {% for pair in duplicates %} - - + + {% endfor %} diff --git a/newspipe/templates/history.html b/newspipe/templates/history.html index 153c2f11..00e22ef3 100644 --- a/newspipe/templates/history.html +++ b/newspipe/templates/history.html @@ -43,7 +43,7 @@ diff --git a/newspipe/templates/home.html b/newspipe/templates/home.html index 631b769c..5feb18d1 100644 --- a/newspipe/templates/home.html +++ b/newspipe/templates/home.html @@ -42,7 +42,7 @@ {% if feed_id == feed.id %}{% endif %} {% endif %} diff --git a/newspipe/templates/layout.html b/newspipe/templates/layout.html index 2464040c..90919a7a 100644 --- a/newspipe/templates/layout.html +++ b/newspipe/templates/layout.html @@ -21,7 +21,7 @@ {% block menu %}
{{ loop.index }} {{ pair[0].title }} ({{ pair[0].retrieved_date }}) {{ pair[1].title }} ({{ pair[1].retrieved_date }}) {{ pair[0].title }} ({{ pair[0].retrieved_date }}) {{ pair[1].title }} ({{ pair[1].retrieved_date }})
- {{ article.source.title | safe }} + {{ article.source.title | safe }} - {{ article.title | truncate(100, False, '...') }} + {{ article.title | truncate(100, False, '...') }} {{ article.date | datetime(format='short') }}