aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormg2023-04-26 19:51:53 +0200
committermg2023-04-26 19:51:53 +0200
commit47ab99f89149352a94ebcebd034d034048881812 (patch)
treeab7f9be34f82ac18401a385c722fe1b14fc7bbf7
parent288816341e60d81203effc5952a746dd10455247 (diff)
add web-ui (#2)
Co-authored-by: Michael Grote <michael.grote@posteo.de> Reviewed-on: https://git.mgrote.net/mg/python-api-server/pulls/2
-rw-r--r--Dockerfile1
-rw-r--r--app.py27
-rw-r--r--templates/file_list.html48
3 files changed, 74 insertions, 2 deletions
diff --git a/Dockerfile b/Dockerfile
index 12d7721..879dcc8 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -12,6 +12,7 @@ RUN apt-get update && \
WORKDIR /app
+COPY templates/file_list.html ./templates/
COPY requirements.txt .
COPY app.py .
COPY gunicorn_config.py .
diff --git a/app.py b/app.py
index dd49463..d21b092 100644
--- a/app.py
+++ b/app.py
@@ -1,14 +1,16 @@
import os
import re
import uuid
-from flask import Flask, request, jsonify, send_from_directory
+from flask import Flask, request, jsonify, send_from_directory, render_template
import datetime
from flasgger import Swagger, swag_from
-app = Flask(__name__)
+app = Flask(__name__, template_folder='templates')
swagger = Swagger(app)
app.config['UPLOAD_DIRECTORY'] = os.environ.get('UPLOAD_DIRECTORY', '/uploads')
app.config['MAX_CONTENT_LENGTH'] = int(os.environ.get('MAX_CONTENT_LENGTH', '5')) * 1024 * 1024 # in MB
+app.config['ENABLE_WEBSERVER'] = os.getenv('ENABLE_WEBSERVER', 'True').lower() == 'true'
+
VALID_FILENAME_REGEX = r'^[a-zA-Z0-9\-_\.]+$'
@@ -17,6 +19,27 @@ AUTH_TOKEN = os.environ.get('AUTH_TOKEN', 'myuploadtoken')
def is_valid_filename(filename):
return bool(re.match(VALID_FILENAME_REGEX, filename))
+if app.config['ENABLE_WEBSERVER']:
+ @app.route('/', methods=['GET'])
+ def file_list():
+ """
+ Endpoint for displaying a list of files in the upload directory.
+ """
+ files = []
+ for filename in os.listdir(app.config['UPLOAD_DIRECTORY']):
+ file_path = os.path.join(app.config['UPLOAD_DIRECTORY'], filename)
+ if os.path.isfile(file_path):
+ stats = os.stat(file_path)
+ size = stats.st_size
+ last_modified = datetime.datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
+ files.append({
+ 'name': filename,
+ 'size': size,
+ 'last_modified': last_modified
+ })
+
+ return render_template('file_list.html', files=files)
+
@app.route('/health', methods=['GET'])
def health_check():
"""
diff --git a/templates/file_list.html b/templates/file_list.html
new file mode 100644
index 0000000..bfb8f2c
--- /dev/null
+++ b/templates/file_list.html
@@ -0,0 +1,48 @@
+<!doctype html>
+<html>
+<head>
+ <title>File List</title>
+ <style>
+ table {
+ border-collapse: collapse;
+ width: 100%;
+ }
+
+ th, td {
+ text-align: left;
+ padding: 8px;
+ border-bottom: 1px solid #ddd;
+ }
+
+ tr:hover {
+ background-color: #f5f5f5;
+ }
+
+ th {
+ background-color: #4CAF50;
+ color: white;
+ }
+ </style>
+</head>
+<body>
+ <h1>File List</h1>
+ <table>
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Size (KB)</th>
+ <th>Last Modified</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for file in files %}
+ <tr>
+ <td><a href="{{ url_for('download_file', filename=file.name) }}">{{ file.name }}</a>
+ <td>{{ '%.2f' % (file.size / 1024) }} KB</td>
+ <td>{{ file.last_modified }}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+</body>
+</html>