Compare commits

...

1 Commits

Author SHA1 Message Date
5ff5bf22b1 lab 3 no swagger 2024-10-10 22:53:30 +04:00
6 changed files with 179 additions and 0 deletions

View File

@ -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

View File

@ -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"]

View File

@ -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('/<int:msg_id>', 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)

View File

@ -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;
}
}
}

View File

@ -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"]

View File

@ -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('/<int:msg_id>', 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)