csaybar commited on
Commit
77cc323
·
verified ·
1 Parent(s): 1e8775d

Rename index.html to app.py

Browse files
Files changed (2) hide show
  1. app.py +72 -0
  2. index.html +0 -19
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from urllib.parse import urlparse
5
+ import httpx
6
+
7
+ ALLOWED_HOSTS = {
8
+ "huggingface.co",
9
+ "cas-bridge.xethub.hf.co",
10
+ # Add more if you need: "cdn-lfs.huggingface.co", etc.
11
+ }
12
+
13
+ app = FastAPI(title="cozip CORS proxy")
14
+
15
+ app.add_middleware(
16
+ CORSMiddleware,
17
+ allow_origins=["*"],
18
+ allow_methods=["GET", "HEAD", "OPTIONS"],
19
+ allow_headers=["Range", "Content-Type"],
20
+ expose_headers=["Content-Range", "Accept-Ranges", "Content-Length", "Content-Type"],
21
+ max_age=86400,
22
+ )
23
+
24
+
25
+ @app.get("/")
26
+ async def root():
27
+ return {
28
+ "service": "cozip CORS proxy",
29
+ "usage": "/proxy?url=<cozip_url>",
30
+ "allowed_hosts": sorted(ALLOWED_HOSTS),
31
+ }
32
+
33
+
34
+ @app.api_route("/proxy", methods=["GET", "HEAD"])
35
+ async def proxy(url: str, request: Request):
36
+ parsed = urlparse(url)
37
+ if parsed.hostname not in ALLOWED_HOSTS:
38
+ raise HTTPException(
39
+ status_code=403,
40
+ detail=f"Host not allowed: {parsed.hostname}",
41
+ )
42
+
43
+ fwd_headers = {}
44
+ if "range" in request.headers:
45
+ fwd_headers["Range"] = request.headers["range"]
46
+
47
+ client = httpx.AsyncClient(follow_redirects=True, timeout=60.0)
48
+ try:
49
+ req = client.build_request(request.method, url, headers=fwd_headers)
50
+ upstream = await client.send(req, stream=True)
51
+ except Exception as e:
52
+ await client.aclose()
53
+ raise HTTPException(status_code=502, detail=f"Upstream error: {e}")
54
+
55
+ response_headers = {}
56
+ for h in ("content-range", "accept-ranges", "content-length", "content-type"):
57
+ if h in upstream.headers:
58
+ response_headers[h] = upstream.headers[h]
59
+
60
+ async def stream():
61
+ try:
62
+ async for chunk in upstream.aiter_bytes(chunk_size=65536):
63
+ yield chunk
64
+ finally:
65
+ await upstream.aclose()
66
+ await client.aclose()
67
+
68
+ return StreamingResponse(
69
+ stream(),
70
+ status_code=upstream.status_code,
71
+ headers=response_headers,
72
+ )
index.html DELETED
@@ -1,19 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>