From 1e11f844d16fd3f4ab3c9e963679417687ac80d6 Mon Sep 17 00:00:00 2001 From: sodaler Date: Sun, 21 Jan 2024 19:06:12 +0400 Subject: [PATCH] degtyarev_mikhail_lab_3 is ready --- .../category_service/Dockerfile | 6 ++ .../category_service/category_service.py | 87 ++++++++++++++++++ .../category_service/requirements.txt | 3 + degtyarev_mikhail_lab_3/docker-compose.yaml | 37 ++++++++ degtyarev_mikhail_lab_3/img.png | Bin 0 -> 7570 bytes degtyarev_mikhail_lab_3/nginx/Dockerfile | 4 + degtyarev_mikhail_lab_3/nginx/nginx.conf | 27 ++++++ .../post_service/Dockerfile | 6 ++ .../post_service/post_service.py | 77 ++++++++++++++++ .../post_service/requirements.txt | 3 + 10 files changed, 250 insertions(+) create mode 100644 degtyarev_mikhail_lab_3/category_service/Dockerfile create mode 100644 degtyarev_mikhail_lab_3/category_service/category_service.py create mode 100644 degtyarev_mikhail_lab_3/category_service/requirements.txt create mode 100644 degtyarev_mikhail_lab_3/docker-compose.yaml create mode 100644 degtyarev_mikhail_lab_3/img.png create mode 100644 degtyarev_mikhail_lab_3/nginx/Dockerfile create mode 100644 degtyarev_mikhail_lab_3/nginx/nginx.conf create mode 100644 degtyarev_mikhail_lab_3/post_service/Dockerfile create mode 100644 degtyarev_mikhail_lab_3/post_service/post_service.py create mode 100644 degtyarev_mikhail_lab_3/post_service/requirements.txt diff --git a/degtyarev_mikhail_lab_3/category_service/Dockerfile b/degtyarev_mikhail_lab_3/category_service/Dockerfile new file mode 100644 index 0000000..8b7a568 --- /dev/null +++ b/degtyarev_mikhail_lab_3/category_service/Dockerfile @@ -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"] \ No newline at end of file diff --git a/degtyarev_mikhail_lab_3/category_service/category_service.py b/degtyarev_mikhail_lab_3/category_service/category_service.py new file mode 100644 index 0000000..351eb43 --- /dev/null +++ b/degtyarev_mikhail_lab_3/category_service/category_service.py @@ -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/', 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/', 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/_', 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/', 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) diff --git a/degtyarev_mikhail_lab_3/category_service/requirements.txt b/degtyarev_mikhail_lab_3/category_service/requirements.txt new file mode 100644 index 0000000..3d70f32 --- /dev/null +++ b/degtyarev_mikhail_lab_3/category_service/requirements.txt @@ -0,0 +1,3 @@ +Flask==3.0.0 +requests==2.31.0 +gunicorn==21.2.0 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_3/docker-compose.yaml b/degtyarev_mikhail_lab_3/docker-compose.yaml new file mode 100644 index 0000000..39a3a1d --- /dev/null +++ b/degtyarev_mikhail_lab_3/docker-compose.yaml @@ -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 \ No newline at end of file diff --git a/degtyarev_mikhail_lab_3/img.png b/degtyarev_mikhail_lab_3/img.png new file mode 100644 index 0000000000000000000000000000000000000000..4134ee4df9612e4b18ff89493147ebc849e355b5 GIT binary patch literal 7570 zcmch6cTiK?+ins9gies=p#*{y1u3EwL3#v4iUlGy@gNpJx`05aA_7trF(6$~P&!D5 z07@04i1aEQB=jBvcZcISbMEhc-#2q-?%Y3;mAz+Wt+n6sywCIQP$L6vCi*|ef_5>k_M;teE-C6rLZH z?`H=qU67i?rT#bt@or9y5M5i*J*J%#RzpA{A{S=a&jLG9eR`SDn@on4dp0}R&%oq;pY4ivCI_C_OcBkB96bxIjT?#obdjcLUCb&CF z^oXHW3#{j3*uN%UMlW`fTeI`q8Atxd>FokTE=3?6 z>2hmz)y8k>-%^_*-hZpxzsmlpKJ{!z38;tIRDY{umEU`u8{!LZyEifN$j2_9Q`mk* z*|A*GJ_?JAQMKV>)A-^iXm)rQ+Ikg*WjkK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hdWCo1zW#c!Xx)m~#;e#y#uRD@!HL$!KW-UMRvXow1@ zY-Xxsj@eQ0u4I%6*erhAK6R;?Zy?Pzr!9Ug+@En)UaqWRAvnO(b@M}@C4U=7z`=7k zSXX*3rL@|3(r7Tk(bFbewt(Jhqe%|o(>V4ikDvjVCC&Ab1j69$5f%}d7&xHfmUR7; zjU_H(E2xP|U!Y6QE%K3KOfc+VCKz!NoEPbb+oqnX)`hyvSy3}Ph_|@4nn71Be9uVc zs@~iCn&d4AnQ>3`<BKUISuLMT!`MMoDn2=ijmSW+^d@!Kdb>>XfhmM a`EgYicyl&tmH_WsLAoddtvn5E;Qs>l4_', 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//', 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/', 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_', 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) diff --git a/degtyarev_mikhail_lab_3/post_service/requirements.txt b/degtyarev_mikhail_lab_3/post_service/requirements.txt new file mode 100644 index 0000000..3d70f32 --- /dev/null +++ b/degtyarev_mikhail_lab_3/post_service/requirements.txt @@ -0,0 +1,3 @@ +Flask==3.0.0 +requests==2.31.0 +gunicorn==21.2.0 \ No newline at end of file -- 2.25.1