Spaces:
Sleeping
Sleeping
Update services.py
Browse files- services.py +51 -53
services.py
CHANGED
|
@@ -1,53 +1,51 @@
|
|
| 1 |
-
# services.py
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
from typing import List
|
| 6 |
-
import json
|
| 7 |
-
|
| 8 |
-
app = FastAPI()
|
| 9 |
-
|
| 10 |
-
app.add_middleware(
|
| 11 |
-
CORSMiddleware,
|
| 12 |
-
allow_origins=["*"], # Allow all origins; use specific domains for security
|
| 13 |
-
allow_credentials=True,
|
| 14 |
-
allow_methods=["*"],
|
| 15 |
-
allow_headers=["*"],
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# Temporary user dictionary for storing credentials
|
| 19 |
-
USERS = {
|
| 20 |
-
"user1": "password1",
|
| 21 |
-
"user2": "password2",
|
| 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 |
-
return matching_users
|
|
|
|
| 1 |
+
# services.py
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import List
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"], # Allow all origins; use specific domains for security
|
| 13 |
+
allow_credentials=True,
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Temporary user dictionary for storing credentials
|
| 19 |
+
USERS = {
|
| 20 |
+
"user1": "password1",
|
| 21 |
+
"user2": "password2",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
class User(BaseModel):
|
| 25 |
+
username: str
|
| 26 |
+
password: str
|
| 27 |
+
|
| 28 |
+
@app.post("/register")
|
| 29 |
+
async def register_user(user: User):
|
| 30 |
+
"""Register a new user."""
|
| 31 |
+
if user.username in USERS:
|
| 32 |
+
raise HTTPException(status_code=400, detail="Username already exists")
|
| 33 |
+
USERS[user.username] = user.password
|
| 34 |
+
return {"message": "User registered successfully"}
|
| 35 |
+
|
| 36 |
+
@app.post("/login")
|
| 37 |
+
async def login_user(user: User):
|
| 38 |
+
"""Authenticate a user."""
|
| 39 |
+
if user.username not in USERS or USERS[user.username] != user.password:
|
| 40 |
+
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 41 |
+
return {"message": "Login successful"}
|
| 42 |
+
|
| 43 |
+
@app.get("/search")
|
| 44 |
+
async def search_users(query: str) -> List[str]:
|
| 45 |
+
"""Search for users by username."""
|
| 46 |
+
matching_users = [username for username in USERS if query.lower() in username.lower()]
|
| 47 |
+
|
| 48 |
+
if not matching_users:
|
| 49 |
+
raise HTTPException(status_code=404, detail="No users found matching the query")
|
| 50 |
+
|
| 51 |
+
return matching_users
|
|
|
|
|
|