diff --git a/fastapi/.dockerignore b/fastapi/.dockerignore new file mode 100644 index 0000000..1a940b2 --- /dev/null +++ b/fastapi/.dockerignore @@ -0,0 +1,4 @@ +Dockerfile +docker-compose.yml +start.sh +stop.sh \ No newline at end of file diff --git a/fastapi/Dockerfile b/fastapi/Dockerfile new file mode 100644 index 0000000..07dd2c6 --- /dev/null +++ b/fastapi/Dockerfile @@ -0,0 +1,16 @@ +FROM python:3.7.2 + +ADD ./ /fastapi + +WORKDIR fastapi + +RUN pip3 install -r /fastapi/requirements.txt + +WORKDIR /fastapi + +EXPOSE 8080 + +ENTRYPOINT ["/fastapi/conf/docker-entrypoint.sh"] + +CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"] + \ No newline at end of file diff --git a/fastapi/app.py b/fastapi/app.py new file mode 100644 index 0000000..b4b0ce9 --- /dev/null +++ b/fastapi/app.py @@ -0,0 +1,29 @@ +import asyncpg +import os + +import uvicorn +from fastapi import FastAPI + +app = FastAPI() + + +@app.on_event("startup") +async def register_db(): + app.db = await asyncpg.create_pool(dsn=os.environ['DATABASE_URL']) + + +@app.on_event("shutdown") +async def cleanup(): + await app.db.close() + + +@app.get('/db') +async def handle_db(): + async with app.db.acquire() as connection: + result = await connection.fetchval("SELECT html FROM render_cache_pagehtml WHERE url='startmatter.com'") + return result + + + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=8080) \ No newline at end of file diff --git a/fastapi/conf/docker-entrypoint.sh b/fastapi/conf/docker-entrypoint.sh new file mode 100755 index 0000000..2b0204b --- /dev/null +++ b/fastapi/conf/docker-entrypoint.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +sleep 5s +exec "$@" diff --git a/fastapi/docker-compose.yml b/fastapi/docker-compose.yml new file mode 100644 index 0000000..94f408c --- /dev/null +++ b/fastapi/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3' +services: + postgres: + restart: always + build: .././database/ + ports: + - "5432:5432" + environment: + - POSTGRES_PASSWORD=render + - POSTGRES_USER=render + - POSTGRES_DB=render + + fastapi: + restart: always + build: . + ports: + - "8080:8080" + depends_on: + - postgres + environment: + - DATABASE_URL=postgres://render:render@postgres:5432/render + \ No newline at end of file diff --git a/fastapi/requirements.txt b/fastapi/requirements.txt new file mode 100644 index 0000000..632c3b5 --- /dev/null +++ b/fastapi/requirements.txt @@ -0,0 +1,3 @@ +asyncpg +fastapi +uvicorn diff --git a/fastapi/start.sh b/fastapi/start.sh new file mode 100644 index 0000000..cfb3d7e --- /dev/null +++ b/fastapi/start.sh @@ -0,0 +1,2 @@ +docker-compose build +docker-compose up -d \ No newline at end of file diff --git a/fastapi/stop.sh b/fastapi/stop.sh new file mode 100644 index 0000000..03e638c --- /dev/null +++ b/fastapi/stop.sh @@ -0,0 +1,2 @@ +docker-compose stop +docker-compose down \ No newline at end of file