aboutsummaryrefslogtreecommitdiffstats
path: root/web/dev-server/main.py
blob: ab0adf20b852a8befe33b5094275c6f4fac93493 (plain)
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
import os
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from starlette.responses import Response, FileResponse
import httpx
from dotenv import load_dotenv

app = FastAPI()
load_dotenv()

auth = httpx.BasicAuth(username=os.environ.get("GONDUL_USERNAME"), password=os.environ.get("GONDUL_PASSWORD"))

@app.get("/")
async def root():
    return FileResponse("../index.html")

@app.get("/api/{path:path}")
async def tile_request(path: str, response: Response):
    async with httpx.AsyncClient() as client:
        proxy = await client.get(f"{os.environ.get("GONDUL_URL")}/api/{path}", auth=auth)
    response.body = proxy.content
    response.status_code = proxy.status_code
    return response

@app.post("/api/{path:path}")
async def tile_post_request(path: str, response: Response):
    async with httpx.AsyncClient() as client:
        proxy = await client.get(f"{os.environ.get("GONDUL_URL")}/api/{path}", auth=auth)
    response.body = proxy.content
    response.status_code = proxy.status_code
    return response

# Mount web root
app.mount("/", StaticFiles(directory="../"))