degtyarev_mikhail_lab_3 is ready
This commit is contained in:
parent
a346187851
commit
1e11f844d1
6
degtyarev_mikhail_lab_3/category_service/Dockerfile
Normal file
6
degtyarev_mikhail_lab_3/category_service/Dockerfile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FROM python:3.11
|
||||||
|
WORKDIR /app
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
COPY . /app
|
||||||
|
CMD ["gunicorn", "--bind", "0.0.0.0:5001", "category_service:app"]
|
87
degtyarev_mikhail_lab_3/category_service/category_service.py
Normal file
87
degtyarev_mikhail_lab_3/category_service/category_service.py
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
# category_service.py
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
from flask import Flask, request, jsonify
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
categories = []
|
||||||
|
|
||||||
|
@app.route('/event', methods=['POST'])
|
||||||
|
def event():
|
||||||
|
data = request.get_json()
|
||||||
|
print(data)
|
||||||
|
input_category_id = data.get('category_id')
|
||||||
|
|
||||||
|
if data['action'] == 'create':
|
||||||
|
category = next((category for category in categories if category['id'] == input_category_id), None)
|
||||||
|
if category:
|
||||||
|
post_id = len(category.get('posts', [])) + 1
|
||||||
|
post = {'id': post_id, 'post': data['post']}
|
||||||
|
category.setdefault('posts', []).append(post)
|
||||||
|
print(f"Post created for category_id {input_category_id}: {post}")
|
||||||
|
else:
|
||||||
|
print(f"Category not found for post creation: {input_category_id}")
|
||||||
|
|
||||||
|
elif data['action'] == 'update':
|
||||||
|
category = next((category for category in categories if category['id'] == input_category_id), None)
|
||||||
|
if category:
|
||||||
|
post_id = data.get('id')
|
||||||
|
post = next((p for p in category['posts'] if p['id'] == post_id), None)
|
||||||
|
if post:
|
||||||
|
post['post'] = data['post']
|
||||||
|
print(f"Post updated for category_id {input_category_id}: {post}")
|
||||||
|
else:
|
||||||
|
print(f"Post not found for update: {post_id}")
|
||||||
|
else:
|
||||||
|
print(f"Category not found for post update: {input_category_id}")
|
||||||
|
|
||||||
|
elif data['action'] == 'delete':
|
||||||
|
category = next((category for category in categories if category['id'] == input_category_id), None)
|
||||||
|
if category:
|
||||||
|
post_id = category.get('id')
|
||||||
|
category['posts'] = [p for p in category.get('posts', []) if p['id'] != post_id]
|
||||||
|
print(f"Post deleted for category_id {input_category_id}: {post_id}")
|
||||||
|
else:
|
||||||
|
print(f"Category not found for post deletion: {input_category_id}")
|
||||||
|
return jsonify({'result': True})
|
||||||
|
|
||||||
|
@app.route('/', methods=['GET'])
|
||||||
|
def show_categories():
|
||||||
|
return jsonify(categories)
|
||||||
|
|
||||||
|
@app.route('/add/<string:name>', methods=['POST'])
|
||||||
|
def create_category(name):
|
||||||
|
if request.method == 'POST':
|
||||||
|
if len(categories) == 0:
|
||||||
|
id = 1
|
||||||
|
else:
|
||||||
|
id = categories[-1]['id'] + 1
|
||||||
|
category = {'id': id, 'name': name}
|
||||||
|
categories.append(category)
|
||||||
|
return jsonify(category)
|
||||||
|
|
||||||
|
@app.route('/get/<int:id>', methods=['GET'])
|
||||||
|
def get_category(id):
|
||||||
|
found_category = next((category for category in categories if category['id'] == id), None)
|
||||||
|
if found_category:
|
||||||
|
return jsonify(found_category)
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Category not found'})
|
||||||
|
|
||||||
|
@app.route('/upd/<int:id>_<string:name>', methods=['PUT'])
|
||||||
|
def update_category(id, name):
|
||||||
|
category = next((category for category in categories if category['id'] == id), None)
|
||||||
|
if category:
|
||||||
|
category['name'] = name
|
||||||
|
return jsonify(category)
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Category not found'})
|
||||||
|
|
||||||
|
@app.route('/del/<int:id>', methods=['DELETE'])
|
||||||
|
def delete_category(id):
|
||||||
|
global categories
|
||||||
|
categories = [category for category in categories if category['id'] != id]
|
||||||
|
return jsonify({'result': True})
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(port=5001)
|
@ -0,0 +1,3 @@
|
|||||||
|
Flask==3.0.0
|
||||||
|
requests==2.31.0
|
||||||
|
gunicorn==21.2.0
|
37
degtyarev_mikhail_lab_3/docker-compose.yaml
Normal file
37
degtyarev_mikhail_lab_3/docker-compose.yaml
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
category-service:
|
||||||
|
build:
|
||||||
|
context: ./category_service
|
||||||
|
ports:
|
||||||
|
- "5001:5001"
|
||||||
|
networks:
|
||||||
|
- app
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
post-service:
|
||||||
|
build:
|
||||||
|
context: ./post_service
|
||||||
|
ports:
|
||||||
|
- "5002:5002"
|
||||||
|
networks:
|
||||||
|
- app
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: nginx:latest
|
||||||
|
ports:
|
||||||
|
- "80:80"
|
||||||
|
depends_on:
|
||||||
|
- category-service
|
||||||
|
- post-service
|
||||||
|
networks:
|
||||||
|
- app
|
||||||
|
volumes:
|
||||||
|
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
|
||||||
|
restart: always
|
||||||
|
|
||||||
|
networks:
|
||||||
|
app:
|
||||||
|
driver: bridge
|
BIN
degtyarev_mikhail_lab_3/img.png
Normal file
BIN
degtyarev_mikhail_lab_3/img.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.4 KiB |
4
degtyarev_mikhail_lab_3/nginx/Dockerfile
Normal file
4
degtyarev_mikhail_lab_3/nginx/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
FROM nginx
|
||||||
|
COPY nginx.conf /etc/nginx/nginx.conf
|
||||||
|
EXPOSE 80
|
||||||
|
CMD ["nginx", "-g", "daemon off;"]
|
27
degtyarev_mikhail_lab_3/nginx/nginx.conf
Normal file
27
degtyarev_mikhail_lab_3/nginx/nginx.conf
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
events {
|
||||||
|
worker_connections 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
http {
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
listen [::]:80;
|
||||||
|
server_name localhost;
|
||||||
|
|
||||||
|
location /category-service/ {
|
||||||
|
proxy_pass http://category-service:5001/;
|
||||||
|
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 /post-service/ {
|
||||||
|
proxy_pass http://post-service:5002/;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
degtyarev_mikhail_lab_3/post_service/Dockerfile
Normal file
6
degtyarev_mikhail_lab_3/post_service/Dockerfile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FROM python:3.11
|
||||||
|
WORKDIR /app
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
COPY . /app
|
||||||
|
CMD ["gunicorn", "--bind", "0.0.0.0:5002", "post_service:app"]
|
77
degtyarev_mikhail_lab_3/post_service/post_service.py
Normal file
77
degtyarev_mikhail_lab_3/post_service/post_service.py
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
from json import dump
|
||||||
|
|
||||||
|
from flask import Flask, request, jsonify
|
||||||
|
import requests
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
posts = []
|
||||||
|
|
||||||
|
@app.route('/all', methods=['GET'])
|
||||||
|
def show_posts():
|
||||||
|
return jsonify(posts)
|
||||||
|
|
||||||
|
@app.route('/add/<int:category_id>_<string:post>', methods=['POST'])
|
||||||
|
def create_post(category_id, post):
|
||||||
|
new_post = {
|
||||||
|
'id': len(posts) + 1,
|
||||||
|
'category_id': category_id,
|
||||||
|
'post': post,
|
||||||
|
'action': 'create'
|
||||||
|
}
|
||||||
|
posts.append(new_post)
|
||||||
|
|
||||||
|
response = requests.post("http://category-service:5001/event", json=new_post)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify(new_post)
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Failed to create post'})
|
||||||
|
|
||||||
|
@app.route('/upd/<int:post_id>/<string:new_post>', methods=['PUT'])
|
||||||
|
def update_post(post_id, new_post):
|
||||||
|
post = next((p for p in posts if p.get('id') == post_id), None)
|
||||||
|
if post:
|
||||||
|
post['post'] = new_post
|
||||||
|
post['action'] = 'update'
|
||||||
|
|
||||||
|
response = requests.post("http://category-service:5001/event", json=post)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify({'message': 'Post updated successfully'})
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Failed to update post'})
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Post not found'})
|
||||||
|
|
||||||
|
@app.route('/get/<int:post_id>', methods=['GET'])
|
||||||
|
def get_post(post_id):
|
||||||
|
found_post = next((p for p in posts if p.get('id') == post_id), None)
|
||||||
|
if found_post:
|
||||||
|
read_post = {'id': found_post['id'],
|
||||||
|
'category_id': found_post['category_id'],
|
||||||
|
'post': found_post['post']}
|
||||||
|
return jsonify(read_post)
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Post not found'})
|
||||||
|
|
||||||
|
@app.route('/post/del_<int:post_id>', methods=['DELETE'])
|
||||||
|
def delete_post(post_id):
|
||||||
|
global posts
|
||||||
|
deleted_post = next((p for p in posts if p.get('id') == post_id), None)
|
||||||
|
|
||||||
|
if deleted_post:
|
||||||
|
posts = [p for p in posts if p.get('id') != post_id]
|
||||||
|
deleted_post['action'] = 'delete'
|
||||||
|
|
||||||
|
response = requests.post("http://category-service:5001/event", json=deleted_post)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify({'result': True})
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Failed to delete post'})
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Post not found'})
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(port=5002)
|
3
degtyarev_mikhail_lab_3/post_service/requirements.txt
Normal file
3
degtyarev_mikhail_lab_3/post_service/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Flask==3.0.0
|
||||||
|
requests==2.31.0
|
||||||
|
gunicorn==21.2.0
|
Loading…
Reference in New Issue
Block a user