from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles import uvicorn import os from datetime import datetime import json from typing import Dict, List app = FastAPI(title="Docker Demo App", version="1.0.0") # Sample data store (in-memory) data_store: Dict[str, Dict] = {} @app.get("/", response_class=HTMLResponse) async def root(): """Main page with the anycoder attribution""" html_content = """ Docker App on HuggingFace

🐳 Docker Application on HuggingFace Spaces

Welcome! This is a Docker-based FastAPI application running on HuggingFace Spaces.

Available Endpoints:

GET /health - Health check endpoint
GET /items - List all items
POST /items - Create a new item
GET /items/{item_id} - Get a specific item
DELETE /items/{item_id} - Delete an item
GET /info - System information
GET /docs - Interactive API documentation
""" return html_content @app.get("/health") async def health_check(): """Health check endpoint""" return { "status": "healthy", "timestamp": datetime.utcnow().isoformat(), "version": "1.0.0" } @app.get("/items", response_model=List[Dict]) async def list_items(): """List all items""" return list(data_store.values()) @app.post("/items") async def create_item(item: Dict): """Create a new item""" if "id" not in item: raise HTTPException(status_code=400, detail="Item must have an 'id' field") item_id = str(item["id"]) if item_id in data_store: raise HTTPException(status_code=409, detail="Item with this ID already exists") item["created_at"] = datetime.utcnow().isoformat() data_store[item_id] = item return {"message": "Item created successfully", "item": item} @app.get("/items/{item_id}") async def get_item(item_id: str): """Get a specific item""" if item_id not in data_store: raise HTTPException(status_code=404, detail="Item not found") return data_store[item_id] @app.delete("/items/{item_id}") async def delete_item(item_id: str): """Delete an item""" if item_id not in data_store: raise HTTPException(status_code=404, detail="Item not found") deleted_item = data_store.pop(item_id) return {"message": "Item deleted successfully", "item": deleted_item} @app.get("/info") async def system_info(): """Get system information""" return { "app_name": "Docker Demo App", "environment": os.getenv("ENVIRONMENT", "development"), "python_version": os.sys.version, "docker": True, "platform": os.name, "total_items": len(data_store) } # Add some sample data on startup @app.on_event("startup") async def startup_event(): """Initialize with sample data""" sample_items = [ {"id": 1, "name": "Sample Item 1", "description": "This is a sample item"}, {"id": 2, "name": "Sample Item 2", "description": "Another sample item"}, {"id": 3, "name": "Docker Demo", "description": "Running in Docker container"} ] for item in sample_items: item_id = str(item["id"]) item["created_at"] = datetime.utcnow().isoformat() data_store[item_id] = item if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)