forked from ndopj/tesp-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from CESNET/adjust-dts-data-can-be-organized-i…
…nto-folders #17 | data can be organized into folders in DTS - HTTP
- Loading branch information
Showing
7 changed files
with
96 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,16 @@ | ||
FROM ubuntu:22.04 | ||
FROM ubuntu:jammy | ||
# target-image-suffix: tools/http-storage:0.1 | ||
|
||
ARG NAME=nginx-http-test | ||
COPY requirements.txt ./ | ||
COPY main_flask.py logger_config.py ./ | ||
COPY docker-entrypoint.sh / | ||
|
||
RUN apt update && apt install -y --no-install-recommends \ | ||
python3 \ | ||
python3-pip | ||
|
||
RUN mkdir /opt/${NAME} | ||
WORKDIR /opt/${NAME} | ||
|
||
COPY requirements.txt ./ | ||
|
||
RUN pip3 install -r requirements.txt | ||
python3-pip \ | ||
&& pip3 install -r requirements.txt \ | ||
&& chmod +x /docker-entrypoint.sh | ||
|
||
COPY main_flask.py ./ | ||
COPY main_fastapi.py ./ | ||
|
||
COPY docker-entrypoint.sh / | ||
RUN chmod +x /docker-entrypoint.sh | ||
EXPOSE 5000 | ||
|
||
ENTRYPOINT ["/docker-entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import logging | ||
|
||
# Vytvořte logger | ||
logger = logging.getLogger('app_logger') | ||
logger.setLevel(logging.DEBUG) | ||
|
||
# Vytvořte console handler a nastavte úroveň na DEBUG | ||
ch = logging.StreamHandler() | ||
ch.setLevel(logging.DEBUG) | ||
|
||
# Vytvořte formátovací objekt a přidejte jej do handleru | ||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
ch.setFormatter(formatter) | ||
|
||
# Přidejte handler do loggeru | ||
logger.addHandler(ch) | ||
|
||
# Zajišťuje, že logger bude konfigurován pouze jednou | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
# Pokud máte více handlerů, nezapomeňte případně na odstranění duplicit | ||
if not logger.handlers: | ||
logger.addHandler(ch) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,58 @@ | ||
from flask import Flask, request, send_file, send_from_directory | ||
import os | ||
import subprocess | ||
from flask import Flask, request, send_file, jsonify | ||
from werkzeug.utils import secure_filename | ||
from logger_config import logger | ||
|
||
app = Flask(__name__) | ||
DATA_DIR = '/data' | ||
|
||
@app.route('/upload', methods=['POST']) | ||
def upload_file(): | ||
if 'file' not in request.files: | ||
return 'No file part', 400 | ||
|
||
@app.route('/upload', defaults={'target_path': ''}, methods=['POST'], strict_slashes=False) | ||
@app.route('/upload/<path:target_path>', methods=['POST']) | ||
def upload_file(target_path): | ||
if 'file' not in request.files: | ||
return "No 'file' parameter present in the HTTP request", 400 | ||
file = request.files['file'] | ||
|
||
if file.filename == '': | ||
return 'No selected file', 400 | ||
|
||
if file: | ||
filename = secure_filename(file.filename) | ||
file.save(os.path.join('/data', filename)) | ||
return 'File uploaded successfully.', 200 | ||
return 'No file received.', 400 | ||
target_dir = os.path.join(DATA_DIR, target_path) | ||
os.makedirs(target_dir, exist_ok=True) | ||
|
||
@app.route('/download/<filename>', methods=['GET']) | ||
def download_file(filename): | ||
return send_from_directory('/data', filename, as_attachment=True) | ||
file_path = os.path.join('uploads', filename) | ||
if os.path.exists(file_path): | ||
return send_file(file_path, as_attachment=True) | ||
return 'File not found.', 400 | ||
filename = secure_filename(file.filename) | ||
file.save(os.path.join(target_dir, filename)) | ||
return 'File uploaded successfully.', 200 | ||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', debug=True) | ||
|
||
@app.route('/download/<path:file_path>', methods=['GET']) | ||
def download_file(file_path): | ||
logger.info(f"path { file_path }") | ||
|
||
target_file = os.path.join(DATA_DIR, file_path) | ||
if os.path.exists(target_file): | ||
return send_file(target_file, as_attachment=True) | ||
|
||
return 'File not found.', 404 | ||
|
||
|
||
@app.route('/list', methods=['GET']) | ||
def list_data(): | ||
try: | ||
# Run the `ls -la` command on the /data directory | ||
result = subprocess.run(['ls', '-laR', DATA_DIR], capture_output=True, text=True) | ||
|
||
# Check if the command was successful | ||
if result.returncode == 0: | ||
# Split the result into lines | ||
output = result.stdout.split('\n') | ||
return jsonify({'status': 'success', 'output': output}) | ||
else: | ||
return jsonify({'status': 'error', 'message': result.stderr}), 500 | ||
except Exception as e: | ||
return jsonify({'status': 'error', 'message': str(e)}), 500 | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', debug=True, port=5000) |