From af77633c6096290026764687c9ea8533c2626e9e Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Fri, 18 Jun 2021 06:36:30 +0200 Subject: [PATCH] initial cacheserver --- Dockerfile | 13 +++++++++++++ main.py | 32 ++++++++++++++++++++++++++++++++ requirements.txt | 19 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Dockerfile create mode 100644 main.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..03e146f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.7 + +RUN mkdir app + +COPY requirements.txt /app/ + +RUN pip install -r /app/requirements.txt + +EXPOSE 80 + +COPY main.py /app/main.py + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] diff --git a/main.py b/main.py new file mode 100644 index 0000000..6e18784 --- /dev/null +++ b/main.py @@ -0,0 +1,32 @@ +from fastapi import FastAPI +import requests +from fastapi.responses import FileResponse +import uvicorn +import hashlib +import pathlib +import logging + +app = FastAPI() +logger = logging.getLogger(__name__) + +cachepath = pathlib.Path('./cache') +if not cachepath.is_dir(): + cachepath.mkdir() + +def fetch_url(url): + urlhash = hashlib.sha1(url.encode('utf8')).hexdigest() + cachefile = (cachepath/urlhash) + if not cachefile.is_file(): + logger.info(f"caching file {cachefile} for {url}") + r = requests.get(url, allow_redirects=True) + open(cachefile, 'wb').write(r.content) + return FileResponse(cachefile) + +@app.get("/{url:path}") +async def root(url): + return fetch_url(url) + + + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=8000) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ed69723 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +aiofiles==0.7.0 +aiohttp==3.7.4.post0 +asgiref==3.3.4 +async-timeout==3.0.1 +attrs==21.2.0 +certifi==2021.5.30 +chardet==4.0.0 +click==8.0.1 +fastapi==0.65.2 +h11==0.12.0 +idna==2.10 +multidict==5.1.0 +pydantic==1.8.2 +requests==2.25.1 +starlette==0.14.2 +typing-extensions==3.10.0.0 +urllib3==1.26.5 +uvicorn==0.14.0 +yarl==1.6.3