Skip to content

Commit

Permalink
Merge pull request #24 from CESNET/adjust-dts-data-can-be-organized-i…
Browse files Browse the repository at this point in the history
…nto-folders

#17 | data can be organized into folders in DTS - HTTP
  • Loading branch information
jan-krystof-csnt authored Oct 2, 2024
2 parents e8a9921 + 834287d commit 2156f19
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 65 deletions.
6 changes: 3 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ services:
context: ./docker/dts/http/
image: http
ports:
- "5000:5000" # Map Flask server's port to host
- "5000:5000"
volumes:
- ./docker/dts/http/data:/data # Mount the uploads folder to the container to persist uploaded files
# command: uvicorn main:app --host 0.0.0.0 --port 8000
- ./docker/dts/http/data:/data


tests-clients:
build:
Expand Down
20 changes: 15 additions & 5 deletions docker/dts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,30 @@ SAMPLE_DATE=/tmp/sample.jpg
curl -o /tmp/sample.jpg https://github.blog/wp-content/uploads/2024/07/github-logo.png
```

#### Access vi FTP
#### Access via FTP

Upload and download data
```
lftp -p 2121 -e "put /tmp/sample.jpg; get $(basename $SAMPLE_DATE) -o /tmp/sample_back.jpg; bye" service-ftp
```

#### Access via HTTP
Upload data

##### Upload

POST + `http://localhost:5000/upload` while payload is sent as 'file' HTTP body parameter.
```
curl -v -T sample.jpg ftp://service-ftp:2121/
curl -F "file=@/tmp/qwerty" http://localhost:5000/upload/foo/bar
```

Download data

##### Download
GET + `http://localhost:5000/download` while payload is sent as 'file' HTTP body parameter.
```
curl -o sample_copy.jpg ftp://service-ftp:2121/sample.jpg
wget http://localhost:5000/download/foo/bar/qwerty
```



##### List of all data
GET + `http://localhost:5000/list`
5 changes: 2 additions & 3 deletions docker/dts/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ services:
context: ./http/
image: http
ports:
- "5000:5000" # Map Flask server's port to host
- "5000:5000"
volumes:
- ./http/data:/data # Mount the uploads folder to the container to persist uploaded files
# command: uvicorn main:app --host 0.0.0.0 --port 8000
- ./http/data:/data

tests-clients:
build:
Expand Down
24 changes: 9 additions & 15 deletions docker/dts/http/Dockerfile
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"]
23 changes: 23 additions & 0 deletions docker/dts/http/logger_config.py
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)
20 changes: 0 additions & 20 deletions docker/dts/http/main_fastapi.py

This file was deleted.

63 changes: 44 additions & 19 deletions docker/dts/http/main_flask.py
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)

0 comments on commit 2156f19

Please sign in to comment.