diff --git a/tsukanova_irina_lab_3/.gitignore b/tsukanova_irina_lab_3/.gitignore new file mode 100644 index 0000000..eb53ea0 --- /dev/null +++ b/tsukanova_irina_lab_3/.gitignore @@ -0,0 +1,2 @@ +/.venv +/.idea \ No newline at end of file diff --git a/tsukanova_irina_lab_3/author_service/Dockerfile b/tsukanova_irina_lab_3/author_service/Dockerfile new file mode 100644 index 0000000..4c74985 --- /dev/null +++ b/tsukanova_irina_lab_3/author_service/Dockerfile @@ -0,0 +1,20 @@ +# Использую базовый образ Python +FROM python:3.12-slim + +# Устанавливаю рабочую директорию внутри контейнера +WORKDIR /app + +# Копирую файл requirements.txt в контейнер +COPY author_service/requirements.txt . + +# Устанавливаю зависимости +RUN pip install --no-cache-dir -r requirements.txt + +# Копирую все файлы в контейнер +COPY author_service/author_service.py . + +## Открываю порт 20001 для Flask +#EXPOSE 20001 + +# Команда для запуска Python-скрипта +CMD ["python", "author_service.py"] \ No newline at end of file diff --git a/tsukanova_irina_lab_3/author_service/author_service.py b/tsukanova_irina_lab_3/author_service/author_service.py new file mode 100644 index 0000000..7240f45 --- /dev/null +++ b/tsukanova_irina_lab_3/author_service/author_service.py @@ -0,0 +1,88 @@ +from flask import Flask, jsonify, request +from uuid import uuid1 +import uuid + + +class Author: + def __init__(self, name, surname): + self.uuid_: uuid = uuid1() + self.name: str = name + self.surname: str = surname + + def to_dict(self): + return { + "uuid": self.uuid_, + "name": self.name, + "surname": self.surname + } + + +app = Flask(__name__) + +authors: list[Author] = [ + Author(name='Leon', surname='Kane'), + Author(name='James', surname='Rasal'), + Author(name='Tess', surname='Root') +] + + +def list_jsonify(): + return jsonify([author.to_dict() for author in authors]) + + +@app.route('/', methods=['GET']) +def get_all(): + return list_jsonify(), 200 + + +@app.route('/', methods=['GET']) +def get_one(uuid_): + for author in authors: + if author.uuid_ == uuid_: + return author.to_dict(), 200 + + return 'Автор с таким uuid не был найден', 404 + + +@app.route('/', methods=['POST']) +def create(): + data = request.json + name = data.get('name', None) + surname = data.get('surname', None) + # if name is None or surname is None: + # return 'Недостаточно полей для создания нового автора', 404 + + new_author = Author(name, surname) + authors.append(new_author) + return get_one(new_author.uuid_) + + +@app.route('/', methods=['PUT']) +def update_by_id(uuid_): + data = request.json + new_name = data.get('name', None) + new_surname = data.get('surname', None) + + for author in authors: + if author.uuid_ == uuid_: + if new_name is not None: + author.name = new_name + if new_surname is not None: + author.surname = new_surname + return get_one(author.uuid_) + + return 'Автор с таким uuid не был найден', 404 + + +@app.route('/', methods=['DELETE']) +def delete(uuid_): + for author in authors: + if author.uuid_ == uuid_: + authors.remove(author) + return 200 + + return 'Автор с таким uuid не был найден', 404 + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/tsukanova_irina_lab_3/author_service/requirements.txt b/tsukanova_irina_lab_3/author_service/requirements.txt new file mode 100644 index 0000000..404c0b6 Binary files /dev/null and b/tsukanova_irina_lab_3/author_service/requirements.txt differ diff --git a/tsukanova_irina_lab_3/book_service/book_service.py b/tsukanova_irina_lab_3/book_service/book_service.py new file mode 100644 index 0000000..e69de29 diff --git a/tsukanova_irina_lab_3/book_service/requirements.txt b/tsukanova_irina_lab_3/book_service/requirements.txt new file mode 100644 index 0000000..404c0b6 Binary files /dev/null and b/tsukanova_irina_lab_3/book_service/requirements.txt differ diff --git a/tsukanova_irina_lab_3/docker-compose.yaml b/tsukanova_irina_lab_3/docker-compose.yaml new file mode 100644 index 0000000..d77020b --- /dev/null +++ b/tsukanova_irina_lab_3/docker-compose.yaml @@ -0,0 +1,10 @@ +services: + + author_service: + container_name: author_service + build: + context: . + dockerfile: ./author_service/Dockerfile + ports: + - "20001:5000" +