initial cacheserver
This commit is contained in:
13
Dockerfile
Normal file
13
Dockerfile
Normal file
@@ -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"]
|
||||||
32
main.py
Normal file
32
main.py
Normal file
@@ -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)
|
||||||
19
requirements.txt
Normal file
19
requirements.txt
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user