From a0ef65e0f911a3b1cafb063a5bd7500c3a9063f2 Mon Sep 17 00:00:00 2001 From: Inohara Date: Fri, 27 Sep 2024 16:53:32 +0400 Subject: [PATCH 1/7] =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tsukanova_irina_lab_3/.gitignore | 2 + .../author_service/Dockerfile | 20 ++++ .../author_service/author_service.py | 88 ++++++++++++++++++ .../author_service/requirements.txt | Bin 0 -> 34 bytes .../book_service/book_service.py | 0 .../book_service/requirements.txt | Bin 0 -> 34 bytes tsukanova_irina_lab_3/docker-compose.yaml | 10 ++ 7 files changed, 120 insertions(+) create mode 100644 tsukanova_irina_lab_3/.gitignore create mode 100644 tsukanova_irina_lab_3/author_service/Dockerfile create mode 100644 tsukanova_irina_lab_3/author_service/author_service.py create mode 100644 tsukanova_irina_lab_3/author_service/requirements.txt create mode 100644 tsukanova_irina_lab_3/book_service/book_service.py create mode 100644 tsukanova_irina_lab_3/book_service/requirements.txt create mode 100644 tsukanova_irina_lab_3/docker-compose.yaml 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 0000000000000000000000000000000000000000..404c0b6b5d21535502d2cf0da8d786bbeeb20ab8 GIT binary patch literal 34 icmezW&y696A(5e&A)CP#2#p!^7z}{en1Ppp3ycAfq6LWn literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..404c0b6b5d21535502d2cf0da8d786bbeeb20ab8 GIT binary patch literal 34 icmezW&y696A(5e&A)CP#2#p!^7z}{en1Ppp3ycAfq6LWn literal 0 HcmV?d00001 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" + From c3537b5abe2d229123bd436c87cc509769c869a6 Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Fri, 27 Sep 2024 23:26:36 +0400 Subject: [PATCH 2/7] =?UTF-8?q?=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../author_service/author_service.py | 12 +++---- .../book_service/book_service.py | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/tsukanova_irina_lab_3/author_service/author_service.py b/tsukanova_irina_lab_3/author_service/author_service.py index 7240f45..367cf3a 100644 --- a/tsukanova_irina_lab_3/author_service/author_service.py +++ b/tsukanova_irina_lab_3/author_service/author_service.py @@ -1,11 +1,11 @@ from flask import Flask, jsonify, request -from uuid import uuid1 +from uuid import uuid4 import uuid class Author: def __init__(self, name, surname): - self.uuid_: uuid = uuid1() + self.uuid_: uuid = uuid4() self.name: str = name self.surname: str = surname @@ -35,7 +35,7 @@ def get_all(): return list_jsonify(), 200 -@app.route('/', methods=['GET']) +@app.route('/', methods=['GET']) def get_one(uuid_): for author in authors: if author.uuid_ == uuid_: @@ -57,7 +57,7 @@ def create(): return get_one(new_author.uuid_) -@app.route('/', methods=['PUT']) +@app.route('/', methods=['PUT']) def update_by_id(uuid_): data = request.json new_name = data.get('name', None) @@ -74,12 +74,12 @@ def update_by_id(uuid_): return 'Автор с таким uuid не был найден', 404 -@app.route('/', methods=['DELETE']) +@app.route('/', methods=['DELETE']) def delete(uuid_): for author in authors: if author.uuid_ == uuid_: authors.remove(author) - return 200 + return '', 200 return 'Автор с таким uuid не был найден', 404 diff --git a/tsukanova_irina_lab_3/book_service/book_service.py b/tsukanova_irina_lab_3/book_service/book_service.py index e69de29..bbd9c7e 100644 --- a/tsukanova_irina_lab_3/book_service/book_service.py +++ b/tsukanova_irina_lab_3/book_service/book_service.py @@ -0,0 +1,32 @@ +from flask import Flask, jsonify, request +from uuid import uuid4 +import uuid +from author_service.author_service import Author + + +class Book: + def __init__(self, title, year, author_id): + self.uuid_: uuid = uuid4() + self.title: str = title + self.year: int = year + self.author_id: uuid = author_id + + def to_dict(self, author: Author): + return { + 'title': self.title, + 'year': self.year, + 'author_id': self.author_id, + 'author_info': author.to_dict(), + } + + +app = Flask(__name__) + +books: list[Book] = [ + Book(title='Garden', year=1977, author_id=1), + Book(title='New York', year=1977, author_id=1), + Book(title='The story of flowers', year=1977, author_id=1), + Book(title='Little Rock', year=1977, author_id=1), + Book(title='One Piece', year=1977, author_id=1), + Book(title='Clack clover', year=1977, author_id=1), +] From 8b96102dbd7e717ed1d04398a12616cdb350c36c Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Tue, 1 Oct 2024 23:05:44 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D0=B8=20=D0=B5=D1=89?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../author_service/author_service.py | 38 ++++- tsukanova_irina_lab_3/book_service/Dockerfile | 20 +++ .../book_service/book_service.py | 148 ++++++++++++++++-- tsukanova_irina_lab_3/docker-compose.yaml | 17 ++ tsukanova_irina_lab_3/nginx/Dockerfile | 4 + tsukanova_irina_lab_3/nginx/nginx.conf | 22 +++ 6 files changed, 231 insertions(+), 18 deletions(-) create mode 100644 tsukanova_irina_lab_3/book_service/Dockerfile create mode 100644 tsukanova_irina_lab_3/nginx/Dockerfile create mode 100644 tsukanova_irina_lab_3/nginx/nginx.conf diff --git a/tsukanova_irina_lab_3/author_service/author_service.py b/tsukanova_irina_lab_3/author_service/author_service.py index 367cf3a..a475a84 100644 --- a/tsukanova_irina_lab_3/author_service/author_service.py +++ b/tsukanova_irina_lab_3/author_service/author_service.py @@ -1,11 +1,15 @@ from flask import Flask, jsonify, request from uuid import uuid4 import uuid +import requests class Author: - def __init__(self, name, surname): - self.uuid_: uuid = uuid4() + def __init__(self, name, surname, uuid_: uuid): + if uuid_ is None: + self.uuid_: uuid = uuid4() + else: + self.uuid_: uuid = uuid.UUID(uuid_) self.name: str = name self.surname: str = surname @@ -16,16 +20,27 @@ class Author: "surname": self.surname } + def to_dict_with_books(self, books: list): + return { + "uuid": self.uuid_, + "name": self.name, + "surname": self.surname, + "books": books + } + app = Flask(__name__) authors: list[Author] = [ - Author(name='Leon', surname='Kane'), - Author(name='James', surname='Rasal'), - Author(name='Tess', surname='Root') + Author(name='Leon', surname='Kane', uuid_='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'), + Author(name='James', surname='Rasal', uuid_='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Author(name='Tess', surname='Root', uuid_='eb815350-c7b9-4446-8434-4c0640c21995') ] +books_url = 'http://localhost:5001/' + + def list_jsonify(): return jsonify([author.to_dict() for author in authors]) @@ -44,6 +59,17 @@ def get_one(uuid_): return 'Автор с таким uuid не был найден', 404 +@app.route('/with-books/', methods=['GET']) +def get_one_with_books(uuid_): + for author in authors: + if author.uuid_ == uuid_: + response = requests.get(books_url + f'by-author/{uuid_}') + print(response.json()) + return author.to_dict_with_books(response.json()), 200 + + return 'Автор с таким uuid не был найден', 404 + + @app.route('/', methods=['POST']) def create(): data = request.json @@ -52,7 +78,7 @@ def create(): # if name is None or surname is None: # return 'Недостаточно полей для создания нового автора', 404 - new_author = Author(name, surname) + new_author = Author(name, surname, None) authors.append(new_author) return get_one(new_author.uuid_) diff --git a/tsukanova_irina_lab_3/book_service/Dockerfile b/tsukanova_irina_lab_3/book_service/Dockerfile new file mode 100644 index 0000000..99845f7 --- /dev/null +++ b/tsukanova_irina_lab_3/book_service/Dockerfile @@ -0,0 +1,20 @@ +# Использую базовый образ Python +FROM python:3.12-slim + +# Устанавливаю рабочую директорию внутри контейнера +WORKDIR /app + +# Копирую файл requirements.txt в контейнер +COPY book_service/requirements.txt . + +# Устанавливаю зависимости +RUN pip install --no-cache-dir -r requirements.txt + +# Копирую все файлы в контейнер +COPY book_service/book_service.py . + +## Открываю порт 20001 для Flask +#EXPOSE 20001 + +# Команда для запуска Python-скрипта +CMD ["python", "book_service.py"] \ No newline at end of file diff --git a/tsukanova_irina_lab_3/book_service/book_service.py b/tsukanova_irina_lab_3/book_service/book_service.py index bbd9c7e..100e8e8 100644 --- a/tsukanova_irina_lab_3/book_service/book_service.py +++ b/tsukanova_irina_lab_3/book_service/book_service.py @@ -1,32 +1,156 @@ from flask import Flask, jsonify, request +import requests from uuid import uuid4 import uuid -from author_service.author_service import Author + + +class Author: + def __init__(self, name, surname, uuid_: uuid): + if uuid_ is None: + self.uuid_: uuid = uuid4() + else: + self.uuid_: uuid = uuid.UUID(uuid_) + self.name: str = name + self.surname: str = surname + + def to_dict(self): + return { + "uuid": self.uuid_, + "name": self.name, + "surname": self.surname + } class Book: - def __init__(self, title, year, author_id): - self.uuid_: uuid = uuid4() + def __init__(self, title, year, uuid_: uuid, author_id: uuid): + if uuid_ is None: + self.uuid_: uuid = uuid4() + else: + self.uuid_: uuid = uuid.UUID(uuid_) self.title: str = title self.year: int = year - self.author_id: uuid = author_id + self.author_id: uuid = uuid.UUID(author_id) - def to_dict(self, author: Author): + def to_dict(self): return { 'title': self.title, 'year': self.year, 'author_id': self.author_id, - 'author_info': author.to_dict(), + 'uuid': self.uuid_ + } + + def to_dict_for_authors(self): + return { + 'title': self.title, + 'year': self.year, + 'uuid': self.uuid_ + } + + def to_dict_with_info(self, author: dict): + return { + 'title': self.title, + 'year': self.year, + 'author_id': self.author_id, + 'author_info': author, + 'uuid': self.uuid_ } app = Flask(__name__) books: list[Book] = [ - Book(title='Garden', year=1977, author_id=1), - Book(title='New York', year=1977, author_id=1), - Book(title='The story of flowers', year=1977, author_id=1), - Book(title='Little Rock', year=1977, author_id=1), - Book(title='One Piece', year=1977, author_id=1), - Book(title='Clack clover', year=1977, author_id=1), + Book(title='Garden', year=1977, uuid_='89fa1e7a-7e88-445e-a4d8-6d4497ea8f19', author_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'), + Book(title='New York', year=1989, uuid_='0351ee11-f11b-4d83-b2c8-1075b0c357dc', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='The story of flowers', uuid_='dfc17619-7690-47aa-ae8e-6a5068f8ddec', year=1977, author_id='eb815350-c7b9-4446-8434-4c0640c21995'), + Book(title='Little Rock', year=1990, uuid_='3fad0e6b-cefc-40dd-99c0-adc5ec0290d2', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='One Piece', year=1994, uuid_='cf0cf26e-c1c6-43ea-bf59-1f8edb180e41', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='Black clover', year=1940, uuid_='2ac3c734-21ad-4450-884d-7de1f5452b9d', author_id='eb815350-c7b9-4446-8434-4c0640c21995'), ] + + +authors_url = 'http://localhost:5000/' + + +def list_jsonify(): + return jsonify([book.to_dict() for book in books]) + + +@app.route('/', methods=['GET']) +def get_all(): + return list_jsonify(), 200 + + +# @app.route('/', methods=['GET']) +# def get_all_full(): +# response = [] +# for book in books: +# response.append(requests.get(authors_url + str(book.author_id)).json()) +# +# return list_jsonify(), 200 + + +@app.route('/by-author/', methods=['GET']) +def get_by_author_id(author_uuid): + return [book.to_dict_for_authors() for book in books if book.author_id == author_uuid], 200 + + +@app.route('/', methods=['GET']) +def get_one(uuid_): + for book in books: + if book.uuid_ == uuid_: + return book.to_dict(), 200 + + return 'Книга с таким uuid не была найдена', 404 + + +@app.route('/full/', methods=['GET']) +def get_one_full(uuid_): + for book in books: + if book.uuid_ == uuid_: + response = requests.get(authors_url + str(book.author_id)) + return book.to_dict_with_info(response.json()), 200 + + return 'Книга с таким uuid не была найдена', 404 + + +@app.route('/', methods=['POST']) +def create(): + data = request.json + title = data.get('title', None) + year = data.get('year', None) + author_id = data.get('author_id', None) + + new_book = Book(title, year, None, author_id) + books.append(new_book) + return get_one(new_book.uuid_) + + +@app.route('/', methods=['PUT']) +def update_by_id(uuid_): + data = request.json + new_title = data.get('title', None) + new_year = data.get('year', None) + + for book in books: + if book.uuid_ == uuid_: + if new_title is not None: + book.title = new_title + if new_year is not None: + book.year = new_year + return get_one(book.uuid_) + + return 'Книга с таким uuid не была найдена', 404 + + +@app.route('/', methods=['DELETE']) +def delete(uuid_): + for book in books: + if book.uuid_ == uuid_: + books.remove(book) + return '', 200 + + return 'Книга с таким uuid не была найдена', 404 + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5001, debug=True) diff --git a/tsukanova_irina_lab_3/docker-compose.yaml b/tsukanova_irina_lab_3/docker-compose.yaml index d77020b..58ca660 100644 --- a/tsukanova_irina_lab_3/docker-compose.yaml +++ b/tsukanova_irina_lab_3/docker-compose.yaml @@ -8,3 +8,20 @@ services: ports: - "20001:5000" + book_service: + container_name: book_service + build: + context: . + dockerfile: ./book_service/Dockerfile + ports: + - "20002:5000" + + nginx: + image: nginx:latest + ports: + - "80:80" + depends_on: + - author_service + - book_service + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf \ No newline at end of file diff --git a/tsukanova_irina_lab_3/nginx/Dockerfile b/tsukanova_irina_lab_3/nginx/Dockerfile new file mode 100644 index 0000000..65f3ec5 --- /dev/null +++ b/tsukanova_irina_lab_3/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM ubuntu:latest +LABEL authors="Main" + +ENTRYPOINT ["top", "-b"] \ No newline at end of file diff --git a/tsukanova_irina_lab_3/nginx/nginx.conf b/tsukanova_irina_lab_3/nginx/nginx.conf new file mode 100644 index 0000000..f6ff7c0 --- /dev/null +++ b/tsukanova_irina_lab_3/nginx/nginx.conf @@ -0,0 +1,22 @@ +http { + server { + listen 80; + server_name localhost; + + location /author_service/ { + proxy_pass http://author_service:20001/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location /book_service/ { + proxy_pass http://book_service:20002/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + } +} \ No newline at end of file From 0f898b968dddcbc3a534d1e8669ea60d6eb2a1d7 Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Wed, 2 Oct 2024 10:47:53 +0400 Subject: [PATCH 4/7] tot --- .../book_service/book_service.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tsukanova_irina_lab_3/book_service/book_service.py b/tsukanova_irina_lab_3/book_service/book_service.py index 100e8e8..624a6df 100644 --- a/tsukanova_irina_lab_3/book_service/book_service.py +++ b/tsukanova_irina_lab_3/book_service/book_service.py @@ -80,13 +80,16 @@ def get_all(): return list_jsonify(), 200 -# @app.route('/', methods=['GET']) -# def get_all_full(): -# response = [] -# for book in books: -# response.append(requests.get(authors_url + str(book.author_id)).json()) -# -# return list_jsonify(), 200 +@app.route('/full', methods=['GET']) +def get_all_full(): + authors: list[dict] = requests.get(authors_url).json() + response = [] + for book in books: + for author in authors: + if book.author_id == uuid.UUID(author.get('uuid')): + response.append(book.to_dict_with_info(author)) + + return response, 200 @app.route('/by-author/', methods=['GET']) From 858ea65e715f9ccb010f8f6c22acfb3f75e0b1c2 Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Wed, 2 Oct 2024 14:16:54 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=D1=87=D1=82=D0=BE-=D1=82=D0=BE=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../author_service/author_service.py | 24 +++++-- .../author_service/requirements.txt | Bin 34 -> 66 bytes .../book_service/book_service.py | 62 ++++++++++-------- .../book_service/requirements.txt | Bin 34 -> 70 bytes tsukanova_irina_lab_3/docker-compose.yaml | 34 +++++----- tsukanova_irina_lab_3/nginx/nginx.conf | 6 +- tsukanova_irina_lab_3/requirements.txt | Bin 0 -> 70 bytes 7 files changed, 74 insertions(+), 52 deletions(-) create mode 100644 tsukanova_irina_lab_3/requirements.txt diff --git a/tsukanova_irina_lab_3/author_service/author_service.py b/tsukanova_irina_lab_3/author_service/author_service.py index a475a84..1269b38 100644 --- a/tsukanova_irina_lab_3/author_service/author_service.py +++ b/tsukanova_irina_lab_3/author_service/author_service.py @@ -37,7 +37,6 @@ authors: list[Author] = [ Author(name='Tess', surname='Root', uuid_='eb815350-c7b9-4446-8434-4c0640c21995') ] - books_url = 'http://localhost:5001/' @@ -45,11 +44,13 @@ 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: @@ -59,6 +60,7 @@ def get_one(uuid_): return 'Автор с таким uuid не был найден', 404 +# получение автора со списком его книг @app.route('/with-books/', methods=['GET']) def get_one_with_books(uuid_): for author in authors: @@ -70,19 +72,30 @@ def get_one_with_books(uuid_): return 'Автор с таким uuid не был найден', 404 +# проверка наличия автора по его идентификатору (для book_service) +@app.route('/check/', methods=['GET']) +def check_exist(uuid_): + for author in authors: + if author.uuid_ == uuid_: + return '', 200 + return '', 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 + if name is None or surname is None: + return 'Недостаточно полей для создания нового автора', 404 new_author = Author(name, surname, None) authors.append(new_author) return get_one(new_author.uuid_) +# изменение автора по идентификатору @app.route('/', methods=['PUT']) def update_by_id(uuid_): data = request.json @@ -100,15 +113,16 @@ def update_by_id(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 'Автор успешно удален', 200 return 'Автор с таким uuid не был найден', 404 if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) + app.run(host='0.0.0.0', port=20001, debug=True) diff --git a/tsukanova_irina_lab_3/author_service/requirements.txt b/tsukanova_irina_lab_3/author_service/requirements.txt index 404c0b6b5d21535502d2cf0da8d786bbeeb20ab8..bbbb04bc6d198171252ae219000ee34398b5065b 100644 GIT binary patch delta 41 mcmY##njoiA#E{BR$WRI-iy2CQ*cJ$l81xv7p(HN@7XtvQI|gb1 delta 9 QcmZ=#njpu*%fQ6|00?UVE&u=k diff --git a/tsukanova_irina_lab_3/book_service/book_service.py b/tsukanova_irina_lab_3/book_service/book_service.py index 624a6df..67b75ba 100644 --- a/tsukanova_irina_lab_3/book_service/book_service.py +++ b/tsukanova_irina_lab_3/book_service/book_service.py @@ -4,23 +4,6 @@ from uuid import uuid4 import uuid -class Author: - def __init__(self, name, surname, uuid_: uuid): - if uuid_ is None: - self.uuid_: uuid = uuid4() - else: - self.uuid_: uuid = uuid.UUID(uuid_) - self.name: str = name - self.surname: str = surname - - def to_dict(self): - return { - "uuid": self.uuid_, - "name": self.name, - "surname": self.surname - } - - class Book: def __init__(self, title, year, uuid_: uuid, author_id: uuid): if uuid_ is None: @@ -59,15 +42,20 @@ class Book: app = Flask(__name__) books: list[Book] = [ - Book(title='Garden', year=1977, uuid_='89fa1e7a-7e88-445e-a4d8-6d4497ea8f19', author_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'), - Book(title='New York', year=1989, uuid_='0351ee11-f11b-4d83-b2c8-1075b0c357dc', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), - Book(title='The story of flowers', uuid_='dfc17619-7690-47aa-ae8e-6a5068f8ddec', year=1977, author_id='eb815350-c7b9-4446-8434-4c0640c21995'), - Book(title='Little Rock', year=1990, uuid_='3fad0e6b-cefc-40dd-99c0-adc5ec0290d2', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), - Book(title='One Piece', year=1994, uuid_='cf0cf26e-c1c6-43ea-bf59-1f8edb180e41', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), - Book(title='Black clover', year=1940, uuid_='2ac3c734-21ad-4450-884d-7de1f5452b9d', author_id='eb815350-c7b9-4446-8434-4c0640c21995'), + Book(title='Garden', year=1977, uuid_='89fa1e7a-7e88-445e-a4d8-6d4497ea8f19', + author_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'), + Book(title='New York', year=1989, uuid_='0351ee11-f11b-4d83-b2c8-1075b0c357dc', + author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='The story of flowers', uuid_='dfc17619-7690-47aa-ae8e-6a5068f8ddec', year=1977, + author_id='eb815350-c7b9-4446-8434-4c0640c21995'), + Book(title='Little Rock', year=1990, uuid_='3fad0e6b-cefc-40dd-99c0-adc5ec0290d2', + author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='One Piece', year=1994, uuid_='cf0cf26e-c1c6-43ea-bf59-1f8edb180e41', + author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='Black clover', year=1940, uuid_='2ac3c734-21ad-4450-884d-7de1f5452b9d', + author_id='eb815350-c7b9-4446-8434-4c0640c21995'), ] - authors_url = 'http://localhost:5000/' @@ -75,11 +63,13 @@ def list_jsonify(): return jsonify([book.to_dict() for book in books]) +# получить список книг без подробной инфы об авторе @app.route('/', methods=['GET']) def get_all(): return list_jsonify(), 200 +# получение всех книг с информацией об авторе @app.route('/full', methods=['GET']) def get_all_full(): authors: list[dict] = requests.get(authors_url).json() @@ -92,11 +82,13 @@ def get_all_full(): return response, 200 +# получение списка книг по идентификатору автора (для author_service) @app.route('/by-author/', methods=['GET']) def get_by_author_id(author_uuid): return [book.to_dict_for_authors() for book in books if book.author_id == author_uuid], 200 +# получение книги по идентификатору @app.route('/', methods=['GET']) def get_one(uuid_): for book in books: @@ -106,6 +98,7 @@ def get_one(uuid_): return 'Книга с таким uuid не была найдена', 404 +# получение книги по идентификатору с инфой об авторе @app.route('/full/', methods=['GET']) def get_one_full(uuid_): for book in books: @@ -116,18 +109,26 @@ def get_one_full(uuid_): return 'Книга с таким uuid не была найдена', 404 +# создание новой книги @app.route('/', methods=['POST']) def create(): data = request.json title = data.get('title', None) year = data.get('year', None) author_id = data.get('author_id', None) + checking = requests.get(authors_url + f'/check/{author_id}') + print(checking) + if checking.status_code == 200: + new_book = Book(title, year, None, author_id) + books.append(new_book) + return get_one(new_book.uuid_) + if checking.status_code == 404: + return 'Автор с таким uuid не существует', 404 - new_book = Book(title, year, None, author_id) - books.append(new_book) - return get_one(new_book.uuid_) + return 'Неизвестная ошибка', 500 +# изменение книги по идентификатору @app.route('/', methods=['PUT']) def update_by_id(uuid_): data = request.json @@ -135,6 +136,8 @@ def update_by_id(uuid_): new_year = data.get('year', None) for book in books: + print(book.uuid_) + if book.uuid_ == uuid_: if new_title is not None: book.title = new_title @@ -145,15 +148,16 @@ def update_by_id(uuid_): return 'Книга с таким uuid не была найдена', 404 +# удаление книги по идентификатору @app.route('/', methods=['DELETE']) def delete(uuid_): for book in books: if book.uuid_ == uuid_: books.remove(book) - return '', 200 + return 'Книга успешно удалена', 200 return 'Книга с таким uuid не была найдена', 404 if __name__ == '__main__': - app.run(host='0.0.0.0', port=5001, debug=True) + app.run(host='0.0.0.0', port=20002, debug=True) diff --git a/tsukanova_irina_lab_3/book_service/requirements.txt b/tsukanova_irina_lab_3/book_service/requirements.txt index 404c0b6b5d21535502d2cf0da8d786bbeeb20ab8..41917d8efabe387e38bb9f8a0987aa512ef04fea 100644 GIT binary patch delta 45 ocmY##n;@rA#E{BR$WRI-iy2CQ*cJ$l81xv7p(HN@7Xt_b0L6a>i2wiq delta 9 QcmZ=$njpu*%fQ6|00@r(G5`Po diff --git a/tsukanova_irina_lab_3/docker-compose.yaml b/tsukanova_irina_lab_3/docker-compose.yaml index 58ca660..3e34647 100644 --- a/tsukanova_irina_lab_3/docker-compose.yaml +++ b/tsukanova_irina_lab_3/docker-compose.yaml @@ -6,22 +6,22 @@ services: context: . dockerfile: ./author_service/Dockerfile ports: - - "20001:5000" + - 20001 - book_service: - container_name: book_service - build: - context: . - dockerfile: ./book_service/Dockerfile - ports: - - "20002:5000" + book_service: + container_name: book_service + build: + context: . + dockerfile: ./book_service/Dockerfile + expose: + - 20002 - nginx: - image: nginx:latest - ports: - - "80:80" - depends_on: - - author_service - - book_service - volumes: - - ./nginx/nginx.conf:/etc/nginx/nginx.conf \ No newline at end of file + nginx: + image: nginx:latest + ports: + - "80:80" + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf + depends_on: + - author_service + - book_service \ No newline at end of file diff --git a/tsukanova_irina_lab_3/nginx/nginx.conf b/tsukanova_irina_lab_3/nginx/nginx.conf index f6ff7c0..805d451 100644 --- a/tsukanova_irina_lab_3/nginx/nginx.conf +++ b/tsukanova_irina_lab_3/nginx/nginx.conf @@ -1,6 +1,10 @@ + +events { worker_connections 1024; } + http { server { - listen 80; + listen 80; + listen [::]:80; server_name localhost; location /author_service/ { diff --git a/tsukanova_irina_lab_3/requirements.txt b/tsukanova_irina_lab_3/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..41917d8efabe387e38bb9f8a0987aa512ef04fea GIT binary patch literal 70 zcmezW&y696A(5e&A)CP#2#p!^7z}{en1Pppi=l`im7$QK6i5~`lmIbEg%Ma5LW0zQ FFaYE|3cLUS literal 0 HcmV?d00001 From f2093f376cb4b5f60b5dd355175d38b7166b26be Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Wed, 2 Oct 2024 23:00:20 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tsukanova_irina_lab_3/author_service/Dockerfile | 3 --- tsukanova_irina_lab_3/book_service/Dockerfile | 3 --- tsukanova_irina_lab_3/docker-compose.yaml | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/tsukanova_irina_lab_3/author_service/Dockerfile b/tsukanova_irina_lab_3/author_service/Dockerfile index 4c74985..1271d2d 100644 --- a/tsukanova_irina_lab_3/author_service/Dockerfile +++ b/tsukanova_irina_lab_3/author_service/Dockerfile @@ -13,8 +13,5 @@ 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/book_service/Dockerfile b/tsukanova_irina_lab_3/book_service/Dockerfile index 99845f7..ab2f40b 100644 --- a/tsukanova_irina_lab_3/book_service/Dockerfile +++ b/tsukanova_irina_lab_3/book_service/Dockerfile @@ -13,8 +13,5 @@ RUN pip install --no-cache-dir -r requirements.txt # Копирую все файлы в контейнер COPY book_service/book_service.py . -## Открываю порт 20001 для Flask -#EXPOSE 20001 - # Команда для запуска Python-скрипта CMD ["python", "book_service.py"] \ No newline at end of file diff --git a/tsukanova_irina_lab_3/docker-compose.yaml b/tsukanova_irina_lab_3/docker-compose.yaml index 3e34647..b9cd210 100644 --- a/tsukanova_irina_lab_3/docker-compose.yaml +++ b/tsukanova_irina_lab_3/docker-compose.yaml @@ -5,7 +5,7 @@ services: build: context: . dockerfile: ./author_service/Dockerfile - ports: + expose: - 20001 book_service: From 281d30a89e0313c5b9e2e45c2ea88938baa19ec6 Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Thu, 3 Oct 2024 16:17:37 +0400 Subject: [PATCH 7/7] lab 3 done --- tsukanova_irina_lab_3/README.md | 27 ++++++++++++++++++ .../author_service/Dockerfile | 2 +- .../author_service/author_service.py | 2 +- .../author_service/requirements.txt | Bin 66 -> 0 bytes tsukanova_irina_lab_3/book_service/Dockerfile | 2 +- .../book_service/book_service.py | 2 +- .../book_service/requirements.txt | Bin 70 -> 0 bytes tsukanova_irina_lab_3/docker-compose.yaml | 2 +- tsukanova_irina_lab_3/{nginx => }/nginx.conf | 0 tsukanova_irina_lab_3/nginx/Dockerfile | 4 --- 10 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 tsukanova_irina_lab_3/README.md delete mode 100644 tsukanova_irina_lab_3/author_service/requirements.txt delete mode 100644 tsukanova_irina_lab_3/book_service/requirements.txt rename tsukanova_irina_lab_3/{nginx => }/nginx.conf (100%) delete mode 100644 tsukanova_irina_lab_3/nginx/Dockerfile diff --git a/tsukanova_irina_lab_3/README.md b/tsukanova_irina_lab_3/README.md new file mode 100644 index 0000000..4ef85d9 --- /dev/null +++ b/tsukanova_irina_lab_3/README.md @@ -0,0 +1,27 @@ +# Цуканова Ирина ПИбд-32 +# Лабораторная работа №3 - REST API, Gateway и синхронный обмен между микросервисами + +### Язык разработки приложений: Python + +## Выбранные сущности: + +- Автор. Содержит Uuid, Name, Surname +- Книга. Содержит Uuid, Title, Year, Author_id + + +## Описание: +Для каждой сущности были реализованы стандартные CRUD-операции: получение всех записей, получение конкретной записи, +создание записи, изменение записи, удаление записи. + +Дополнительно для сущности Автор прописаны: +1. Получение автора со списком его книг(с полной информацией). При этом сервис взаимодействует с другим сервисом, +чтобы получить список книг по идентификатору автора. +2. Операция проверка наличия автора по идентификатору. Это нужно для сервиса книг. + +Дополнительно для сущности Книга прописаны: +1. Получение списка записей с полной информацией об авторе (не только идентификатор). +2. Получение списка записей по идентификатору автора. Это нужно для сервиса авторов. +3. Получение конкретной записи с полной информацией об авторе. + + +## [Видео](https://drive.google.com/file/d/10qXA-jS9QIX5DnYjSAHlEhLx0OyKEk0W/view?usp=sharing) \ No newline at end of file diff --git a/tsukanova_irina_lab_3/author_service/Dockerfile b/tsukanova_irina_lab_3/author_service/Dockerfile index 1271d2d..cd6721c 100644 --- a/tsukanova_irina_lab_3/author_service/Dockerfile +++ b/tsukanova_irina_lab_3/author_service/Dockerfile @@ -5,7 +5,7 @@ FROM python:3.12-slim WORKDIR /app # Копирую файл requirements.txt в контейнер -COPY author_service/requirements.txt . +COPY requirements.txt . # Устанавливаю зависимости RUN pip install --no-cache-dir -r requirements.txt diff --git a/tsukanova_irina_lab_3/author_service/author_service.py b/tsukanova_irina_lab_3/author_service/author_service.py index 1269b38..357eeb0 100644 --- a/tsukanova_irina_lab_3/author_service/author_service.py +++ b/tsukanova_irina_lab_3/author_service/author_service.py @@ -37,7 +37,7 @@ authors: list[Author] = [ Author(name='Tess', surname='Root', uuid_='eb815350-c7b9-4446-8434-4c0640c21995') ] -books_url = 'http://localhost:5001/' +books_url = 'http://book_service:20002/' def list_jsonify(): diff --git a/tsukanova_irina_lab_3/author_service/requirements.txt b/tsukanova_irina_lab_3/author_service/requirements.txt deleted file mode 100644 index bbbb04bc6d198171252ae219000ee34398b5065b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 66 zcmezW&y696A(5e&A)CP#2#p!^7z}{en1Pppi=l`im7$QK6i5~`lmIbEg%Ma5LW0x) E0J&ibr2qf` diff --git a/tsukanova_irina_lab_3/book_service/Dockerfile b/tsukanova_irina_lab_3/book_service/Dockerfile index ab2f40b..ded1775 100644 --- a/tsukanova_irina_lab_3/book_service/Dockerfile +++ b/tsukanova_irina_lab_3/book_service/Dockerfile @@ -5,7 +5,7 @@ FROM python:3.12-slim WORKDIR /app # Копирую файл requirements.txt в контейнер -COPY book_service/requirements.txt . +COPY requirements.txt . # Устанавливаю зависимости RUN pip install --no-cache-dir -r requirements.txt diff --git a/tsukanova_irina_lab_3/book_service/book_service.py b/tsukanova_irina_lab_3/book_service/book_service.py index 67b75ba..6ce953a 100644 --- a/tsukanova_irina_lab_3/book_service/book_service.py +++ b/tsukanova_irina_lab_3/book_service/book_service.py @@ -56,7 +56,7 @@ books: list[Book] = [ author_id='eb815350-c7b9-4446-8434-4c0640c21995'), ] -authors_url = 'http://localhost:5000/' +authors_url = 'http://author_service:20001/' def list_jsonify(): diff --git a/tsukanova_irina_lab_3/book_service/requirements.txt b/tsukanova_irina_lab_3/book_service/requirements.txt deleted file mode 100644 index 41917d8efabe387e38bb9f8a0987aa512ef04fea..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 70 zcmezW&y696A(5e&A)CP#2#p!^7z}{en1Pppi=l`im7$QK6i5~`lmIbEg%Ma5LW0zQ FFaYE|3cLUS diff --git a/tsukanova_irina_lab_3/docker-compose.yaml b/tsukanova_irina_lab_3/docker-compose.yaml index b9cd210..bd9f0f6 100644 --- a/tsukanova_irina_lab_3/docker-compose.yaml +++ b/tsukanova_irina_lab_3/docker-compose.yaml @@ -21,7 +21,7 @@ services: ports: - "80:80" volumes: - - ./nginx/nginx.conf:/etc/nginx/nginx.conf + - ./nginx.conf:/etc/nginx/nginx.conf depends_on: - author_service - book_service \ No newline at end of file diff --git a/tsukanova_irina_lab_3/nginx/nginx.conf b/tsukanova_irina_lab_3/nginx.conf similarity index 100% rename from tsukanova_irina_lab_3/nginx/nginx.conf rename to tsukanova_irina_lab_3/nginx.conf diff --git a/tsukanova_irina_lab_3/nginx/Dockerfile b/tsukanova_irina_lab_3/nginx/Dockerfile deleted file mode 100644 index 65f3ec5..0000000 --- a/tsukanova_irina_lab_3/nginx/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM ubuntu:latest -LABEL authors="Main" - -ENTRYPOINT ["top", "-b"] \ No newline at end of file