-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
145 lines (123 loc) · 3.63 KB
/
Copy pathmain.py
File metadata and controls
145 lines (123 loc) · 3.63 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
"""
FastAPI Gomoku Game Application
Main application entry point
"""
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
import socketio
import eventlet
import os
from pathlib import Path
from typing import Dict, Any
from config import settings
from game.views import (
set_socketio_server,
handle_connect,
handle_disconnect,
handle_init_game,
handle_join_current_game,
handle_move,
handle_rematch,
handle_disconnect_request,
get_game_context,
get_active_games,
get_health_status
)
# Create FastAPI app
app = FastAPI(
title=settings.app_name,
version=settings.version,
debug=settings.debug
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Create Socket.IO server
sio = socketio.AsyncServer(
async_mode='asgi',
cors_allowed_origins='*'
)
# Create Socket.IO app
socket_app = socketio.ASGIApp(sio, app)
# Setup static files and templates
static_path = Path(__file__).parent / "static"
templates_path = Path(__file__).parent / "templates"
if static_path.exists():
app.mount("/static", StaticFiles(directory=str(static_path)), name="static")
templates = Jinja2Templates(directory=str(templates_path))
# Set up Socket.IO server in views module
set_socketio_server(sio)
# Socket.IO event handlers - using handlers from views.py
@sio.event
async def connect(sid, environ):
print(f"Socket.IO connect event: {sid}")
await handle_connect(sid, environ)
@sio.event
async def disconnect(sid):
print(f"Socket.IO disconnect event: {sid}")
await handle_disconnect(sid)
@sio.event
async def init_game(sid, data):
print(f"Socket.IO init_game event: {sid}, {data}")
await handle_init_game(sid, data)
@sio.event
async def join_current_game(sid, data):
print(f"Socket.IO join_current_game event: {sid}, {data}")
await handle_join_current_game(sid, data)
@sio.event
async def move(sid, data):
print(f"Socket.IO move event: {sid}, {data}")
await handle_move(sid, data)
@sio.event
async def rematch(sid, data):
print(f"Socket.IO rematch event: {sid}, {data}")
await handle_rematch(sid, data)
@sio.event
async def disconnect_request(sid):
print(f"Socket.IO disconnect_request event: {sid}")
await handle_disconnect_request(sid)
# FastAPI routes
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
"""Main page route"""
context = get_game_context()
return templates.TemplateResponse("board_game.html", {
"request": request,
**context
})
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return get_health_status()
@app.get("/api/games")
async def get_games():
"""API endpoint to get active games"""
return get_active_games()
# WebSocket endpoint for Socket.IO
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""WebSocket endpoint for Socket.IO"""
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
# Handle WebSocket messages if needed
await websocket.send_text(f"Message received: {data}")
except WebSocketDisconnect:
print("WebSocket disconnected")
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:socket_app",
host=settings.host,
port=settings.port,
reload=settings.reload
)