Skip to content

Commit bfd3e52

Browse files
authored
Add docker live reloading to Cleezy (#21)
1 parent 25853bc commit bfd3e52

File tree

9 files changed

+44
-11
lines changed

9 files changed

+44
-11
lines changed

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
FROM python:3.9-slim-buster
22

3-
WORKDIR /src
3+
WORKDIR /app
44

55
RUN apt-get update && apt-get install -y default-libmysqlclient-dev
66

77
COPY requirements.txt .
88

99
RUN pip install -r requirements.txt
1010

11-
COPY . .
12-
1311
EXPOSE 8000
1412

1513
ENTRYPOINT ["python", "server.py"]

docker-compose.dev.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: '3.7'
2+
services:
3+
app:
4+
container_name: cleezy-app-dev
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
command:
9+
- --database-file-path=/tmp/urldatabase.db
10+
- --port=8000
11+
- -vvv
12+
ports:
13+
- 8000:8000
14+
volumes:
15+
- cleezy_data_dev:/tmp/
16+
- ./server.py:/app/server.py
17+
- ./modules:/app/modules
18+
environment:
19+
- WATCHFILES_FORCE_POLLING=true
20+
21+
volumes:
22+
cleezy_data_dev:

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ services:
1010
- -vvv
1111
volumes:
1212
- cleezy_data:/tmp/
13+
- ./server.py:/app/server.py
14+
- ./modules:/app/modules
1315
prometheus:
1416
image: prom/prometheus:latest
1517
restart: always
File renamed without changes.
File renamed without changes.
File renamed without changes.

server.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import prometheus_client
77
import uvicorn
88

9-
from args import get_args
10-
from generate_alias import generate_alias
11-
import sqlite_helpers
12-
from constants import HttpResponse, http_code_to_enum
13-
from metrics import MetricsHandler
9+
from modules.args import get_args
10+
from modules.generate_alias import generate_alias
11+
import modules.sqlite_helpers as sqlite_helpers
12+
from modules.constants import HttpResponse, http_code_to_enum
13+
from modules.metrics import MetricsHandler
14+
1415

1516
app = FastAPI()
1617
args = get_args()
@@ -105,8 +106,18 @@ def get_metrics():
105106
level= logging.ERROR - (args.verbose*10),
106107
)
107108

108-
if __name__ == "__main__":
109-
logging.info(f"running on {args.host}, listening on port {args.port}")
109+
# we have a separate __name__ check here due to how FastAPI starts
110+
# a server. the file is first ran (where __name__ == "__main__")
111+
# and then calls `uvicorn.run`. the call to run() reruns the file,
112+
# this time __name__ == "server". the separate __name__ if statement
113+
# is so the thread references the same instance as the global
114+
# metrics_handler referenced by the rest of the file. otherwise,
115+
# the thread interacts with an instance different than the one the
116+
# server uses
117+
if __name__ == "server":
110118
initial_url_count = sqlite_helpers.get_number_of_entries(DATABASE_FILE)
111119
MetricsHandler.url_count.inc(initial_url_count)
112-
uvicorn.run(app, host=args.host, port=args.port)
120+
121+
if __name__ == "__main__":
122+
logging.info(f"running on {args.host}, listening on port {args.port}")
123+
uvicorn.run("server:app", host=args.host, port=args.port, reload=True)

0 commit comments

Comments
 (0)