refactor(bridge): migrate gravity bridge to pure websocket gateway architecture, deleting legacy local file scanners and dependencies
This commit is contained in:
56
main.py
56
main.py
@@ -10,7 +10,6 @@ import os
|
||||
import sys
|
||||
|
||||
from config import Config
|
||||
from watcher import BrainWatcher
|
||||
from bot import GravityBot
|
||||
|
||||
# Logging setup (UTF-8 forced for Windows cp949 compatibility)
|
||||
@@ -51,45 +50,32 @@ async def main():
|
||||
# Get the running loop
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
# ── Local / Gateway mode ──
|
||||
|
||||
# Create components
|
||||
watcher = None
|
||||
if Config.BOT_MODE != 'gateway':
|
||||
watcher = BrainWatcher(event_queue, loop)
|
||||
bot = GravityBot(event_queue)
|
||||
|
||||
try:
|
||||
# Start watcher (local mode only — gateway receives data via HTTP)
|
||||
if watcher:
|
||||
watcher.start()
|
||||
logger.info(f"Watcher started, {len(watcher.known_sessions)} existing sessions")
|
||||
else:
|
||||
logger.info("Gateway mode — watcher disabled (data via HTTP API)")
|
||||
# Start Gateway HTTP API + WebSocket Hub
|
||||
from gateway import GatewayAPI
|
||||
from hub import WSHub
|
||||
from auth import TokenManager
|
||||
|
||||
# Start Gateway HTTP API + WebSocket Hub (gateway mode)
|
||||
if Config.BOT_MODE == 'gateway':
|
||||
from gateway import GatewayAPI
|
||||
from hub import WSHub
|
||||
from auth import TokenManager
|
||||
# Initialize Hub
|
||||
token_mgr = TokenManager(
|
||||
secret=Config.GRAVITY_HUB_SECRET,
|
||||
registration_code=Config.GRAVITY_REGISTRATION_CODE,
|
||||
)
|
||||
hub = WSHub(token_mgr)
|
||||
|
||||
# Initialize Hub
|
||||
token_mgr = TokenManager(
|
||||
secret=Config.GRAVITY_HUB_SECRET,
|
||||
registration_code=Config.GRAVITY_REGISTRATION_CODE,
|
||||
)
|
||||
hub = WSHub(token_mgr)
|
||||
|
||||
gateway_port = int(os.environ.get('GATEWAY_PORT', '8585'))
|
||||
gateway = GatewayAPI(
|
||||
bot, port=gateway_port,
|
||||
api_key=Config.GATEWAY_API_KEY,
|
||||
hub=hub,
|
||||
)
|
||||
bot.gateway = gateway # Enable _write_command → gateway.push_command
|
||||
bot.hub = hub # Enable Hub-based message routing
|
||||
await gateway.start()
|
||||
logger.info(f"Gateway API + WS Hub running on port {gateway_port}")
|
||||
gateway_port = int(os.environ.get('GATEWAY_PORT', '8585'))
|
||||
gateway = GatewayAPI(
|
||||
bot, port=gateway_port,
|
||||
api_key=Config.GATEWAY_API_KEY,
|
||||
hub=hub,
|
||||
)
|
||||
bot.gateway = gateway # Enable _write_command → gateway.push_command
|
||||
bot.hub = hub # Enable Hub-based message routing
|
||||
await gateway.start()
|
||||
logger.info(f"Gateway API + WS Hub running on port {gateway_port}")
|
||||
|
||||
# Run Discord bot (blocks until bot disconnects)
|
||||
await bot.start(Config.DISCORD_TOKEN)
|
||||
@@ -100,8 +86,6 @@ async def main():
|
||||
logger.error(f"Fatal error: {e}", exc_info=True)
|
||||
finally:
|
||||
# Cleanup
|
||||
if watcher:
|
||||
watcher.stop()
|
||||
if not bot.is_closed():
|
||||
await bot.close()
|
||||
logger.info("Gravity Control shutdown complete")
|
||||
|
||||
Reference in New Issue
Block a user