From ca8044697d0b64cb82fdbb942ef364250f0f971f Mon Sep 17 00:00:00 2001 From: Mikel Bober-Irizar Date: Fri, 27 Jan 2023 18:41:59 +0000 Subject: [PATCH 1/3] Add development docker w/ postgres --- README.md | 2 +- app/Dockerfile | 8 +++++++ admin.py => app/admin.py | 0 app.py => app/app.py | 9 +++++++- elections.py => app/elections.py | 0 requirements.txt => app/requirements.txt | 0 {static => app/static}/style.css | 0 {templates => app/templates}/about.html | 0 {templates => app/templates}/audit.html | 0 .../templates}/create_election.html | 0 .../templates}/create_society.html | 0 {templates => app/templates}/election.html | 0 .../templates}/includes/header.html | 0 {templates => app/templates}/index.html | 0 {templates => app/templates}/results.html | 0 {templates => app/templates}/vote.html | 0 dev.yaml | 21 +++++++++++++++++++ 17 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 app/Dockerfile rename admin.py => app/admin.py (100%) rename app.py => app/app.py (51%) rename elections.py => app/elections.py (100%) rename requirements.txt => app/requirements.txt (100%) rename {static => app/static}/style.css (100%) rename {templates => app/templates}/about.html (100%) rename {templates => app/templates}/audit.html (100%) rename {templates => app/templates}/create_election.html (100%) rename {templates => app/templates}/create_society.html (100%) rename {templates => app/templates}/election.html (100%) rename {templates => app/templates}/includes/header.html (100%) rename {templates => app/templates}/index.html (100%) rename {templates => app/templates}/results.html (100%) rename {templates => app/templates}/vote.html (100%) create mode 100644 dev.yaml diff --git a/README.md b/README.md index 0d90a6c..6003e44 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ Just A Cool Online Ballot-Box ## Requirements -- Python 3.11 +- Python 3.11 \ No newline at end of file diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100644 index 0000000..dd3813f --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.11 + +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt +COPY . . + +CMD ["python", "app.py"] \ No newline at end of file diff --git a/admin.py b/app/admin.py similarity index 100% rename from admin.py rename to app/admin.py diff --git a/app.py b/app/app.py similarity index 51% rename from app.py rename to app/app.py index 3e006a2..14c0a3b 100644 --- a/app.py +++ b/app/app.py @@ -15,4 +15,11 @@ def about(): import admin if __name__ == '__main__': - app.run() + import os + + if os.environ.get('PRODUCTION'): + raise Exception("Can't run interactively in PRODUCTION") + + app.run(host=os.environ.get('FLASK_HOST', '127.0.0.1'), + port=os.environ.get('FLASK_PORT', 5000), + debug=os.environ.get('FLASK_DEBUG', True)) diff --git a/elections.py b/app/elections.py similarity index 100% rename from elections.py rename to app/elections.py diff --git a/requirements.txt b/app/requirements.txt similarity index 100% rename from requirements.txt rename to app/requirements.txt diff --git a/static/style.css b/app/static/style.css similarity index 100% rename from static/style.css rename to app/static/style.css diff --git a/templates/about.html b/app/templates/about.html similarity index 100% rename from templates/about.html rename to app/templates/about.html diff --git a/templates/audit.html b/app/templates/audit.html similarity index 100% rename from templates/audit.html rename to app/templates/audit.html diff --git a/templates/create_election.html b/app/templates/create_election.html similarity index 100% rename from templates/create_election.html rename to app/templates/create_election.html diff --git a/templates/create_society.html b/app/templates/create_society.html similarity index 100% rename from templates/create_society.html rename to app/templates/create_society.html diff --git a/templates/election.html b/app/templates/election.html similarity index 100% rename from templates/election.html rename to app/templates/election.html diff --git a/templates/includes/header.html b/app/templates/includes/header.html similarity index 100% rename from templates/includes/header.html rename to app/templates/includes/header.html diff --git a/templates/index.html b/app/templates/index.html similarity index 100% rename from templates/index.html rename to app/templates/index.html diff --git a/templates/results.html b/app/templates/results.html similarity index 100% rename from templates/results.html rename to app/templates/results.html diff --git a/templates/vote.html b/app/templates/vote.html similarity index 100% rename from templates/vote.html rename to app/templates/vote.html diff --git a/dev.yaml b/dev.yaml new file mode 100644 index 0000000..08c1d4b --- /dev/null +++ b/dev.yaml @@ -0,0 +1,21 @@ +version: '3.8' + +services: + app: + build: ./app/ + ports: + - 127.0.0.1:5000:5000 + depends_on: + - db + environment: + - FLASK_HOST=0.0.0.0 + + db: + image: postgres + volumes: + - dev_db_data:/var/lib/postgresql/data + environment: + - POSTGRES_PASSWORD=dev + +volumes: + dev_db_data: From d4eb1439469cfbaa14b7fcbadc648e72dd0a642e Mon Sep 17 00:00:00 2001 From: Mikel Bober-Irizar Date: Fri, 27 Jan 2023 18:46:00 +0000 Subject: [PATCH 2/3] Add live-reload on dev container --- dev.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dev.yaml b/dev.yaml index 08c1d4b..d380181 100644 --- a/dev.yaml +++ b/dev.yaml @@ -5,6 +5,10 @@ services: build: ./app/ ports: - 127.0.0.1:5000:5000 + volumes: + - type: bind # For development, we pass through the existing files for ez reload + source: app/ + target: /app/ depends_on: - db environment: From 5b1d2c60628fbd8016c3bb3e825b5487238cffc8 Mon Sep 17 00:00:00 2001 From: Mikel Bober-Irizar Date: Fri, 27 Jan 2023 18:56:47 +0000 Subject: [PATCH 3/3] Add adminer panel --- README.md | 16 +++++++++++++++- dev.yaml | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6003e44..461f79b 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,18 @@ Just A Cool Online Ballot-Box ## Requirements -- Python 3.11 \ No newline at end of file +- Python 3.11 + +## Docker for development + +You can use docker and avoid any setup. Just run: + +``` +docker-compose -f dev.yaml build +docker-compose -f dev.yaml up +``` + +which will spin up the Flask application (http://localhost:5000), a Postgres instance, and an Adminer instance to poke at the database (http://localhost:5001). +Any changes to the `app/` folder will be immediately reflected inside the Flask container. +To wipe the database, delete the `jacob_dev_db_data` volume. +By default, webservers are only visible to localhost. \ No newline at end of file diff --git a/dev.yaml b/dev.yaml index d380181..7c5d837 100644 --- a/dev.yaml +++ b/dev.yaml @@ -1,6 +1,7 @@ version: '3.8' services: + # Flask app app: build: ./app/ ports: @@ -14,12 +15,22 @@ services: environment: - FLASK_HOST=0.0.0.0 + # Postgres DB db: image: postgres volumes: - dev_db_data:/var/lib/postgresql/data environment: - POSTGRES_PASSWORD=dev + expose: + - 5432 # visible to other containers only + + # Admin panel for Postgres DB - DEVELOPMENT ONLY + # Use login credentials postgres/dev + adminer: + image: adminer + ports: + - 127.0.0.1:5001:8080 volumes: dev_db_data: