File size: 2,551 Bytes
2299bb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Chat App - FastAPI + React (HuggingFace Space)

from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os

app = FastAPI(title="Chat App", version="1.0.0")

# Enable CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Get the directory where this file is located
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# Mount frontend static files
frontend_path = os.path.join(BASE_DIR, "frontend")
if os.path.exists(frontend_path):
    app.mount("/frontend", StaticFiles(directory=frontend_path), name="frontend")

# API Routes
@app.get("/")
async def root(request: Request):
    """Serve the frontend index.html"""
    index_path = os.path.join(BASE_DIR, "index.html")
    if os.path.exists(index_path):
        with open(index_path, "r") as f:
            content = f.read()
        return HTMLResponse(content=content)
    return {"message": "Chat App API", "status": "running"}

@app.get("/api/health")
async def health():
    """Health check endpoint"""
    return {"status": "healthy", "service": "chat-app"}

@app.get("/api/v1")
async def api_info():
    """API info endpoint"""
    return {
        "name": "Chat App API",
        "version": "1.0.0",
        "endpoints": {
            "auth": "/api/v1/auth",
            "users": "/api/v1/users", 
            "chat": "/api/v1/chat",
            "groups": "/api/v1/groups"
        }
    }

# Auth endpoints
@app.post("/api/v1/auth/register")
async def register(data: dict):
    return {"message": "Register endpoint - requires database"}

@app.post("/api/v1/auth/login")
async def login(data: dict):
    return {"message": "Login endpoint - requires database"}

# Chat endpoints
@app.get("/api/v1/chat/conversations")
async def get_conversations():
    return {"conversations": [], "message": "Requires database setup"}

@app.post("/api/v1/chat/send")
async def send_message(data: dict):
    return {"message": "Send message - requires database"}

# Group endpoints
@app.get("/api/v1/groups")
async def get_groups():
    return {"groups": [], "message": "Requires database setup"}

@app.post("/api/v1/groups/create")
async def create_group(data: dict):
    return {"message": "Create group - requires database"}

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860))
    uvicorn.run(app, host="0.0.0.0", port=port)