mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-17 09:50:31 +08:00
Resolved conflicts: - openai_source.py: keep dev version with abort_signal filtering - customizer.ts: keep dev version with viewMode functionality - useSessions.ts: keep dev version with pendingSessionId handling - platformUtils.js: keep dev version with correct tutorial links - AddNewPlatform.vue: keep dev version with correct docs link - FullLayout.vue: keep dev version with viewMode-based logic - VerticalHeader.vue: keep dev version with viewMode-based logic
74 lines
2.9 KiB
Python
74 lines
2.9 KiB
Python
"""
|
|
AstrBot Gateway - HTTP/WebSocket API server.
|
|
|
|
Built on FastAPI, provides:
|
|
- HTTP REST API (stats, inspector, config)
|
|
- WebSocket for real-time events
|
|
- Static file serving (dashboard)
|
|
- Authentication (JWT/API key)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
class BaseAstrbotGateway(ABC):
|
|
"""
|
|
Gateway: HTTP/WebSocket server built on FastAPI.
|
|
|
|
┌─────────────────────────────────────────────────────────┐
|
|
│ FastAPI App │
|
|
├─────────────────────────────────────────────────────────┤
|
|
│ REST Endpoints WebSocket │
|
|
│ ├─ GET /api/stats ├─ /ws (connection manager)│
|
|
│ ├─ GET /api/inspector/* │ │
|
|
│ ├─ GET /api/memory/* │ │
|
|
│ └─ ... │ │
|
|
│ │
|
|
│ Middleware: CORS, Auth, Logging │
|
|
└─────────────────────────────────────────────────────────┘
|
|
│
|
|
▼
|
|
┌─────────────────────────┐
|
|
│ Orchestrator │
|
|
│ (owns protocol clients)│
|
|
└─────────────────────────┘
|
|
|
|
Routes (typical):
|
|
GET / → Dashboard static files
|
|
GET /api/stats → System statistics
|
|
GET /api/inspector/stars → List registered stars
|
|
WS /ws → WebSocket for real-time events
|
|
|
|
serve() Lifecycle:
|
|
1. Create FastAPI app
|
|
2. Register routes
|
|
3. Start WebSocket manager
|
|
4. Bind to host:port
|
|
5. Run ASGI server (uvicorn/hypercorn)
|
|
6. Block until shutdown
|
|
7. Close all connections
|
|
|
|
Subclass must implement:
|
|
- serve(): start server, block until shutdown
|
|
"""
|
|
|
|
@abstractmethod
|
|
async def serve(self) -> None:
|
|
"""
|
|
Start gateway server - blocks until shutdown.
|
|
|
|
Should:
|
|
1. Create FastAPI app with routes
|
|
2. Configure CORS, auth middleware
|
|
3. Start WebSocket connection manager
|
|
4. Bind to ASTRBOT_PORT (default 6185)
|
|
5. Run ASGI server
|
|
6. Handle graceful shutdown on SIGTERM/SIGINT
|
|
|
|
Raises:
|
|
OSError: address already in use
|
|
"""
|
|
...
|