Browse Source

Use WhiteNoise to serve static assets for the Pagure web interface

In order to make it easier to run Pagure in a containerized environment,
the web frontend needs to be capable of serving all of the frontend static
files. This change introduces WhiteNoise as a dependency that would enable
the Flask application itself to serve the static assets correctly.

This makes it possible to run a Pagure server without Apache or Nginx at all
in a containerized environment for all of the main functions.

Note that the releases folder is still not served through WhiteNoise, and
deployments that have uploading releases enabled will need something to
serve them.

Signed-off-by: Neal Gompa <ngompa13@gmail.com>
Neal Gompa 3 years ago
parent
commit
83e1979ca9
6 changed files with 19 additions and 23 deletions
  1. 0 3
      files/pagure-apache-httpd.conf
  2. 0 9
      files/pagure-nginx.conf
  3. 2 11
      files/pagure.spec
  4. 9 0
      pagure/docs_server.py
  5. 7 0
      pagure/flask_app.py
  6. 1 0
      requirements.txt

+ 0 - 3
files/pagure-apache-httpd.conf

@@ -33,8 +33,6 @@
   #SSLCertificateChainFile /etc/pki/tls/....intermediate.crt
   #SSLCertificateKeyFile /etc/pki/tls/....key
 
-  #Alias /static /usr/lib/pythonX.Y/site-packages/pagure/static/
-
   #<Location />
     #WSGIProcessGroup paguredocs
     #<IfModule mod_authz_core.c>
@@ -64,7 +62,6 @@
   #SSLCertificateChainFile /etc/pki/tls/....intermediate.crt
   #SSLCertificateKeyFile /etc/pki/tls/....key
 
-  #Alias /static /usr/lib/pythonX.Y/site-packages/pagure/static/
   #Alias /releases /var/www/releases
 
   ## Section used to support cloning git repo over http (https in this case)

+ 0 - 9
files/pagure-nginx.conf

@@ -27,11 +27,6 @@
         #try_files $uri @pagure_docs;
     #}
 
-    #location /static {
-        #alias /usr/lib/pythonX.Y/site-packages/pagure/static/;
-        #try_files $uri $uri/;
-    #}
-
 #}
 
 #server {
@@ -63,10 +58,6 @@
         #try_files $uri @pagure;
     #}
 
-    #location /static {
-        #alias /usr/lib/pythonX.Y/site-packages/pagure/static/;
-    #}
-
     #location /releases {
         #alias /var/www/releases/;
         #autoindex on;

+ 2 - 11
files/pagure.spec

@@ -75,6 +75,7 @@ Requires:           python%{python_pkgversion}-requests
 Requires:           python%{python_pkgversion}-six
 Requires:           python%{python_pkgversion}-sqlalchemy >= 0.8
 Requires:           python%{python_pkgversion}-straight-plugin
+Requires:           python%{python_pkgversion}-whitenoise
 Requires:           python%{python_pkgversion}-wtforms
 %endif
 
@@ -365,22 +366,12 @@ sed -e "s|#!/usr/bin/env python|#!%{__python}|" -i \
 # Switch interpreter for systemd units
 sed -e "s|/usr/bin/python|%{__python}|g" -i $RPM_BUILD_ROOT/%{_unitdir}/*.service
 
-%if 0%{?rhel} && 0%{?rhel} < 8
-# Change to correct static file path for apache httpd and nginx
-sed -e "s/pythonX.Y/python%{python2_version}/g" -i \
-    $RPM_BUILD_ROOT/%{_sysconfdir}/httpd/conf.d/pagure.conf \
-    $RPM_BUILD_ROOT/%{_sysconfdir}/nginx/conf.d/pagure.conf
-%else
+%if ! (0%{?rhel} && 0%{?rhel} < 8)
 # Switch all systemd units to use the correct celery
 sed -e "s|/usr/bin/celery|/usr/bin/celery-3|g" -i $RPM_BUILD_ROOT/%{_unitdir}/*.service
 
 # Switch all systemd units to use the correct gunicorn
 sed -e "s|/usr/bin/gunicorn|/usr/bin/gunicorn-3|g" -i $RPM_BUILD_ROOT/%{_unitdir}/*.service
-
-# Change to correct static file path for apache httpd and nginx
-sed -e "s/pythonX.Y/python%{python3_version}/g" -i \
-    $RPM_BUILD_ROOT/%{_sysconfdir}/httpd/conf.d/pagure.conf \
-    $RPM_BUILD_ROOT/%{_sysconfdir}/nginx/conf.d/pagure.conf
 %endif
 
 # Make log directories

+ 9 - 0
pagure/docs_server.py

@@ -17,6 +17,7 @@ import flask
 import pygit2
 
 from binaryornot.helpers import is_binary_string
+from whitenoise import WhiteNoise
 
 import pagure.config
 import pagure.doc_utils
@@ -29,6 +30,14 @@ import pagure.forms
 # Create the application.
 APP = flask.Flask(__name__)
 
+# Setup WhiteNoise for serving static files
+here = os.path.abspath(
+    os.path.join(os.path.dirname(os.path.abspath(__file__)))
+)
+APP.wsgi_app = WhiteNoise(
+    APP.wsgi_app, root=os.path.join(here, "static"), prefix="/static"
+)
+
 # set up FAS
 APP.config = pagure.config.reload_config()
 

+ 7 - 0
pagure/flask_app.py

@@ -21,6 +21,8 @@ import warnings
 import flask
 import pygit2
 
+from whitenoise import WhiteNoise
+
 import pagure.doc_utils
 import pagure.exceptions
 import pagure.forms
@@ -188,6 +190,11 @@ def create_app(config=None):
     app.jinja_loader = jinja2.ChoiceLoader(templ_loaders)
     app.register_blueprint(themeblueprint)
 
+    # Setup WhiteNoise for serving static files
+    app.wsgi_app = WhiteNoise(
+        app.wsgi_app, root=os.path.join(here, "static"), prefix="/static"
+    )
+
     app.before_request(set_request)
     app.after_request(after_request)
     app.teardown_request(end_request)

+ 1 - 0
requirements.txt

@@ -33,6 +33,7 @@ sqlalchemy >= 0.8
 # 1.4.0 is broken, 1.4.0-post-1 works but gives odd results on newer setuptools
 # the latest version 1.5.0 is also known to work
 straight.plugin
+whitenoise
 wtforms
 
 # Required only for the `oidc` authentication backend