4.5 KiB
4.5 KiB
API Refactor v2.0 - Complete Redesign
Overview
The API has been completely refactored to be standalone and independent. It no longer depends on bot modules and contains all necessary code within the api/ directory.
Changes
✅ Completed
-
Cleaned root directory:
- Moved all
.mddocumentation files todocs/archive/ - Moved migration scripts to
scripts/ - Root is now clean with only essential config files
- Moved all
-
Created standalone API modules:
api/database.py- Complete database operations (no bot dependency)api/world_loader.py- Game world loader with data modelsapi/items.py- Items managerapi/game_logic.py- All game mechanicsapi/main_new.py- New standalone FastAPI application
-
New database schema:
players.idis now the primary key (auto-increment)telegram_idis optional (nullable) for Telegram usersusername/password_hashfor web users- All foreign keys now reference
players.idinstead oftelegram_id
-
Simplified deployment:
- Removed unnecessary nginx complexity
- Traefik handles all routing
- PWA serves static files via nginx (efficient for static content)
- API is completely standalone
Migration Path
Option 1: Fresh Start (Recommended)
Pros: Clean database, no migration issues
Cons: Loses existing Telegram user data
# 1. Stop all containers
docker compose down
# 2. Remove old database
docker volume rm echoes-of-the-ashes-postgres-data
# 3. Update files
mv api/main_new.py api/main.py
mv api/requirements_new.txt api/requirements.txt
mv Dockerfile.api.new Dockerfile.api
# 4. Rebuild and start
docker compose up -d --build
Option 2: Migrate Existing Data
Pros: Keeps Telegram user data
Cons: Requires running migration script
# 1. Create migration script to:
# - Add `id` column as primary key
# - Make `telegram_id` nullable
# - Update all foreign keys
# - Backfill `id` values
# 2. Run migration
docker exec -it echoes_of_the_ashes_api python scripts/migrate_to_v2.py
# 3. Update files and rebuild
# (same as Option 1 steps 3-4)
New API Structure
api/
├── main_new.py # Standalone FastAPI app
├── database.py # All database operations
├── world_loader.py # World data loading
├── items.py # Items management
├── game_logic.py # Game mechanics
├── internal.py # (deprecated - logic moved to main)
└── requirements_new.txt # Minimal dependencies
Bot Integration
The bot will now call the API for all operations instead of directly accessing the database.
Bot Changes Needed:
-
Replace direct database calls with API calls using
httpx:# Old: player = await get_player(telegram_id) # New: response = await http_client.get( f"{API_URL}/api/internal/player/{telegram_id}", headers={"Authorization": f"Bearer {INTERNAL_KEY}"} ) player = response.json() -
Use internal endpoints (protected by API key):
GET /api/internal/player/{telegram_id}- Get playerPOST /api/internal/player- Create player- All other game operations use public endpoints with JWT
Environment Variables
# Database
POSTGRES_USER=your_user
POSTGRES_PASSWORD=your_password
POSTGRES_DB=echoes_db
POSTGRES_HOST=echoes_of_the_ashes_db
POSTGRES_PORT=5432
# API
JWT_SECRET_KEY=your-jwt-secret-key
API_INTERNAL_KEY=your-internal-api-key
# Bot (if using)
TELEGRAM_BOT_TOKEN=your-bot-token
Testing the New API
-
Health check:
curl https://your-domain.com/health -
Register web user:
curl -X POST https://your-domain.com/api/auth/register \ -H "Content-Type: application/json" \ -d '{"username":"testuser","password":"testpass"}' -
Get location:
curl https://your-domain.com/api/game/location \ -H "Authorization: Bearer YOUR_JWT_TOKEN"
Benefits
- Standalone - API has zero bot dependencies
- Clean - All logic in one place
- Testable - Easy to test without bot infrastructure
- Maintainable - Clear separation of concerns
- Scalable - API and bot can scale independently
- Flexible - Easy to add new clients (mobile app, etc.)
Next Steps
- Choose migration path (fresh start vs migrate)
- Update and rebuild containers
- Test web interface
- Refactor bot to use API endpoints
- Remove old
api/main.pyandapi/internal.py