Spaces:
Paused

File size: 1,742 Bytes
2bbb987
 
 
4a03be1
2bbb987
 
 
 
 
 
 
4a03be1
2bbb987
4a03be1
2bbb987
4a03be1
 
 
 
 
2bbb987
4a03be1
 
 
 
 
 
 
 
2bbb987
4a03be1
2bbb987
 
 
 
 
 
 
 
 
 
 
 
4a03be1
2bbb987
4a03be1
 
 
 
2bbb987
 
4a03be1
 
 
 
2bbb987
 
 
 
 
 
 
 
 
 
 
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
import time
import uuid

from fastapi import FastAPI, HTTPException, Request, Response

app = FastAPI()

ALLOWED_ORIGIN = "https://dash-4ek5uj.example.com"


@app.middleware("http")
async def cors_and_headers(request: Request, call_next):
    start = time.perf_counter()
    origin = request.headers.get("origin")

    # Handle CORS preflight
    if request.method == "OPTIONS":
        response = Response(status_code=200)
    else:
        response = await call_next(request)

    # Allow only the assigned origin
    if origin == ALLOWED_ORIGIN:
        response.headers["Access-Control-Allow-Origin"] = ALLOWED_ORIGIN
        response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
        response.headers["Access-Control-Allow-Headers"] = "*"
        response.headers["Vary"] = "Origin"

    # Required headers on every response
    response.headers["X-Request-ID"] = str(uuid.uuid4())
    response.headers["X-Process-Time"] = f"{time.perf_counter() - start:.6f}"

    return response


@app.get("/")
def home():
    return {"message": "FastAPI Stats API is running."}


@app.get("/stats")
def stats(values: str):
    try:
        numbers = [int(x.strip()) for x in values.split(",") if x.strip()]
    except ValueError:
        raise HTTPException(
            status_code=400,
            detail="Values must be comma-separated integers."
        )

    if not numbers:
        raise HTTPException(
            status_code=400,
            detail="No values provided."
        )

    total = sum(numbers)

    return {
        "email": "24f3005189@ds.study.iitm.ac.in",
        "count": len(numbers),
        "sum": total,
        "min": min(numbers),
        "max": max(numbers),
        "mean": total / len(numbers),
    }