MEDIUM: Split api.py Into Route Modules #24

Open
opened 2026-02-16 22:44:03 +02:00 by Koko210 · 0 comments
Owner

bot/api.py is too large and handles multiple concerns, making it difficult to maintain and test.

Where It Occurs

  • bot/api.py - Large file with multiple route groups
  • All endpoints in one file: configuration, mood, server, logs

Why This Is a Problem

  1. Large File: Difficult to navigate and understand
  2. Multiple Concerns: Configuration, mood, server, logs mixed together
  3. Testing: Hard to test routes in isolation
  4. Merge Conflicts: Multiple developers modifying same file

What Can Go Wrong

Scenario 1: Merge Conflict

  1. Developer A adds configuration endpoint
  2. Developer B adds mood endpoint
  3. Both working in bot/api.py
  4. Merge conflicts when both PRs merge
  5. Time wasted resolving conflicts

Scenario 2: Bug in One Area Affects All

  1. Bug introduced in server endpoint
  2. Developer fixes bug in bot/api.py
  3. Accidentally breaks configuration endpoint
  4. Regression not caught in tests

Proposed Fix

Split api.py into route modules:

bot/
├── api.py                 # Main app initialization, middleware
├── routes/
│   ├── __init__.py
│   ├── config.py          # Configuration endpoints
│   ├── mood.py            # Mood management endpoints
│   ├── server.py          # Server operations endpoints
│   └── logs.py           # Log viewing endpoints

Example migration:

# bot/api.py - Simplified
from fastapi import FastAPI
from bot.routes import config, mood, server, logs

app = FastAPI()

# Include routers
app.include_router(config.router, prefix="/api/config", tags=["config"])
app.include_router(mood.router, prefix="/api/mood", tags=["mood"])
app.include_router(server.router, prefix="/api/server", tags=["server"])
app.include_router(logs.router, prefix="/api/logs", tags=["logs"])

# Middleware, error handlers, startup/shutdown
@app.middleware("http")
async def add_process_time_header(request, request_handler):
    # ... middleware code
# bot/routes/config.py - NEW FILE
from fastapi import APIRouter
from pydantic import BaseModel

router = APIRouter()

class ConfigUpdate(BaseModel):
    key: str
    value: str

@router.get("/")
async def get_config():
    """Get current configuration"""
    return config_manager.get_all()

@router.post("/")
async def update_config(update: ConfigUpdate):
    """Update configuration"""
    config_manager.set(update.key, update.value)
    return {"success": True}

Severity

MEDIUM - Large files are hard to maintain and cause merge conflicts.

Files Affected

bot/api.py (refactor), new files: bot/routes/init.py, bot/routes/config.py, bot/routes/mood.py, bot/routes/server.py, bot/routes/logs.py

bot/api.py is too large and handles multiple concerns, making it difficult to maintain and test. ## Where It Occurs - bot/api.py - Large file with multiple route groups - All endpoints in one file: configuration, mood, server, logs ## Why This Is a Problem 1. Large File: Difficult to navigate and understand 2. Multiple Concerns: Configuration, mood, server, logs mixed together 3. Testing: Hard to test routes in isolation 4. Merge Conflicts: Multiple developers modifying same file ## What Can Go Wrong ### Scenario 1: Merge Conflict 1. Developer A adds configuration endpoint 2. Developer B adds mood endpoint 3. Both working in bot/api.py 4. Merge conflicts when both PRs merge 5. Time wasted resolving conflicts ### Scenario 2: Bug in One Area Affects All 1. Bug introduced in server endpoint 2. Developer fixes bug in bot/api.py 3. Accidentally breaks configuration endpoint 4. Regression not caught in tests ## Proposed Fix Split api.py into route modules: ``` bot/ ├── api.py # Main app initialization, middleware ├── routes/ │ ├── __init__.py │ ├── config.py # Configuration endpoints │ ├── mood.py # Mood management endpoints │ ├── server.py # Server operations endpoints │ └── logs.py # Log viewing endpoints ``` Example migration: ```python # bot/api.py - Simplified from fastapi import FastAPI from bot.routes import config, mood, server, logs app = FastAPI() # Include routers app.include_router(config.router, prefix="/api/config", tags=["config"]) app.include_router(mood.router, prefix="/api/mood", tags=["mood"]) app.include_router(server.router, prefix="/api/server", tags=["server"]) app.include_router(logs.router, prefix="/api/logs", tags=["logs"]) # Middleware, error handlers, startup/shutdown @app.middleware("http") async def add_process_time_header(request, request_handler): # ... middleware code ``` ```python # bot/routes/config.py - NEW FILE from fastapi import APIRouter from pydantic import BaseModel router = APIRouter() class ConfigUpdate(BaseModel): key: str value: str @router.get("/") async def get_config(): """Get current configuration""" return config_manager.get_all() @router.post("/") async def update_config(update: ConfigUpdate): """Update configuration""" config_manager.set(update.key, update.value) return {"success": True} ``` ## Severity MEDIUM - Large files are hard to maintain and cause merge conflicts. ## Files Affected bot/api.py (refactor), new files: bot/routes/__init__.py, bot/routes/config.py, bot/routes/mood.py, bot/routes/server.py, bot/routes/logs.py
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Koko210/miku-discord#24