This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from typing import Dict, Any, Optional, List
|
||||
|
||||
@@ -52,11 +53,11 @@ app.add_middleware(
|
||||
|
||||
# Service registry
|
||||
SERVICES = {
|
||||
"users": "http://localhost:8001",
|
||||
"emergency": "http://localhost:8002",
|
||||
"location": "http://localhost:8003",
|
||||
"calendar": "http://localhost:8004",
|
||||
"notifications": "http://localhost:8005",
|
||||
"users": os.getenv("USER_SERVICE_URL", "http://localhost:8001"),
|
||||
"emergency": os.getenv("EMERGENCY_SERVICE_URL", "http://localhost:8002"),
|
||||
"location": os.getenv("LOCATION_SERVICE_URL", "http://localhost:8003"),
|
||||
"calendar": os.getenv("CALENDAR_SERVICE_URL", "http://localhost:8004"),
|
||||
"notifications": os.getenv("NOTIFICATION_SERVICE_URL", "http://localhost:8005"),
|
||||
}
|
||||
|
||||
# Rate limiting (simple in-memory implementation)
|
||||
@@ -202,8 +203,31 @@ async def register_user(user_create: UserCreate, request: Request):
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
elif response.status_code == 400:
|
||||
# Handle specific registration errors
|
||||
try:
|
||||
error_json = response.json()
|
||||
error_detail = error_json.get("detail", "Registration failed")
|
||||
if "Email already registered" in error_detail:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Email {user_create.email} is already registered. Please use a different email or try logging in."
|
||||
)
|
||||
elif "Username already taken" in error_detail:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Username {user_create.username} is already taken. Please choose a different username."
|
||||
)
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=error_detail)
|
||||
except ValueError:
|
||||
# JSON parsing failed
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Registration failed. Please check your input data."
|
||||
)
|
||||
else:
|
||||
error_detail = response.text
|
||||
error_detail = "Registration failed"
|
||||
try:
|
||||
error_json = response.json()
|
||||
error_detail = error_json.get("detail", error_detail)
|
||||
@@ -214,7 +238,10 @@ async def register_user(user_create: UserCreate, request: Request):
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Registration error: {str(e)}")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Registration service error: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/login", response_model=Token, tags=["Authentication"], summary="Login user")
|
||||
@@ -248,6 +275,46 @@ async def login_user(user_login: UserLogin, request: Request):
|
||||
raise HTTPException(status_code=500, detail=f"Login error: {str(e)}")
|
||||
|
||||
|
||||
# Utility endpoints
|
||||
@app.get("/api/v1/auth/check-email", tags=["Authentication"], summary="Check if email is available")
|
||||
async def check_email_availability(email: str):
|
||||
"""Check if email is available for registration"""
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
try:
|
||||
# Make request to user service to check if email exists
|
||||
response = await client.get(
|
||||
f"{SERVICES['users']}/api/v1/check-email",
|
||||
params={"email": email}
|
||||
)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
return {"available": True, "message": "Email is available"}
|
||||
except Exception:
|
||||
return {"available": True, "message": "Unable to check availability"}
|
||||
|
||||
|
||||
@app.get("/api/v1/auth/generate-test-data", tags=["Development"], summary="Generate test user data")
|
||||
async def generate_test_user_data():
|
||||
"""Generate unique test user data for development"""
|
||||
import random
|
||||
import string
|
||||
|
||||
# Generate unique email with timestamp
|
||||
from datetime import datetime
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=4))
|
||||
|
||||
return {
|
||||
"email": f"test_{timestamp}_{random_suffix}@example.com",
|
||||
"username": f"testuser_{timestamp}_{random_suffix}",
|
||||
"password": "TestPassword123!",
|
||||
"first_name": "Test",
|
||||
"last_name": "User",
|
||||
"phone": f"+123456{random.randint(1000, 9999)}"
|
||||
}
|
||||
|
||||
|
||||
# User Service routes
|
||||
@app.post("/api/v1/users/register", operation_id="user_register", response_model=UserResponse, tags=["Users"], summary="Register a new user")
|
||||
@app.get("/api/v1/users/me", operation_id="user_me_get", response_model=UserResponse, tags=["Users"], summary="Get current user profile")
|
||||
|
||||
@@ -43,12 +43,12 @@ check_api_response() {
|
||||
echo -e "\n${GREEN}1. Проверка доступности сервисов${NC}"
|
||||
|
||||
services=(
|
||||
"http://localhost:8000"
|
||||
"http://localhost:8001"
|
||||
"http://localhost:8002"
|
||||
"http://localhost:8003"
|
||||
"http://localhost:8004"
|
||||
"http://localhost:8005"
|
||||
"http://192.168.0.103:8000"
|
||||
"http://192.168.0.103:8001"
|
||||
"http://192.168.0.103:8002"
|
||||
"http://192.168.0.103:8003"
|
||||
"http://192.168.0.103:8004"
|
||||
"http://192.168.0.103:8005"
|
||||
)
|
||||
|
||||
service_names=(
|
||||
|
||||
Reference in New Issue
Block a user