From 5ff5bf22b1e7aaa99776e4957b6ee725ab2d484e Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Thu, 10 Oct 2024 22:53:30 +0400 Subject: [PATCH] lab 3 no swagger --- presnyakova_victoria_lab_3/docker-compose.yml | 27 ++++++++++ .../event_service/Dockerfile | 11 ++++ .../event_service/event_service.py | 48 +++++++++++++++++ presnyakova_victoria_lab_3/nginx.conf | 28 ++++++++++ .../session_service/Dockerfile | 11 ++++ .../session_service/session_service.py | 54 +++++++++++++++++++ 6 files changed, 179 insertions(+) create mode 100644 presnyakova_victoria_lab_3/docker-compose.yml create mode 100644 presnyakova_victoria_lab_3/event_service/Dockerfile create mode 100644 presnyakova_victoria_lab_3/event_service/event_service.py create mode 100644 presnyakova_victoria_lab_3/nginx.conf create mode 100644 presnyakova_victoria_lab_3/session_service/Dockerfile create mode 100644 presnyakova_victoria_lab_3/session_service/session_service.py diff --git a/presnyakova_victoria_lab_3/docker-compose.yml b/presnyakova_victoria_lab_3/docker-compose.yml new file mode 100644 index 0000000..20a10c6 --- /dev/null +++ b/presnyakova_victoria_lab_3/docker-compose.yml @@ -0,0 +1,27 @@ +version: '3.8' +services: + event_service: + build: + context: /event_service + dockerfile: Dockerfile + depends_on: + - session_service + expose: + - 8081 + + session_service: + build: + context: /session_service + dockerfile: Dockerfile + expose: + - 8082 + + nginx: + image: nginx + ports: + - 8086:8086 + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf + depends_on: + - event_service + - session_service diff --git a/presnyakova_victoria_lab_3/event_service/Dockerfile b/presnyakova_victoria_lab_3/event_service/Dockerfile new file mode 100644 index 0000000..4d1817a --- /dev/null +++ b/presnyakova_victoria_lab_3/event_service/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.9 + +EXPOSE 8081 + +RUN pip install Flask requests flasgger + +WORKDIR /work + +COPY event_service.py ./ + +CMD ["python", "event_service.py"] diff --git a/presnyakova_victoria_lab_3/event_service/event_service.py b/presnyakova_victoria_lab_3/event_service/event_service.py new file mode 100644 index 0000000..b346216 --- /dev/null +++ b/presnyakova_victoria_lab_3/event_service/event_service.py @@ -0,0 +1,48 @@ +from flask import Flask, jsonify, request, Response +from datetime import datetime + +app = Flask(__name__) + +events = {0: {'id': 0, 'session': 0, 'info': '{"defects": 1}', 'dt': datetime(2024, 10, 10, 8, 0)}, + 1: {'id': 1, 'session': 1, 'info': '{"defects": 1}', 'dt': datetime(2024, 10, 10, 8, 5)}, + 2: {'id': 2, 'session': 0, 'info': '{"defects": 1}', 'dt': datetime(2024, 10, 10, 8, 10)}, + 3: {'id': 3, 'session': 1, 'info': '{"defects": 1}', 'dt': datetime(2024, 10, 10, 8, 15)}} + + +@app.route('/', methods=['GET', 'POST']) +def get_all(): + if request.method == 'POST': + dto = request.get_json()['dto'] + new_id = max(events.keys()) + 1 + events[new_id] = { + 'id': new_id, + 'session': dto['session'], + 'info': dto['info'], + 'dt': datetime.now() + } + return jsonify(events[new_id]) + return jsonify(events) + + +@app.route('/', methods=['GET', 'PUT', 'DELETE']) +def get_by_id(msg_id): + if msg_id not in events.keys(): + return Response(status=404) + + if request.method == 'PUT': + dto = request.get_json()['dto'] + events[msg_id] = { + 'session': dto['session'], + 'info': dto['info'], + 'dt': datetime.now() + } + return events[msg_id] + elif request.method == 'DELETE': + events.pop(msg_id) + return Response(status=200) + + return jsonify(events[msg_id]) + + +if __name__ == '__main__': + app.run(host='0.0.0.0', use_reloader=False, port=8081) diff --git a/presnyakova_victoria_lab_3/nginx.conf b/presnyakova_victoria_lab_3/nginx.conf new file mode 100644 index 0000000..290c17b --- /dev/null +++ b/presnyakova_victoria_lab_3/nginx.conf @@ -0,0 +1,28 @@ +events { + worker_connections 1024; +} + +http { + server { + listen 8086; + listen [::]:8086; + server_name localhost; + + location /event_service/ { + proxy_pass http://event_service:8081/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-Proto $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Prefix $scheme; + } + + location /session_service/ { + proxy_pass http://session_service:8082/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-Proto $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Prefix $scheme; + } + + } +} \ No newline at end of file diff --git a/presnyakova_victoria_lab_3/session_service/Dockerfile b/presnyakova_victoria_lab_3/session_service/Dockerfile new file mode 100644 index 0000000..ff15900 --- /dev/null +++ b/presnyakova_victoria_lab_3/session_service/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.9 + +EXPOSE 8082 + +RUN pip install Flask requests flasgger + +WORKDIR /work + +COPY session_service.py ./ + +CMD ["python", "session_service.py"] diff --git a/presnyakova_victoria_lab_3/session_service/session_service.py b/presnyakova_victoria_lab_3/session_service/session_service.py new file mode 100644 index 0000000..1b0887f --- /dev/null +++ b/presnyakova_victoria_lab_3/session_service/session_service.py @@ -0,0 +1,54 @@ +from flask import Flask, jsonify, request, Response +from datetime import datetime + +app = Flask(__name__) + +sessions = {0: {'id': 0, 'info': '{"distance": 1}', 'dt_start': datetime(2024, 10, 10, 8, 0), + 'dt_end': datetime(2024, 10, 11, 8, 0)}, + 1: {'id': 1, 'info': '{"distance": 1}', 'dt_start': datetime(2024, 10, 12, 8, 0), + 'dt_end': datetime(2024, 10, 13, 8, 0)}, + 2: {'id': 2, 'info': '{"distance": 1}', 'dt_start': datetime(2024, 10, 14, 8, 0), + 'dt_end': datetime(2024, 10, 15, 8, 0)}, + 3: {'id': 3, 'info': '{"distance": 1}', 'dt_start': datetime(2024, 10, 16, 8, 0), + 'dt_end': datetime(2024, 10, 17, 8, 0)}, + } + + +@app.route('/', methods=['GET', 'POST']) +def get_all(): + if request.method == 'POST': + dto = request.get_json()['dto'] + new_id = max(sessions.keys()) + 1 + sessions[new_id] = { + 'id': new_id, + 'info': dto['info'], + 'dt_start': datetime.now(), + 'dt_end': datetime.now() + datetime.timedelta(days=1) + + } + return jsonify(sessions[new_id]) + return jsonify(sessions) + + +@app.route('/', methods=['GET', 'PUT', 'DELETE']) +def get_by_id(msg_id): + if msg_id not in sessions.keys(): + return Response(status=404) + + if request.method == 'PUT': + dto = request.get_json()['dto'] + sessions[msg_id] = { + 'info': dto['info'], + 'dt_start': datetime.now(), + 'dt_end': datetime.now() + datetime.timedelta(days=1) + } + return sessions[msg_id] + elif request.method == 'DELETE': + sessions.pop(msg_id) + return Response(status=200) + + return jsonify(sessions[msg_id]) + + +if __name__ == '__main__': + app.run(host='0.0.0.0', use_reloader=False, port=8082)