File size: 3,117 Bytes
119600e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
OpenAI TTS API Server

This module provides a server that's compatible with OpenAI's TTS API format.
This is the main entry point for the application.
"""

import asyncio
import aiohttp
import logging
import ssl
import time
import sys
from typing import Optional
from aiohttp import TCPConnector, ClientTimeout

from utils.config import load_config, test_connection
from server.api import TTSServer

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

async def create_test_session(verify_ssl: bool = True) -> Optional[aiohttp.ClientSession]:
    """Create a session for testing with optimized settings."""
    try:
        if not verify_ssl:
            connector = TCPConnector(
                ssl=False,
                limit=5,
                ttl_dns_cache=300,
                use_dns_cache=True,
                enable_cleanup_closed=True
            )
        else:
            connector = TCPConnector(
                limit=5,
                ttl_dns_cache=300,
                use_dns_cache=True,
                enable_cleanup_closed=True
            )
            
        timeout = ClientTimeout(
            total=30,
            connect=10,
            sock_read=20
        )
        
        return aiohttp.ClientSession(
            connector=connector,
            timeout=timeout
        )
    except Exception as e:
        logger.error(f"Failed to create test session: {str(e)}")
        return None

async def main():
    """Main function to start the server."""
    try:
        config = load_config()
        
        # Test connection mode
        if config.get('test_connection', False):
            session = await create_test_session(config['verify_ssl'])
            if not session:
                logger.error("Failed to create test session")
                sys.exit(1)
                
            try:
                await test_connection(session)
            except Exception as e:
                logger.error(f"Connection test failed: {str(e)}")
                sys.exit(1)
            finally:
                await session.close()
                
            logger.info("Connection test completed successfully")
            return
        
        # Start the server
        server = TTSServer(
            host=config['host'],
            port=config['port'],
            verify_ssl=config['verify_ssl'],
            max_queue_size=config['max_queue_size']
        )
        
        await server.start()
        
        try:
            # Keep the server running
            while True:
                await asyncio.sleep(1)
        except KeyboardInterrupt:
            logger.info("Received shutdown signal")
            await server.stop()
            logger.info("TTS server stopped gracefully")
        except Exception as e:
            logger.error(f"Server error: {str(e)}")
            await server.stop()
            sys.exit(1)
            
    except Exception as e:
        logger.error(f"Fatal error: {str(e)}")
        sys.exit(1)