From f1f6760beffb33250a2e95924b1b320612cf3050 Mon Sep 17 00:00:00 2001 From: Marius Mutu Date: Sun, 4 Jan 2026 03:56:27 +0200 Subject: [PATCH] refactor: Remove deprecated INTERNAL_API_PORT (ultrathin monolith cleanup) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Architecture cleanup after migration to ultrathin monolith: - Remove INTERNAL_API_PORT from .env files (was port 8002) - Clean up bot_main.py: remove uvicorn, Thread, run_internal_api() - Update validate.md to check /api/telegram/health instead of port 8002 - Add deprecation notices to old Windows deployment docs - Update docs/telegram/README.md with architecture note The Telegram internal API is now served at /api/telegram/internal/* on the main backend port (8000/8001) instead of separate port 8002. Also includes: menu updates, ServerLogsView improvements, script fixes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .claude/commands/validate.md | 6 +- backend/.env.dev.example | 2 +- backend/.env.prod.example | 2 +- backend/.env.test.example | 2 +- backend/modules/telegram/bot_main.py | 34 ++--------- data/ocr_queue/.gitkeep | 0 data/ocr_queue/files/.gitkeep | 0 .../windows/docs/TELEGRAM_BOT_DEPLOYMENT.md | 13 ++++- .../docs/TELEGRAM_BOT_TROUBLESHOOTING.md | 6 ++ docs/telegram/README.md | 11 +++- docs/telegram/testing/DOCKER_TESTING_GUIDE.md | 6 ++ src/config/menu.js | 3 +- src/modules/reports/views/ServerLogsView.vue | 58 ++++++++----------- src/modules/reports/views/TelegramView.vue | 2 +- start-backend.sh | 16 +++-- start-prod.sh | 20 +++++-- 16 files changed, 96 insertions(+), 85 deletions(-) delete mode 100644 data/ocr_queue/.gitkeep delete mode 100644 data/ocr_queue/files/.gitkeep diff --git a/.claude/commands/validate.md b/.claude/commands/validate.md index 4c7bfc8..32be45b 100644 --- a/.claude/commands/validate.md +++ b/.claude/commands/validate.md @@ -401,9 +401,9 @@ else exit 1 fi -# Telegram Bot health check -if check_port_available 8002; then - echo "✅ Telegram bot internal API is running on port 8002" +# Telegram Bot health check (now part of main backend on port 8000) +if curl -s http://localhost:8000/api/telegram/health > /dev/null 2>&1; then + echo "✅ Telegram bot is running (via main backend)" else echo "⚠️ Telegram bot is not running (optional for validation)" fi diff --git a/backend/.env.dev.example b/backend/.env.dev.example index b6c5700..ab3dbfe 100644 --- a/backend/.env.dev.example +++ b/backend/.env.dev.example @@ -143,7 +143,7 @@ TELEGRAM_BOT_TOKEN=your_bot_token_from_botfather BACKEND_URL=http://localhost:8000 # Internal API port (bot's internal API for backend callbacks) -INTERNAL_API_PORT=8002 +# INTERNAL_API_PORT=8002 # DEPRECATED: Now served via main app at /api/telegram/internal/* # Enable internal API documentation (development only) ENABLE_DOCS=false diff --git a/backend/.env.prod.example b/backend/.env.prod.example index 9a8c1ef..c294bbe 100644 --- a/backend/.env.prod.example +++ b/backend/.env.prod.example @@ -137,7 +137,7 @@ TELEGRAM_BOT_TOKEN=your_bot_token_from_botfather BACKEND_URL=http://localhost:8000 # Internal API port (bot's internal API for backend callbacks) -INTERNAL_API_PORT=8002 +# INTERNAL_API_PORT=8002 # DEPRECATED: Now served via main app at /api/telegram/internal/* # Enable internal API documentation (DISABLE in production!) ENABLE_DOCS=false diff --git a/backend/.env.test.example b/backend/.env.test.example index dab39e5..e375ee0 100644 --- a/backend/.env.test.example +++ b/backend/.env.test.example @@ -144,7 +144,7 @@ TELEGRAM_BOT_TOKEN=8483383555:AAGNY1z6WiBkvVfy1ZV_gM_JnAqW4q4MlEY BACKEND_URL=http://localhost:8000 # Internal API port (bot's internal API for backend callbacks) -INTERNAL_API_PORT=8002 +# INTERNAL_API_PORT=8002 # DEPRECATED: Now served via main app at /api/telegram/internal/* # Enable internal API documentation (development only) ENABLE_DOCS=false diff --git a/backend/modules/telegram/bot_main.py b/backend/modules/telegram/bot_main.py index 8472bac..46b6161 100644 --- a/backend/modules/telegram/bot_main.py +++ b/backend/modules/telegram/bot_main.py @@ -10,8 +10,7 @@ import logging import os from pathlib import Path from dotenv import load_dotenv -import uvicorn -from threading import Thread +# Note: uvicorn and threading removed - internal API now served via main.py # ============================================================================ # LOAD ENVIRONMENT VARIABLES FIRST - BEFORE ANY APP IMPORTS @@ -69,8 +68,7 @@ from backend.modules.telegram.bot.handlers import ( # Import email authentication handler from backend.modules.telegram.bot.email_handlers import email_login_handler -# Import internal API -from backend.modules.telegram.internal_api import internal_api +# Note: internal_api import removed - now served via main.py at /api/telegram/internal/* # Configure logging logging.basicConfig( @@ -82,7 +80,7 @@ logger = logging.getLogger(__name__) # Environment variables (already loaded above) TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') BACKEND_URL = os.getenv('BACKEND_URL', 'http://localhost:8000') -INTERNAL_API_PORT = int(os.getenv('INTERNAL_API_PORT', '8002')) +# Note: INTERNAL_API_PORT removed - internal API now served via main.py # ============================================================================ @@ -153,29 +151,10 @@ def create_telegram_application() -> Application: return application -# ============================================================================ -# INTERNAL API SERVER -# ============================================================================ - -def run_internal_api(): - """ - Run the internal FastAPI server in a separate thread. - - This API handles communication from the backend (saving auth codes). - """ - logger.info(f"Starting internal API on port {INTERNAL_API_PORT}...") - - uvicorn.run( - internal_api, - host="0.0.0.0", - port=INTERNAL_API_PORT, - log_level="info" - ) - - # ============================================================================ # STARTUP/SHUTDOWN # ============================================================================ +# Note: Internal API server removed - now served via main.py at /api/telegram/internal/* async def startup(): """ @@ -247,10 +226,7 @@ async def main(): # Create Telegram application telegram_app = create_telegram_application() - # Start internal API in a separate thread - api_thread = Thread(target=run_internal_api, daemon=True) - api_thread.start() - logger.info(f"✅ Internal API started on port {INTERNAL_API_PORT}") + # Note: Internal API server removed - now served via main.py # Start scheduled cleanup task in background cleanup_task = asyncio.create_task(scheduled_cleanup()) diff --git a/data/ocr_queue/.gitkeep b/data/ocr_queue/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/data/ocr_queue/files/.gitkeep b/data/ocr_queue/files/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/deployment/windows/docs/TELEGRAM_BOT_DEPLOYMENT.md b/deployment/windows/docs/TELEGRAM_BOT_DEPLOYMENT.md index d70e714..fe44cfa 100644 --- a/deployment/windows/docs/TELEGRAM_BOT_DEPLOYMENT.md +++ b/deployment/windows/docs/TELEGRAM_BOT_DEPLOYMENT.md @@ -1,9 +1,20 @@ # ROA2WEB Telegram Bot - Windows Server Deployment Guide +> ⚠️ **DEPRECATED ARCHITECTURE** +> +> This documentation refers to the OLD microservices architecture where the Telegram bot +> ran as a separate service on port 8002. The current architecture is an **ultrathin monolith** +> where everything runs on a single port (8000/8001) via `backend/main.py`. +> +> **Current Architecture**: Telegram bot runs as a background task within the unified backend. +> Internal API endpoints are now at `/api/telegram/internal/*` on the main port. +> +> See `CLAUDE.md` and `QUICK-START.md` for the current architecture. + **Target Server**: Windows Server (10.0.20.36) **Deployment Method**: Windows Service (NSSM) - No Docker **Service Name**: ROA2WEB-TelegramBot -**Internal API Port**: 8002 +**Internal API Port**: ~~8002~~ (DEPRECATED - now via main backend) **Created**: 2025-10-22 **Last Updated**: 2025-10-22 diff --git a/deployment/windows/docs/TELEGRAM_BOT_TROUBLESHOOTING.md b/deployment/windows/docs/TELEGRAM_BOT_TROUBLESHOOTING.md index 6f06f4c..20b642f 100644 --- a/deployment/windows/docs/TELEGRAM_BOT_TROUBLESHOOTING.md +++ b/deployment/windows/docs/TELEGRAM_BOT_TROUBLESHOOTING.md @@ -1,5 +1,11 @@ # ROA2WEB Telegram Bot - Windows Deployment Troubleshooting Guide +> ⚠️ **DEPRECATED ARCHITECTURE** +> +> This documentation refers to the OLD microservices architecture (port 8002). +> The current architecture is an **ultrathin monolith** - everything on port 8000/8001. +> Telegram internal API is now at `/api/telegram/internal/*` on the main backend. + This guide helps diagnose and fix common issues with Telegram bot integration on Windows Server deployments. ## Problem: "Link invalid sau expirat" (Invalid or expired link) diff --git a/docs/telegram/README.md b/docs/telegram/README.md index 2792593..4dba540 100644 --- a/docs/telegram/README.md +++ b/docs/telegram/README.md @@ -1,5 +1,12 @@ # ROA2WEB Telegram Bot +> ⚠️ **ARCHITECTURE NOTE** +> +> This documentation partially references the old standalone bot architecture. +> The current architecture is an **ultrathin monolith** where the Telegram bot runs +> as a background task within the main backend (`backend/main.py`) on port 8000/8001. +> The `INTERNAL_API_PORT` variable is no longer used - internal API is at `/api/telegram/internal/*`. + > **Telegram Frontend for ROA2WEB ERP System** with Direct Command Interface ## Overview @@ -117,8 +124,8 @@ BACKEND_URL=http://localhost:8001 # Database SQLITE_DB_PATH=./data/telegram_bot.db -# Internal API (for backend communication) -INTERNAL_API_PORT=8002 +# Internal API - DEPRECATED (now served via main backend at /api/telegram/internal/*) +# INTERNAL_API_PORT=8002 # Optional LOG_LEVEL=INFO diff --git a/docs/telegram/testing/DOCKER_TESTING_GUIDE.md b/docs/telegram/testing/DOCKER_TESTING_GUIDE.md index 06259d8..d634157 100644 --- a/docs/telegram/testing/DOCKER_TESTING_GUIDE.md +++ b/docs/telegram/testing/DOCKER_TESTING_GUIDE.md @@ -1,5 +1,11 @@ # 🐳 Docker Testing Guide - ROA2WEB Telegram Bot +> ⚠️ **DEPRECATED ARCHITECTURE** +> +> This guide references the OLD microservices architecture (separate Telegram container on port 8002). +> The current architecture is an **ultrathin monolith** where the Telegram bot runs as a background +> task within the main backend (port 8000/8001). No separate Docker container needed for Telegram. + This guide provides instructions for testing the Telegram bot Docker deployment. ## 📋 Prerequisites diff --git a/src/config/menu.js b/src/config/menu.js index a6e01ff..c93a0ac 100644 --- a/src/config/menu.js +++ b/src/config/menu.js @@ -20,7 +20,8 @@ export const menuSections = [ items: [ { to: '/reports/telegram', icon: 'pi pi-telegram', label: 'Telegram Bot' }, { to: '/reports/cache-stats', icon: 'pi pi-chart-bar', label: 'Statistici Cache' }, - { to: '/data-entry/ocr-metrics', icon: 'pi pi-eye', label: 'Statistici OCR' } + { to: '/data-entry/ocr-metrics', icon: 'pi pi-eye', label: 'Statistici OCR' }, + { to: '/reports/server-logs', icon: 'pi pi-server', label: 'Server Logs' } ] } ] diff --git a/src/modules/reports/views/ServerLogsView.vue b/src/modules/reports/views/ServerLogsView.vue index 04c899a..afa01ea 100644 --- a/src/modules/reports/views/ServerLogsView.vue +++ b/src/modules/reports/views/ServerLogsView.vue @@ -70,7 +70,11 @@

No log entries found

-
{{ line }}
+
{{ line }}
@@ -173,18 +177,28 @@ const loadLogs = async () => { } } -const getLineClass = (line) => { +const getLineStyle = (line) => { const lineLower = line.toLowerCase() + const baseStyle = { + display: 'block', + padding: '3px 8px', + borderRadius: '2px', + fontFamily: "'Consolas', 'Monaco', 'Courier New', monospace", + fontSize: '0.8125rem', + lineHeight: '1.6' + } + if (lineLower.includes('error') || lineLower.includes('exception') || lineLower.includes('failed')) { - return 'log-error' + return { ...baseStyle, color: '#b91c1c', backgroundColor: 'rgba(185, 28, 28, 0.1)', fontWeight: '600' } } if (lineLower.includes('warning') || lineLower.includes('warn')) { - return 'log-warning' + return { ...baseStyle, color: '#b45309', backgroundColor: 'rgba(180, 83, 9, 0.1)', fontWeight: '500' } } if (lineLower.includes('success') || lineLower.includes('completed')) { - return 'log-success' + return { ...baseStyle, color: '#047857', backgroundColor: 'rgba(4, 120, 87, 0.1)' } } - return 'log-info' + // Default: pure black text on light background + return { ...baseStyle, color: '#000000', backgroundColor: 'rgba(0, 0, 0, 0.02)' } } const toggleAutoRefresh = () => { @@ -329,43 +343,17 @@ onUnmounted(() => { .logs-container { max-height: 600px; overflow-y: auto; - background: var(--color-bg-secondary, #1e1e1e); + background: #ffffff; + border: 1px solid #e2e8f0; border-radius: var(--radius-md); padding: var(--space-sm); } .logs-content { margin: 0; - font-family: 'Consolas', 'Monaco', 'Courier New', monospace; - font-size: 0.8125rem; - line-height: 1.5; white-space: pre-wrap; word-break: break-all; -} - -.logs-content code { - display: block; - padding: 2px var(--space-xs); - border-radius: 2px; -} - -.log-error { - color: #f87171; - background: rgba(248, 113, 113, 0.1); -} - -.log-warning { - color: #fbbf24; - background: rgba(251, 191, 36, 0.1); -} - -.log-success { - color: #34d399; - background: rgba(52, 211, 153, 0.1); -} - -.log-info { - color: #e5e7eb; + background: transparent; } /* Responsive */ diff --git a/src/modules/reports/views/TelegramView.vue b/src/modules/reports/views/TelegramView.vue index 4a685bf..330178a 100644 --- a/src/modules/reports/views/TelegramView.vue +++ b/src/modules/reports/views/TelegramView.vue @@ -83,7 +83,7 @@ import axios from "axios"; // Telegram API uses /api/telegram (separate from reports) const telegramApi = axios.create({ - baseURL: import.meta.env.BASE_URL + 'api/telegram', + baseURL: '/api/telegram', headers: { 'Content-Type': 'application/json' } }); diff --git a/start-backend.sh b/start-backend.sh index 9a82e55..c1a3fc1 100755 --- a/start-backend.sh +++ b/start-backend.sh @@ -12,7 +12,10 @@ source "$SCRIPT_DIR/scripts/service-helpers.sh" # Service configuration SERVICE_NAME="Unified Backend" PORT=8000 -LOG_FILE="/tmp/unified-backend.log" +LOGS_DIR="$ROOT_DIR/logs" +LOG_FILE_STDOUT="$LOGS_DIR/backend-stdout.log" +LOG_FILE_STDERR="$LOGS_DIR/backend-stderr.log" +LOG_FILE="$LOG_FILE_STDERR" # Default for status/logs commands BACKEND_DIR="$ROOT_DIR/backend" VENV_DIR="$BACKEND_DIR/venv" ENV_FILE="$BACKEND_DIR/.env" @@ -59,11 +62,16 @@ start_backend() { print_info "Starting unified backend on port $PORT..." cd "$BACKEND_DIR" - # Clear old log file - > "$LOG_FILE" + # Create logs directory if it doesn't exist + mkdir -p "$LOGS_DIR" + + # Clear old log files + > "$LOG_FILE_STDOUT" + > "$LOG_FILE_STDERR" # Activate virtual environment and start uvicorn - nohup bash -c "source venv/bin/activate && uvicorn main:app --host 0.0.0.0 --port $PORT --reload" > "$LOG_FILE" 2>&1 & + # stdout → backend-stdout.log, stderr → backend-stderr.log + nohup bash -c "source venv/bin/activate && uvicorn main:app --host 0.0.0.0 --port $PORT --reload" > "$LOG_FILE_STDOUT" 2> "$LOG_FILE_STDERR" & BACKEND_PID=$! cd "$ROOT_DIR" diff --git a/start-prod.sh b/start-prod.sh index fc0a2e4..2ae00a3 100755 --- a/start-prod.sh +++ b/start-prod.sh @@ -7,6 +7,9 @@ set -e # Exit on any error +# Get the directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' @@ -164,13 +167,18 @@ else source .env set +a - # Clear old log files - > /tmp/unified_backend_prod.log + # Create logs directory if not exists + mkdir -p "$SCRIPT_DIR/logs" + + # Clear old log files (use logs/ for ServerLogs UI, keep /tmp for quick access) + > "$SCRIPT_DIR/logs/backend-stdout.log" + > "$SCRIPT_DIR/logs/backend-stderr.log" > /tmp/unified_frontend_prod.log # Start backend with auto-restart on crash (OOM protection) + # Logs go to logs/backend-stderr.log for ServerLogs UI print_message "Starting unified backend with auto-restart (includes Reports, Data Entry, and Telegram bot)..." - nohup ./run-with-restart.sh 8000 /tmp/unified_backend_prod.log > /dev/null 2>&1 & + nohup ./run-with-restart.sh 8000 "$SCRIPT_DIR/logs/backend-stderr.log" > /dev/null 2>&1 & cd - > /dev/null @@ -192,8 +200,8 @@ else done if ! check_port 8000; then - print_error "Unified Backend failed to start - check /tmp/unified_backend_prod.log" - tail -n 30 /tmp/unified_backend_prod.log + print_error "Unified Backend failed to start - check logs/backend-stderr.log" + tail -n 30 "$SCRIPT_DIR/logs/backend-stderr.log" cleanup fi fi @@ -253,7 +261,7 @@ echo " • Unified API Docs: http://localhost:8000/docs" echo " • Health Check: http://localhost:8000/health" echo echo -e "${BLUE}Log Files:${NC}" -echo " • Unified Backend: /tmp/unified_backend_prod.log" +echo " • Unified Backend: logs/backend-stderr.log" echo " • Unified Frontend: /tmp/unified_frontend_prod.log" echo echo -e "${YELLOW}Press Ctrl+C to stop all services${NC}"