From c72b34aa3e0979192aa7166e29c49bf592dea88c Mon Sep 17 00:00:00 2001 From: Ali Kaafarani Date: Tue, 30 Aug 2022 01:14:39 +0200 Subject: [PATCH] Persist data in production --- .gitignore | 2 +- data/.gitkeep | 0 docker-compose.production.yml | 1 + src/kvikshaug/app.py | 10 +++++----- 4 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 data/.gitkeep diff --git a/.gitignore b/.gitignore index f60513d..372e16c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,5 @@ /.env /assets/css/ /assets/script/ +/data/guestbook.json /logs/access.log -/guestbook.json diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/docker-compose.production.yml b/docker-compose.production.yml index 2bc310d..7b4c47d 100644 --- a/docker-compose.production.yml +++ b/docker-compose.production.yml @@ -17,6 +17,7 @@ services: build: . image: kvikshaug.no volumes: + - "./data:/app/data" - "./logs:/app/logs" environment: - ENVIRONMENT=production diff --git a/src/kvikshaug/app.py b/src/kvikshaug/app.py index 3e04e9d..459d933 100644 --- a/src/kvikshaug/app.py +++ b/src/kvikshaug/app.py @@ -17,14 +17,14 @@ if app.config["DEBUG"]: app.jinja_env.undefined = StrictUndefined # Initialize guestbook if not yet created. -if not os.path.exists("guestbook.json"): - with open("guestbook.json", "w") as file_: +if not os.path.exists("data/guestbook.json"): + with open("data/guestbook.json", "w") as file_: json.dump([], file_) @app.route("/") def home(): - with open("guestbook.json") as file_: + with open("data/guestbook.json") as file_: guestbook = json.load(file_) for message in guestbook: message["datetime"] = datetime.strptime(message["datetime"], "%Y-%m-%d %H:%M") @@ -37,9 +37,9 @@ def guestbook(): message = request.form.get("message", "").strip() if message == "": return redirect("/#guestbook") - with open("guestbook.json") as file_: + with open("data/guestbook.json") as file_: guestbook = json.load(file_) - with open("guestbook.json", "w") as file_: + with open("data/guestbook.json", "w") as file_: guestbook.append({ "message": message, "ip": request.remote_addr, -- 2.45.2