forked from Alexey/DAS_2024_1
еще и еще запросы
This commit is contained in:
parent
c3537b5abe
commit
8b96102dbd
@ -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/<uuid:uuid_>', 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_)
|
||||
|
||||
|
20
tsukanova_irina_lab_3/book_service/Dockerfile
Normal file
20
tsukanova_irina_lab_3/book_service/Dockerfile
Normal file
@ -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"]
|
@ -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/<uuid:author_uuid>', 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('/<uuid:uuid_>', 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/<uuid:uuid_>', 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('/<uuid:uuid_>', 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('/<uuid:uuid_>', 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)
|
||||
|
@ -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
|
4
tsukanova_irina_lab_3/nginx/Dockerfile
Normal file
4
tsukanova_irina_lab_3/nginx/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM ubuntu:latest
|
||||
LABEL authors="Main"
|
||||
|
||||
ENTRYPOINT ["top", "-b"]
|
22
tsukanova_irina_lab_3/nginx/nginx.conf
Normal file
22
tsukanova_irina_lab_3/nginx/nginx.conf
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user