From 204b10805e67f125e63a2ece4b184a4634038bea Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Thu, 31 Aug 2023 17:18:59 -0400 Subject: add reverse proxy and virtual path This adds the python logic, and prefix variable to assembled links in html and js --- instance/config.py | 3 +++ instance/sqlite.py | 3 +++ newspipe/bootstrap.py | 19 +++++++++++++++++++ newspipe/controllers/user.py | 2 +- newspipe/static/js/articles.js | 6 +++--- newspipe/static/js/config.js | 7 +++++++ newspipe/templates/article.html | 2 +- newspipe/templates/duplicates.html | 6 +++--- newspipe/templates/history.html | 2 +- newspipe/templates/home.html | 10 +++++----- newspipe/templates/layout.html | 3 ++- newspipe/templates/login.html | 2 +- newspipe/templates/management.html | 2 +- 13 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 newspipe/static/js/config.js diff --git a/instance/config.py b/instance/config.py index 42e624cb..af7617a4 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 1f8d6206..c6eaa461 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 00ffb96a..d2c5bc07 100644 --- a/newspipe/controllers/user.py +++ b/newspipe/controllers/user.py @@ -138,7 +138,7 @@ class LdapuserController: 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. diff --git a/newspipe/static/js/articles.js b/newspipe/static/js/articles.js index f1cee6e7..29ebac2e 100644 --- a/newspipe/static/js/articles.js +++ b/newspipe/static/js/articles.js @@ -86,7 +86,7 @@ Array.prototype.map.call(nodes, function(node) { } // sends the updates to the server - fetch(API_ROOT + "article/" + article_id, { + fetch(prefix + API_ROOT + "article/" + article_id, { method: "PUT", headers: { 'Content-Type': 'application/json', @@ -157,7 +157,7 @@ Array.prototype.map.call(nodes, function(node) { } // sends the updates to the server - fetch(API_ROOT + "article/" + article_id, { + fetch(prefix + API_ROOT + "article/" + article_id, { method: "PUT", headers: { 'Content-Type': 'application/json', @@ -189,7 +189,7 @@ Array.prototype.map.call(nodes, function(node) { data = JSON.stringify(data); // sends the updates to the server - fetch(API_ROOT + "articles", { + fetch(prefix + API_ROOT + "articles", { method: "DELETE", headers: { 'Content-Type': 'application/json', diff --git a/newspipe/static/js/config.js b/newspipe/static/js/config.js new file mode 100644 index 00000000..7fad4388 --- /dev/null +++ b/newspipe/static/js/config.js @@ -0,0 +1,7 @@ +/* + * Set variables here for the javascript used by newspipe. + */ +var prefix = "/newspipe"; + +/* Set exactly one trailing slash on prefix. */ +prefix = prefix.replace(/\/+$/,"/"); diff --git a/newspipe/templates/article.html b/newspipe/templates/article.html index c62e3f0c..d9cf9a62 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..466ff4e2 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') }}