-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_server.py
More file actions
142 lines (128 loc) Β· 4.15 KB
/
test_server.py
File metadata and controls
142 lines (128 loc) Β· 4.15 KB
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
#!/usr/bin/env python3
"""
Simple test server for FloatChat without database dependencies.
This allows us to test the API endpoints while Docker is starting up.
"""
import asyncio
from typing import Dict, Any
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
import uvicorn
from app.core.config import get_settings
from app.services.real_gemini_service import real_gemini_service
# Get settings
settings = get_settings()
# Create FastAPI app
app = FastAPI(
title="FloatChat Test Server",
description="Simplified test server for FloatChat API testing",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
"""Root endpoint with system status."""
return {
"message": "FloatChat Test Server is running!",
"status": "online",
"version": "1.0.0",
"gemini_available": real_gemini_service.available,
"docs": "/docs"
}
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {
"status": "healthy",
"timestamp": asyncio.get_event_loop().time(),
"services": {
"gemini": real_gemini_service.available,
"database": "bypassed_for_testing"
}
}
@app.post("/api/v1/chat/query")
async def test_chat_query(request: Dict[str, Any]):
"""Test chat endpoint without database."""
try:
query = request.get("query", "")
if not query:
raise HTTPException(status_code=400, detail="Query is required")
# Test Gemini API
if real_gemini_service.available:
response = await real_gemini_service.analyze_ocean_query(query)
return {
"success": True,
"message": response.get("message", "Response generated successfully"),
"query_type": response.get("query_type", "unknown"),
"confidence": response.get("confidence", 0.8),
"data_source": "test_mode",
"processing_time_ms": 150
}
else:
return {
"success": True,
"message": f"Test response for: {query}",
"query_type": "test",
"confidence": 1.0,
"data_source": "mock",
"processing_time_ms": 50
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Chat processing failed: {str(e)}")
@app.get("/api/v1/floats/test")
async def test_floats():
"""Test floats endpoint with mock data."""
return {
"success": True,
"data": [
{
"float_id": "test_001",
"latitude": 20.5,
"longitude": 75.3,
"temperature": 28.5,
"salinity": 35.2,
"date": "2024-09-18"
},
{
"float_id": "test_002",
"latitude": 21.0,
"longitude": 76.0,
"temperature": 29.1,
"salinity": 35.5,
"date": "2024-09-18"
}
],
"count": 2,
"data_source": "test_mode"
}
@app.get("/test", response_class=HTMLResponse)
async def test_page():
"""Serve the test HTML page."""
try:
with open("test_api.html", "r", encoding="utf-8") as f:
return HTMLResponse(content=f.read())
except FileNotFoundError:
return HTMLResponse(content="<h1>Test page not found</h1><p>test_api.html is missing</p>")
if __name__ == "__main__":
print("π Starting FloatChat Test Server...")
print("π Dashboard: http://localhost:8001/test")
print("π API Docs: http://localhost:8001/docs")
print("π Health Check: http://localhost:8001/health")
uvicorn.run(
"test_server:app",
host="0.0.0.0",
port=8001,
reload=True,
log_level="info"
)