Почти сделано. Исправить связи и сделать readme.md
This commit is contained in:
parent
5aa2cae670
commit
1e9bdf2806
1
kashin_maxim_lab_3/.gitignore
vendored
Normal file
1
kashin_maxim_lab_3/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.venv
|
53
kashin_maxim_lab_3/docker-compose.yml
Normal file
53
kashin_maxim_lab_3/docker-compose.yml
Normal file
@ -0,0 +1,53 @@
|
||||
version: '3.8'
|
||||
services:
|
||||
university_service:
|
||||
build:
|
||||
context: ./university_service
|
||||
depends_on:
|
||||
- university_db
|
||||
environment:
|
||||
- FLASK_APP=app.py
|
||||
expose:
|
||||
- 8081
|
||||
|
||||
faculty_service:
|
||||
build:
|
||||
context: ./faculty_service
|
||||
depends_on:
|
||||
- faculty_db
|
||||
environment:
|
||||
- FLASK_APP=app.py
|
||||
expose:
|
||||
- 8082
|
||||
|
||||
university_db:
|
||||
image: postgres
|
||||
environment:
|
||||
POSTGRES_USER: university_user
|
||||
POSTGRES_PASSWORD: password
|
||||
POSTGRES_DB: universitydb
|
||||
ports:
|
||||
- "5433:5432"
|
||||
volumes:
|
||||
- ./university_service/init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||
|
||||
faculty_db:
|
||||
image: postgres
|
||||
environment:
|
||||
POSTGRES_USER: faculty_user
|
||||
POSTGRES_PASSWORD: password
|
||||
POSTGRES_DB: facultydb
|
||||
ports:
|
||||
- "5434:5432"
|
||||
volumes:
|
||||
- ./faculty_service/init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||
|
||||
nginx:
|
||||
image: nginx
|
||||
ports:
|
||||
- 8086:8086
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf
|
||||
depends_on:
|
||||
- university_service
|
||||
- faculty_service
|
22
kashin_maxim_lab_3/faculty_service/Dockerfile
Normal file
22
kashin_maxim_lab_3/faculty_service/Dockerfile
Normal file
@ -0,0 +1,22 @@
|
||||
FROM python:3.9-slim
|
||||
|
||||
# Установка зависимостей
|
||||
RUN apt-get update && apt-get install -y \
|
||||
gcc \
|
||||
libpq-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Установка рабочей директории
|
||||
WORKDIR /app
|
||||
|
||||
# Копирование файлов
|
||||
COPY requirements.txt requirements.txt
|
||||
|
||||
# Установка Python-зависимостей
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Копирование всех файлов приложения
|
||||
COPY . .
|
||||
|
||||
# Запуск приложения
|
||||
CMD ["python", "app.py"]
|
42
kashin_maxim_lab_3/faculty_service/app.py
Normal file
42
kashin_maxim_lab_3/faculty_service/app.py
Normal file
@ -0,0 +1,42 @@
|
||||
from flask import Flask, jsonify, request, Response
|
||||
from service import FacultyService
|
||||
from db import db
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://faculty_user:password@faculty_db:5432/facultydb'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db.init_app(app)
|
||||
|
||||
faculty_service = FacultyService()
|
||||
|
||||
|
||||
@app.route('/faculties', methods=['GET', 'POST'])
|
||||
def handle_faculties():
|
||||
if request.method == 'POST':
|
||||
dto = request.get_json()['dto']
|
||||
faculty = faculty_service.create_faculty(dto['name'], dto['university_id'])
|
||||
return jsonify({"id": faculty.id, "name": faculty.name, "university_id": faculty.university_id})
|
||||
|
||||
return jsonify(faculty_service.get_all_faculties())
|
||||
|
||||
|
||||
@app.route('/faculties/<int:faculty_id>', methods=['GET', 'PUT', 'DELETE'])
|
||||
def handle_faculty(faculty_id):
|
||||
if request.method == 'PUT':
|
||||
dto = request.get_json()['dto']
|
||||
updated_faculty = faculty_service.update_faculty(faculty_id, dto['name'])
|
||||
if updated_faculty:
|
||||
return jsonify(updated_faculty)
|
||||
else:
|
||||
return Response(status=404)
|
||||
|
||||
elif request.method == 'DELETE':
|
||||
faculty_service.delete_faculty(faculty_id)
|
||||
return Response(status=200)
|
||||
|
||||
university = faculty_service.get_faculty(faculty_id)
|
||||
return jsonify({"id": university.id, "name": university.name, "university_id": university.university_id})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', use_reloader=False, port=8082)
|
15
kashin_maxim_lab_3/faculty_service/db.py
Normal file
15
kashin_maxim_lab_3/faculty_service/db.py
Normal file
@ -0,0 +1,15 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
class Faculty(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(100), nullable=False)
|
||||
university_id = db.Column(db.Integer, nullable=False)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"university_id": self.university_id
|
||||
}
|
5
kashin_maxim_lab_3/faculty_service/init.sql
Normal file
5
kashin_maxim_lab_3/faculty_service/init.sql
Normal file
@ -0,0 +1,5 @@
|
||||
CREATE TABLE IF NOT EXISTS faculty (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
university_id INT NOT NULL
|
||||
);
|
3
kashin_maxim_lab_3/faculty_service/requirements.txt
Normal file
3
kashin_maxim_lab_3/faculty_service/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
flask
|
||||
flask_sqlalchemy
|
||||
psycopg2
|
28
kashin_maxim_lab_3/faculty_service/service.py
Normal file
28
kashin_maxim_lab_3/faculty_service/service.py
Normal file
@ -0,0 +1,28 @@
|
||||
from db import db, Faculty
|
||||
|
||||
class FacultyService:
|
||||
def get_all_faculties(self):
|
||||
return [faculty.to_dict() for faculty in Faculty.query.all()]
|
||||
|
||||
def get_faculty(self, faculty_id):
|
||||
return Faculty.query.get(faculty_id)
|
||||
|
||||
def create_faculty(self, name, university_id):
|
||||
faculty = Faculty(name=name, university_id=university_id)
|
||||
db.session.add(faculty)
|
||||
db.session.commit()
|
||||
return faculty.to_dict()
|
||||
|
||||
def update_faculty(self, faculty_id, name):
|
||||
faculty = self.get_faculty(faculty_id)
|
||||
if faculty:
|
||||
faculty.name = name
|
||||
db.session.commit()
|
||||
return faculty.to_dict()
|
||||
return None
|
||||
|
||||
def delete_faculty(self, faculty_id):
|
||||
faculty = self.get_faculty(faculty_id)
|
||||
if faculty:
|
||||
db.session.delete(faculty)
|
||||
db.session.commit()
|
27
kashin_maxim_lab_3/nginx.conf
Normal file
27
kashin_maxim_lab_3/nginx.conf
Normal file
@ -0,0 +1,27 @@
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
server {
|
||||
listen 8086;
|
||||
listen [::]:8086;
|
||||
server_name localhost;
|
||||
|
||||
location /university_service/ {
|
||||
proxy_pass http://university_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 /faculty_service/ {
|
||||
proxy_pass http://faculty_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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +1,3 @@
|
||||
# Тут будет отчёт.
|
||||
# Кашин Максим ПИбд-42
|
||||
|
||||
# Тематика: Учебная групп и студенты
|
22
kashin_maxim_lab_3/university_service/Dockerfile
Normal file
22
kashin_maxim_lab_3/university_service/Dockerfile
Normal file
@ -0,0 +1,22 @@
|
||||
FROM python:3.9-slim
|
||||
|
||||
# Установка зависимостей
|
||||
RUN apt-get update && apt-get install -y \
|
||||
gcc \
|
||||
libpq-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Установка рабочей директории
|
||||
WORKDIR /app
|
||||
|
||||
# Копирование файлов
|
||||
COPY requirements.txt requirements.txt
|
||||
|
||||
# Установка Python-зависимостей
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Копирование всех файлов приложения
|
||||
COPY . .
|
||||
|
||||
# Запуск приложения
|
||||
CMD ["python", "app.py"]
|
42
kashin_maxim_lab_3/university_service/app.py
Normal file
42
kashin_maxim_lab_3/university_service/app.py
Normal file
@ -0,0 +1,42 @@
|
||||
from flask import Flask, jsonify, request, Response
|
||||
from service import UniversityService
|
||||
from db import db
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://university_user:password@university_db:5432/universitydb'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db.init_app(app)
|
||||
|
||||
university_service = UniversityService()
|
||||
|
||||
|
||||
@app.route('/universities', methods=['GET', 'POST'])
|
||||
def handle_universities():
|
||||
if request.method == 'POST':
|
||||
dto = request.get_json()['dto']
|
||||
university = university_service.create_university(dto['name'])
|
||||
return jsonify({"id": university.id, "name": university.name, "established_at": university.established_at})
|
||||
|
||||
universities = university_service.get_all_universities()
|
||||
return jsonify(universities)
|
||||
|
||||
|
||||
@app.route('/universities/<int:university_id>', methods=['GET', 'PUT', 'DELETE'])
|
||||
def handle_university(university_id):
|
||||
if request.method == 'PUT':
|
||||
dto = request.get_json()['dto']
|
||||
updated_university = university_service.update_university(university_id, dto['name'])
|
||||
return jsonify(updated_university) if updated_university else Response(status=404)
|
||||
|
||||
elif request.method == 'DELETE':
|
||||
university_service.delete_university(university_id)
|
||||
return Response(status=200)
|
||||
|
||||
university = university_service.get_university(university_id)
|
||||
if university is None:
|
||||
return Response(status=404)
|
||||
return jsonify(university)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', use_reloader=False, port=8081)
|
16
kashin_maxim_lab_3/university_service/db.py
Normal file
16
kashin_maxim_lab_3/university_service/db.py
Normal file
@ -0,0 +1,16 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from datetime import datetime
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
class University(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(100), nullable=False)
|
||||
established_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"established_at": self.established_at.isoformat()
|
||||
}
|
4
kashin_maxim_lab_3/university_service/init.sql
Normal file
4
kashin_maxim_lab_3/university_service/init.sql
Normal file
@ -0,0 +1,4 @@
|
||||
CREATE TABLE IF NOT EXISTS university (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL
|
||||
);
|
3
kashin_maxim_lab_3/university_service/requirements.txt
Normal file
3
kashin_maxim_lab_3/university_service/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
flask
|
||||
flask_sqlalchemy
|
||||
psycopg2
|
30
kashin_maxim_lab_3/university_service/service.py
Normal file
30
kashin_maxim_lab_3/university_service/service.py
Normal file
@ -0,0 +1,30 @@
|
||||
from db import db, University
|
||||
|
||||
class UniversityService:
|
||||
def get_all_universities(self):
|
||||
return [university.to_dict() for university in University.query.all()]
|
||||
|
||||
def get_university(self, university_id):
|
||||
university = University.query.get(university_id)
|
||||
return university.to_dict() if university else None
|
||||
|
||||
def create_university(self, name):
|
||||
university = University(name=name)
|
||||
db.session.add(university)
|
||||
db.session.commit()
|
||||
return university.to_dict()
|
||||
|
||||
def update_university(self, university_id, name):
|
||||
university = self.get_university(university_id)
|
||||
if university:
|
||||
university_obj = University.query.get(university_id)
|
||||
university_obj.name = name
|
||||
db.session.commit()
|
||||
return university_obj.to_dict()
|
||||
return None
|
||||
|
||||
def delete_university(self, university_id):
|
||||
university = self.get_university(university_id)
|
||||
if university:
|
||||
db.session.delete(University.query.get(university_id))
|
||||
db.session.commit()
|
Loading…
Reference in New Issue
Block a user