-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
193 lines (150 loc) · 5.59 KB
/
main.py
File metadata and controls
193 lines (150 loc) · 5.59 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
"""
Chat UNcontrol - Main Entry Point
Sistema di messaggistica temporanea e sicura con crittografia end-to-end
"""
import asyncio
import signal
import sys
import uvicorn
from pathlib import Path
from contextlib import asynccontextmanager
from src.websocket_server import WebSocketServer
from src.database_manager import DatabaseManager
from src.session_manager import SessionManager
from src.message_handler import MessageHandler
from src.config import Config
class ChatUncontrolServer:
"""
Main server class for Chat UNcontrol
Manages lifecycle of all components
"""
def __init__(self):
self.running = True
self.config = Config()
self.db_manager = None
self.websocket_server = None
self.session_manager = None
self.message_handler = None
async def initialize(self):
"""Initialize all server components"""
try:
# Initialize database connection
mongodb_config = self.config.mongodb
connection_string = f"mongodb://{mongodb_config.get('username', 'chat_admin')}:{mongodb_config.get('password', 'secure_password_change_me')}@{mongodb_config.get('host', 'localhost')}:{mongodb_config.get('port', 27017)}"
self.db_manager = DatabaseManager(
connection_string=connection_string,
database_name=mongodb_config.get('database', 'chat_uncontrol')
)
# Connect to database
await self.db_manager.connect()
print("✅ Connected to MongoDB")
# Initialize managers
self.session_manager = SessionManager(self.db_manager)
self.message_handler = MessageHandler(self.db_manager)
# Initialize WebSocket server
self.websocket_server = WebSocketServer(
db_manager=self.db_manager,
session_manager=self.session_manager,
message_handler=self.message_handler
)
print("✅ All components initialized")
return True
except Exception as e:
print(f"❌ Initialization failed: {e}")
return False
async def cleanup(self):
"""Cleanup resources on shutdown"""
print("🔄 Shutting down server...")
if self.websocket_server:
await self.websocket_server.cleanup()
if self.db_manager:
await self.db_manager.disconnect()
print("✅ Server shutdown complete")
def setup_signal_handlers(self):
"""Setup graceful shutdown signal handlers"""
def signal_handler(sig, frame):
print(f"\n🛑 Received signal {sig}, shutting down...")
self.running = False
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Global server instance
chat_server = ChatUncontrolServer()
@asynccontextmanager
async def lifespan(app):
"""FastAPI lifespan events"""
# Startup
success = await chat_server.initialize()
if not success:
print("❌ Failed to start server")
sys.exit(1)
# Register WebSocket endpoint after initialization
app.websocket("/ws")(chat_server.websocket_server.websocket_endpoint)
yield
# Shutdown
await chat_server.cleanup()
# FastAPI app with lifespan
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
app = FastAPI(
title="Chat UNcontrol",
description="Secure ephemeral messaging with end-to-end encryption",
version="1.0.0",
lifespan=lifespan
)
# Mount static files
app.mount("/static", StaticFiles(directory="frontend"), name="static")
# HTTP routes for serving frontend
@app.get("/")
async def serve_index():
"""Serve main page"""
return FileResponse("frontend/index.html")
@app.get("/chat/{session_id}")
async def serve_chat(session_id: str):
"""Serve chat page with session ID"""
# Read chat.html and replace session ID placeholder
chat_path = Path("frontend/chat.html")
if not chat_path.exists():
return {"error": "Chat page not found"}
content = chat_path.read_text(encoding='utf-8')
content = content.replace("{{SESSION_ID}}", session_id)
from fastapi.responses import HTMLResponse
return HTMLResponse(content=content)
@app.get("/chat.js")
async def serve_chat_js():
"""Serve JavaScript file"""
return FileResponse("frontend/chat.js", media_type="application/javascript")
@app.get("/style.css")
async def serve_css():
"""Serve CSS file"""
return FileResponse("frontend/style.css", media_type="text/css")
def main():
"""Main entry point"""
print("🚀 Starting Chat UNcontrol Server...")
print("📡 Server will be available at: http://localhost:8080")
print("🔒 WebSocket endpoint: ws://localhost:8080/ws")
print("📋 Press Ctrl+C to stop\n")
# Setup signal handlers
chat_server.setup_signal_handlers()
# Run with uvicorn
server_config = chat_server.config.server
config = uvicorn.Config(
"main:app",
host=server_config.get('host', "0.0.0.0"),
port=server_config.get('port', 8080),
log_level="debug" if server_config.get('debug', False) else "info",
reload=False, # Disable reload for production
access_log=True
)
server = uvicorn.Server(config)
try:
asyncio.run(server.serve())
except KeyboardInterrupt:
print("\n🛑 Server interrupted by user")
except Exception as e:
print(f"❌ Server error: {e}")
finally:
print("👋 Goodbye!")
if __name__ == "__main__":
main()