Spaces:
Sleeping
Sleeping
File size: 8,198 Bytes
f84a02d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | #!/usr/bin/env python3
"""
Minimal Backend API Server for ATOM Platform
Simple FastAPI server that provides core functionality without complex dependencies
"""
from datetime import datetime
import logging
import os
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# Create FastAPI app
app = FastAPI(
title="ATOM Minimal API",
description="Minimal backend API server for ATOM platform",
version="1.0.0-minimal",
docs_url="/docs",
redoc_url="/redoc",
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Pydantic models
class HealthResponse(BaseModel):
status: str
service: str
version: str
timestamp: str
message: str
class OAuthStatusResponse(BaseModel):
ok: bool
service: str
status: str
message: str
timestamp: str
class ServiceListResponse(BaseModel):
ok: bool
services: list
total_services: int
timestamp: str
class SearchRequest(BaseModel):
query: str
user_id: str = "test_user"
class SearchResponse(BaseModel):
ok: bool
query: str
results: list
timestamp: str
class ChatRequest(BaseModel):
message: str
user_id: str = "test_user"
class ChatResponse(BaseModel):
ok: bool
message: str
response: str
timestamp: str
# Mock data for demonstration
MOCK_SERVICES = [
"gmail",
"outlook",
"slack",
"teams",
"trello",
"asana",
"notion",
"github",
"dropbox",
"gdrive",
]
MOCK_SEARCH_RESULTS = [
{
"title": "Meeting Notes",
"source": "gmail",
"snippet": "Discussion about project timelines",
},
{
"title": "Project Plan",
"source": "notion",
"snippet": "Complete project roadmap and milestones",
},
{
"title": "Team Updates",
"source": "slack",
"snippet": "Weekly team sync meeting notes",
},
{
"title": "Calendar Event",
"source": "outlook",
"snippet": "Client meeting scheduled for next week",
},
{
"title": "Task List",
"source": "asana",
"snippet": "Pending tasks for current sprint",
},
]
# API Routes
@app.get("/", response_model=HealthResponse)
async def root():
"""Root endpoint with basic info"""
return HealthResponse(
status="ok",
service="atom-minimal-api",
version="1.0.0",
timestamp=datetime.now().isoformat(),
message="ATOM Minimal API Server is running",
)
@app.get("/health", response_model=HealthResponse)
async def health_check():
"""Health check endpoint"""
return HealthResponse(
status="ok",
service="atom-minimal-api",
version="1.0.0",
timestamp=datetime.now().isoformat(),
message="API server is healthy and running",
)
@app.get("/api/oauth/{service}/status", response_model=OAuthStatusResponse)
async def oauth_status(service: str, user_id: str = "test_user"):
"""Check OAuth status for a service"""
if service not in MOCK_SERVICES:
raise HTTPException(status_code=404, detail=f"Service {service} not found")
return OAuthStatusResponse(
ok=True,
service=service,
status="connected"
if service
in ["gmail", "slack", "trello", "asana", "notion", "dropbox", "gdrive"]
else "needs_credentials",
message=f"{service.title()} OAuth is connected"
if service
in ["gmail", "slack", "trello", "asana", "notion", "dropbox", "gdrive"]
else f"{service.title()} OAuth needs credentials",
timestamp=datetime.now().isoformat(),
)
@app.get("/api/oauth/services", response_model=ServiceListResponse)
async def list_services():
"""List all available services"""
return ServiceListResponse(
ok=True,
services=MOCK_SERVICES,
total_services=len(MOCK_SERVICES),
timestamp=datetime.now().isoformat(),
)
@app.post("/api/search", response_model=SearchResponse)
async def search_content(request: SearchRequest):
"""Search across all connected services"""
logger.info(f"Search query: {request.query} from user: {request.user_id}")
# Filter mock results based on query
filtered_results = [
result
for result in MOCK_SEARCH_RESULTS
if request.query.lower() in result["title"].lower()
or request.query.lower() in result["snippet"].lower()
]
return SearchResponse(
ok=True,
query=request.query,
results=filtered_results[:5], # Limit to 5 results
timestamp=datetime.now().isoformat(),
)
@app.post("/api/chat", response_model=ChatResponse)
async def chat_message(request: ChatRequest):
"""Handle chat messages"""
logger.info(f"Chat message: {request.message} from user: {request.user_id}")
# Simple response logic
if "search" in request.message.lower():
response = "I can help you search across your connected services. Try using the search endpoint or tell me what you're looking for."
elif "oauth" in request.message.lower() or "connect" in request.message.lower():
response = "I can help you connect services. Currently available services include Gmail, Slack, Asana, Notion, and more."
elif "help" in request.message.lower():
response = "I'm your ATOM assistant. I can help you search across your connected services, manage OAuth connections, and coordinate your workflow."
else:
response = f"I received your message: '{request.message}'. I'm here to help you coordinate across your connected services and workflows."
return ChatResponse(
ok=True,
message=request.message,
response=response,
timestamp=datetime.now().isoformat(),
)
@app.get("/api/user/{user_id}/services")
async def get_user_services(user_id: str):
"""Get services connected for a specific user"""
connected_services = [
"gmail",
"slack",
"trello",
"asana",
"notion",
"dropbox",
"gdrive",
]
return {
"ok": True,
"user_id": user_id,
"connected_services": connected_services,
"total_connected": len(connected_services),
"available_services": MOCK_SERVICES,
"timestamp": datetime.now().isoformat(),
}
@app.get("/api/system/status")
async def system_status():
"""Get overall system status"""
return {
"ok": True,
"backend": "running",
"oauth_server": "running",
"database": "connected",
"services_registered": len(MOCK_SERVICES),
"active_users": 1,
"timestamp": datetime.now().isoformat(),
"message": "ATOM system is operational",
}
def start_minimal_api():
"""Start the minimal API server"""
print("🚀 ATOM Minimal Backend API Server")
print("=" * 50)
print("🌐 Starting server on http://localhost:8000")
print("📋 Available Endpoints:")
print(" - GET / - Root endpoint")
print(" - GET /health - Health check")
print(" - GET /docs - API documentation")
print(" - GET /api/oauth/services - List services")
print(" - GET /api/oauth/{service}/status - Service OAuth status")
print(" - POST /api/search - Search across services")
print(" - POST /api/chat - Chat interface")
print(" - GET /api/user/{user_id}/services - User services")
print(" - GET /api/system/status - System status")
print("=" * 50)
try:
uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")
except KeyboardInterrupt:
print("\n🛑 Server stopped by user")
except Exception as e:
logger.error(f"Failed to start server: {e}")
if __name__ == "__main__":
start_minimal_api()
|