Skip to content

FastAPI

lisambet edited this page Feb 2, 2026 · 3 revisions

⚡ FastAPI

FastAPI is a modern, high-performance web framework for building APIs with Python 3.8+ based on standard Python type hints.

Property Value
Used In AI Service
Language Python
Port 3005
Documentation fastapi.tiangolo.com

⚠️ Note: Do not confuse FastAPI (Python) with Fastify (Node.js).

  • FastAPI is used only for the AI microservice.
  • Fastify is used for the Gateway, Auth, and Game services.

💡 Why FastAPI?

We chose FastAPI for the AI microservice because it bridges the gap between web servers and Data Science tools perfectly.

  1. Native Python Support: Our AI model uses PyTorch and Stable Baselines3, which are Python-native. FastAPI allows us to serve these models directly without context switching.
  2. High Performance: It is one of the fastest Python frameworks available, running on Uvicorn (an ASGI server). This is critical for the AI, which must reply with a move in <50ms.
  3. Automatic Documentation: It automatically generates interactive Swagger UI documentation, allowing us to test the AI moves manually in the browser.
  4. Data Validation: It uses Pydantic to validate game coordinates automatically. If the Game Service sends a string instead of a number, FastAPI rejects it instantly.

🏗️ Implementation Details

Request Validation (Pydantic)

Instead of manually checking if ball_x is a number, we define a "Model" (Schema). FastAPI guarantees that the function get_move is only called if the data is valid.

from pydantic import BaseModel

# Define the expected data structure
class GameState(BaseModel):
    ball_x: float
    ball_y: float
    paddle_y: float
    opponent_y: float

# The API Endpoint
@app.post("/join-game")
async def get_move(state: GameState):
    # 'state' is already validated and converted to the correct types
    action = model.predict([state.ball_x, state.ball_y, ...])
    return {"action": int(action)}

🏗️ Architecture

🌐 Web Technologies

Backend

Frontend

🔧 Core Technologies

🔐 Security

⛓️ Blockchain

🛠️ Dev Tools & Quality


📝 Page model

Clone this wiki locally