/
ingvion
/
PlanerService
Обзор
Документация
Войти
/
ingvion
/
PlanerService
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Backend/main.py
90 строк
3 KB
lenadekart
Добавлено управление навыками в админ панели. Улучшения фронтенда
23 ноя 2025, 02:32
23 ноя 2025, 02:32
b0a3c04
Код
Авторство
О чём код?
from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware import uvicorn import sys import asyncio # Фикс для Windows и asyncpg if sys.platform == 'win32': asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) from app.core.db_core import engine from app.core.base import Base from app.api.v1 import main_router app = FastAPI() # Настройка CORS origins = [ "http://localhost:3000", # Frontend (React/Vite) "http://127.0.0.1:3000", "http://localhost:80", # Production Frontend ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(main_router) @app.on_event("startup") async def on_startup(): async with engine.begin() as conn: # Миграция: добавляем direction_id и specialization_id в students, если их нет from sqlalchemy import text, inspect def check_and_add_columns(connection): inspector = inspect(connection) # Миграция для students students_columns = [col['name'] for col in inspector.get_columns('students')] if 'direction_id' not in students_columns: connection.execute(text( "ALTER TABLE students ADD COLUMN direction_id INTEGER" )) connection.execute(text( "CREATE INDEX IF NOT EXISTS ix_students_direction_id ON students(direction_id)" )) print("Added direction_id column to students table") if 'specialization_id' not in students_columns: connection.execute(text( "ALTER TABLE students ADD COLUMN specialization_id INTEGER" )) connection.execute(text( "CREATE INDEX IF NOT EXISTS ix_students_specialization_id ON students(specialization_id)" )) print("Added specialization_id column to students table") # Миграция для student_course if 'student_course' in inspector.get_table_names(): student_course_columns = [col['name'] for col in inspector.get_columns('student_course')] if 'status' not in student_course_columns: connection.execute(text( "ALTER TABLE student_course ADD COLUMN status VARCHAR(20) DEFAULT 'process'" )) print("Added status column to student_course table") try: await conn.run_sync(check_and_add_columns) except Exception as e: print(f"Migration check error: {e}") await conn.run_sync(Base.metadata.create_all) from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.asyncio import AsyncSession from app.services.auth_service import AuthService async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async with async_session() as session: await AuthService(session).create_super_admin_if_not_exists() if __name__ == "__main__": uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)