add web-ui (#2)

Co-authored-by: Michael Grote <michael.grote@posteo.de>
Reviewed-on: mg/python-api-server#2
This commit is contained in:
Michael Grote 2023-04-26 19:51:53 +02:00
parent 288816341e
commit 47ab99f891
3 changed files with 74 additions and 2 deletions

View file

@ -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 .

27
app.py
View file

@ -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():
"""

48
templates/file_list.html Normal file
View file

@ -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>