From 3e15f643d623142b99d4ac9d6b4444fe1a5aa92a Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 7 Apr 2026 13:49:12 +0500 Subject: [PATCH] =?UTF-8?q?fix:=20deployment=20=E2=80=94=20resolve=20depen?= =?UTF-8?q?dency=20conflicts,=20add=20initial=20migration,=20CI/CD=20webho?= =?UTF-8?q?ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix httpx/pydantic/redis version conflicts for litellm/aiogram/celery compatibility - Add PYTHONPATH=/app to Dockerfile for alembic module resolution - Skip Telegram webhook setup when URL is not HTTPS - Fix FileUpload prop type mismatch in RecordingsPage - Add alembic script.py.mako template and initial migration - Configure docker-compose: port 8081, built frontend, no exposed internal ports - Add deploy.sh and webhook-server.js for Gitea CI/CD auto-deploy Co-Authored-By: Claude Opus 4.6 (1M context) --- backend/Dockerfile | 1 + backend/alembic/script.py.mako | 26 ++++ .../alembic/versions/a7d968fceaaf_initial.py | 111 ++++++++++++++++++ backend/app/main.py | 4 +- backend/requirements.txt | 8 +- deploy.sh | 18 +++ docker-compose.yml | 16 ++- frontend/src/pages/RecordingsPage.tsx | 2 +- webhook-server.js | 46 ++++++++ 9 files changed, 216 insertions(+), 16 deletions(-) create mode 100644 backend/alembic/script.py.mako create mode 100644 backend/alembic/versions/a7d968fceaaf_initial.py create mode 100644 deploy.sh create mode 100644 webhook-server.js diff --git a/backend/Dockerfile b/backend/Dockerfile index e35489e..621a8ea 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -2,6 +2,7 @@ FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04 ENV DEBIAN_FRONTEND=noninteractive ENV PYTHONUNBUFFERED=1 +ENV PYTHONPATH=/app # System dependencies RUN apt-get update && apt-get install -y \ diff --git a/backend/alembic/script.py.mako b/backend/alembic/script.py.mako new file mode 100644 index 0000000..fbc4b07 --- /dev/null +++ b/backend/alembic/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/backend/alembic/versions/a7d968fceaaf_initial.py b/backend/alembic/versions/a7d968fceaaf_initial.py new file mode 100644 index 0000000..dbcf670 --- /dev/null +++ b/backend/alembic/versions/a7d968fceaaf_initial.py @@ -0,0 +1,111 @@ +"""initial + +Revision ID: a7d968fceaaf +Revises: +Create Date: 2026-04-06 17:59:05.406839 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision: str = 'a7d968fceaaf' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('prompt_templates', + sa.Column('name', sa.String(length=255), nullable=False), + sa.Column('description', sa.String(length=1000), nullable=False), + sa.Column('type', sa.Enum('client_survey', 'client_intro', 'internal', 'custom', name='templatetype'), nullable=False), + sa.Column('system_prompt', sa.Text(), nullable=False), + sa.Column('user_prompt', sa.Text(), nullable=False), + sa.Column('output_schema', postgresql.JSONB(astext_type=sa.Text()), nullable=False), + sa.Column('is_default', sa.Boolean(), nullable=False), + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('users', + sa.Column('username', sa.String(length=150), nullable=False), + sa.Column('email', sa.String(length=255), nullable=False), + sa.Column('password_hash', sa.String(length=255), nullable=False), + sa.Column('role', sa.Enum('admin', 'user', name='userrole'), nullable=False), + sa.Column('is_active', sa.Boolean(), nullable=False), + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True) + op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) + op.create_table('recordings', + sa.Column('user_id', sa.Uuid(), nullable=False), + sa.Column('title', sa.String(length=500), nullable=False), + sa.Column('file_path', sa.String(length=1000), nullable=False), + sa.Column('file_size', sa.BigInteger(), nullable=False), + sa.Column('duration', sa.Float(), nullable=True), + sa.Column('format', sa.String(length=20), nullable=False), + sa.Column('source', sa.Enum('web', 'telegram', 'lensa', name='recordingsource'), nullable=False), + sa.Column('status', sa.Enum('uploaded', 'processing', 'done', 'error', name='recordingstatus'), nullable=False), + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('transcriptions', + sa.Column('recording_id', sa.Uuid(), nullable=False), + sa.Column('text', sa.Text(), nullable=False), + sa.Column('segments', postgresql.JSONB(astext_type=sa.Text()), nullable=False), + sa.Column('language', sa.String(length=10), nullable=True), + sa.Column('whisper_model', sa.String(length=50), nullable=False), + sa.Column('processing_time', sa.Float(), nullable=True), + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.ForeignKeyConstraint(['recording_id'], ['recordings.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('recording_id') + ) + op.create_table('protocols', + sa.Column('transcription_id', sa.Uuid(), nullable=False), + sa.Column('template_id', sa.Uuid(), nullable=False), + sa.Column('content', postgresql.JSONB(astext_type=sa.Text()), nullable=False), + sa.Column('raw_text', sa.Text(), nullable=False), + sa.Column('llm_model', sa.String(length=100), nullable=False), + sa.Column('status', sa.Enum('generating', 'done', 'error', name='protocolstatus'), nullable=False), + sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.ForeignKeyConstraint(['template_id'], ['prompt_templates.id'], ), + sa.ForeignKeyConstraint(['transcription_id'], ['transcriptions.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('export_files', + sa.Column('protocol_id', sa.Uuid(), nullable=False), + sa.Column('format', sa.Enum('docx', 'pdf', 'md', name='exportformat'), nullable=False), + sa.Column('file_path', sa.String(length=1000), nullable=False), + sa.Column('file_size', sa.BigInteger(), nullable=False), + sa.Column('id', sa.Uuid(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.ForeignKeyConstraint(['protocol_id'], ['protocols.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('export_files') + op.drop_table('protocols') + op.drop_table('transcriptions') + op.drop_table('recordings') + op.drop_index(op.f('ix_users_username'), table_name='users') + op.drop_index(op.f('ix_users_email'), table_name='users') + op.drop_table('users') + op.drop_table('prompt_templates') + # ### end Alembic commands ### diff --git a/backend/app/main.py b/backend/app/main.py index 0576e3b..94300b8 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -30,10 +30,10 @@ async def seed_default_templates(): @asynccontextmanager async def lifespan(app: FastAPI): await seed_default_templates() - if settings.telegram_bot_token: + if settings.telegram_bot_token and settings.telegram_webhook_url.startswith("https"): await telegram_service.setup() yield - if settings.telegram_bot_token: + if settings.telegram_bot_token and settings.telegram_webhook_url.startswith("https"): await telegram_service.shutdown() diff --git a/backend/requirements.txt b/backend/requirements.txt index c2185dd..1455bbf 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -10,7 +10,7 @@ psycopg2-binary==2.9.10 alembic==1.14.1 # Validation -pydantic==2.10.4 +pydantic>=2.7.0,<2.10 pydantic-settings==2.7.1 # Auth @@ -19,7 +19,7 @@ python-jose[cryptography]==3.3.0 # Task queue celery[redis]==5.4.0 -redis==5.2.1 +redis==4.6.0 # AI/ML faster-whisper==1.1.0 @@ -36,10 +36,10 @@ markdown==3.7 # Utils ffmpeg-python==0.2.0 -httpx==0.28.1 +httpx>=0.23.0,<0.28.0 aiofiles==24.1.0 # Testing pytest==8.3.4 pytest-asyncio==0.25.0 -httpx==0.28.1 +httpx>=0.23.0,<0.28.0 diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..e7683fa --- /dev/null +++ b/deploy.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# Auto-deploy script triggered by Gitea webhook +set -e + +DEPLOY_DIR="D:/meeting-protocol-service" +LOG_FILE="$DEPLOY_DIR/deploy.log" + +echo "$(date) - Deploy started" >> "$LOG_FILE" + +cd "$DEPLOY_DIR" + +# Pull latest changes +git pull origin main >> "$LOG_FILE" 2>&1 + +# Rebuild and restart services +docker compose up -d --build >> "$LOG_FILE" 2>&1 + +echo "$(date) - Deploy completed" >> "$LOG_FILE" diff --git a/docker-compose.yml b/docker-compose.yml index 7307cca..db649e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,8 +2,8 @@ services: app: build: ./backend env_file: .env - ports: - - "8000:8000" + expose: + - "8000" volumes: - uploads:/app/uploads - exports:/app/exports @@ -12,6 +12,7 @@ services: condition: service_healthy redis: condition: service_healthy + working_dir: /app command: > sh -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000" @@ -45,8 +46,6 @@ services: redis: image: redis:7-alpine - ports: - - "6379:6379" healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 5s @@ -57,7 +56,7 @@ services: image: postgres:16-alpine env_file: .env ports: - - "5432:5432" + - "5433:5432" volumes: - pgdata:/var/lib/postgresql/data healthcheck: @@ -66,13 +65,12 @@ services: timeout: 3s retries: 5 - nginx: - image: nginx:alpine + frontend: + build: ./frontend ports: - - "80:80" + - "8081:80" volumes: - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf - - ./frontend/dist:/usr/share/nginx/html depends_on: - app diff --git a/frontend/src/pages/RecordingsPage.tsx b/frontend/src/pages/RecordingsPage.tsx index 27fdaa5..d267881 100644 --- a/frontend/src/pages/RecordingsPage.tsx +++ b/frontend/src/pages/RecordingsPage.tsx @@ -26,7 +26,7 @@ export default function RecordingsPage() {

Записи

- + upload({ file, title })} isUploading={isUploading} />
{isLoading ? (

Загрузка...

diff --git a/webhook-server.js b/webhook-server.js new file mode 100644 index 0000000..ffb4a9b --- /dev/null +++ b/webhook-server.js @@ -0,0 +1,46 @@ +const http = require('http'); +const { execFile } = require('child_process'); +const crypto = require('crypto'); + +const PORT = 9000; +const SECRET = 'meeting-proto-deploy-secret'; + +const server = http.createServer((req, res) => { + if (req.method !== 'POST' || req.url !== '/deploy') { + res.writeHead(404); + res.end('Not found'); + return; + } + + let body = ''; + req.on('data', chunk => { body += chunk; }); + req.on('end', () => { + // Verify Gitea signature + const sig = req.headers['x-gitea-signature']; + if (sig) { + const hmac = crypto.createHmac('sha256', SECRET).update(body).digest('hex'); + if (sig !== hmac) { + console.log(`${new Date().toISOString()} - Invalid signature, rejecting`); + res.writeHead(403); + res.end('Invalid signature'); + return; + } + } + + console.log(`${new Date().toISOString()} - Deploy triggered`); + res.writeHead(200); + res.end('Deploy started'); + + execFile('bash', ['D:/meeting-protocol-service/deploy.sh'], (err, stdout, stderr) => { + if (err) { + console.error(`Deploy error: ${err.message}`); + return; + } + console.log(`Deploy output: ${stdout}`); + }); + }); +}); + +server.listen(PORT, () => { + console.log(`Webhook server listening on port ${PORT}`); +});