{ "name": "Advanced FastAPI App", "type": "web-python", "version": "0.1.0", "description": "An advanced FastAPI web application with database integration, authentication, and real-time features.", "tags": ["web", "api", "fastapi", "advanced", "database", "auth"], "ui": "", "cover": "", "icon": "🚀", "api_version": "0.1.8", "requirements": ["fastapi==0.112.1", "pydantic==2.5.0", "passlib==1.7.4", "python-jose==3.3.0"], "dependencies": [], "startup_config": { "timeout": 60, "wait_for_service": "advanced-fastapi", "stop_after_inactive": 600 } } """ # Authentication endpoints @app.post("/auth/register") async def register(user_data: UserCreate): # Check if user already exists for user in users_db.values(): if user["username"] == user_data.username or user["email"] == user_data.email: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="User already exists" ) # Create new user user_id = str(uuid.uuid4()) user = { "id": user_id, "username": user_data.username, "email": user_data.email, "password": hash_password(user_data.password), "created_at": datetime.now() } users_db[user_id] = user # Remove password from response user_response = {k: v for k, v in user.items() if k != "password"} return {"message": "User created successfully", "user": user_response} @app.post("/auth/login") async def login(credentials: dict): username = credentials.get("username") password = credentials.get("password") # Find user user = None for u in users_db.values(): if u["username"] == username: user = u break if not user or not verify_password(password, user["password"]): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials" ) # Create session session_id = create_session(user["id"]) return { "access_token": session_id, "token_type": "bearer", "user": {k: v for k, v in user.items() if k != "password"} } # User endpoints @app.get("/users/me") async def get_current_user_info(current_user: dict = Depends(get_current_user)): return current_user @app.get("/users/") async def get_users(current_user: dict = Depends(get_current_user)): return [ {k: v for k, v in user.items() if k != "password"} for user in users_db.values() ] # Task endpoints @app.get("/tasks/") async def get_tasks(current_user: dict = Depends(get_current_user)): user_tasks = [ task for task in tasks_db.values() if task["user_id"] == current_user["id"] ] return user_tasks @app.post("/tasks/") async def create_task(task_data: TaskCreate, current_user: dict = Depends(get_current_user)): task_id = str(uuid.uuid4()) task = { "id": task_id, "title": task_data.title, "description": task_data.description, "completed": False, "created_at": datetime.now(), "user_id": current_user["id"] } tasks_db[task_id] = task return task @app.get("/tasks/{task_id}") async def get_task(task_id: str, current_user: dict = Depends(get_current_user)): task = tasks_db.get(task_id) if not task or task["user_id"] != current_user["id"]: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Task not found" ) return task @app.put("/tasks/{task_id}") async def update_task( task_id: str, task_data: TaskUpdate, current_user: dict = Depends(get_current_user) ): task = tasks_db.get(task_id) if not task or task["user_id"] != current_user["id"]: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Task not found" ) # Update task if task_data.title is not None: task["title"] = task_data.title if task_data.description is not None: task["description"] = task_data.description if task_data.completed is not None: task["completed"] = task_data.completed task["updated_at"] = datetime.now() return task @app.delete("/tasks/{task_id}") async def delete_task(task_id: str, current_user: dict = Depends(get_current_user)): task = tasks_db.get(task_id) if not task or task["user_id"] != current_user["id"]: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Task not found" ) del tasks_db[task_id] return {"message": "Task deleted successfully"} # Analytics endpoints @app.get("/analytics/stats") async def get_analytics(): total_users = len(users_db) total_tasks = len(tasks_db) completed_tasks = sum(1 for task in tasks_db.values() if task["completed"]) active_sessions = len(sessions_db) return { "total_users": total_users, "total_tasks": total_tasks, "completed_tasks": completed_tasks, "pending_tasks": total_tasks - completed_tasks, "active_sessions": active_sessions, "completion_rate": (completed_tasks / total_tasks * 100) if total_tasks > 0 else 0, "timestamp": datetime.now().isoformat() } @app.get("/analytics/health") async def health_check(): return { "status": "healthy", "service": "advanced-fastapi", "version": "2.0.0", "timestamp": datetime.now().isoformat(), "database": { "users": len(users_db), "tasks": len(tasks_db), "sessions": len(sessions_db) } } return app async def setup(): # Register advanced FastAPI app fastapi_app = create_advanced_fastapi_app() async def serve_fastapi(args): await fastapi_app(args["scope"], args["receive"], args["send"]) await api.register_service({ "id": "advanced-fastapi", "type": "asgi", "serve": serve_fastapi, "config": { "visibility": "public" } }, {"overwrite": True}) api.export({"setup": setup})