File size: 1,088 Bytes
136ae30
 
 
b5bf16b
 
 
 
 
 
136ae30
 
 
b5bf16b
 
 
 
 
 
 
 
 
 
 
 
 
136ae30
 
 
 
 
 
 
b5bf16b
 
 
 
136ae30
b5bf16b
 
 
 
 
 
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
import os
import httpx
from nbconvert import HTMLExporter
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.responses import FileResponse, JSONResponse, HTMLResponse
from starlette.requests import Request
from starlette.routing import Route

client = httpx.AsyncClient()
html_exporter = HTMLExporter(template_name="classic")


async def homepage(_):
    return FileResponse("static/index.html")


async def healthz(_):
    return JSONResponse({"success": True})


async def convert(req: Request):
    url = req.query_params.get("url")
    if not url:
        raise HTTPException(400, "Param url is missing")
    r = await client.get(
        url,
        headers={"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"},
        follow_redirects=True,
        # httpx no follow redirect by default
    )
    print(r.text)
    return HTMLResponse("<strong>FOO</strong>")


app = Starlette(
    debug=False,
    routes=[
        Route("/", homepage),
        Route("/healthz", healthz),
        Route("/convert", convert),
    ],
)