From 5248ae0123226045870084d917ece6b22f7794fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gdula?= Date: Thu, 4 May 2023 17:14:41 +0300 Subject: [PATCH] Switch to Postgres database --- Highscore-Server/Dockerfile | 8 +++++--- Highscore-Server/highscore/config.py | 17 +++++++++++++--- Highscore-Server/highscore/run.sh | 19 ++++++++++++++++++ Highscore-Server/highscore/server.py | 11 ++-------- Highscore-Server/requirements.txt | 1 + docker-compose.yml | 30 ++++++++++++++++++++++++---- 6 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 Highscore-Server/highscore/run.sh diff --git a/Highscore-Server/Dockerfile b/Highscore-Server/Dockerfile index 558ec76..2ffcbaf 100644 --- a/Highscore-Server/Dockerfile +++ b/Highscore-Server/Dockerfile @@ -2,13 +2,15 @@ FROM python:3.10-alpine EXPOSE 8080 -RUN apk add --no-cache gcc musl-dev linux-headers +# RUN apk add --no-cache gcc musl-dev linux-headers +RUN apk add --no-cache postgresql-client WORKDIR /data COPY requirements.txt requirements.txt RUN pip install -r requirements.txt RUN mkdir /storage -COPY /highscore/ . +COPY ./highscore . +RUN chmod +x ./run.sh -CMD ["gunicorn", "--bind", "highscore:8080", "server:app"] +CMD ["./run.sh"] diff --git a/Highscore-Server/highscore/config.py b/Highscore-Server/highscore/config.py index 57cd158..58b0e25 100644 --- a/Highscore-Server/highscore/config.py +++ b/Highscore-Server/highscore/config.py @@ -1,10 +1,21 @@ import os -SECRET_KEY = os.getenv('SECRET_KEY') -BEARER_TOKEN = os.getenv('BEARER_TOKEN') +# Purely to make the code a bit more readable +def env(key): + return os.getenv(key) -SQLALCHEMY_DATABASE_URI = 'sqlite:///db.sqlite' +SECRET_KEY = env('SECRET_KEY') +BEARER_TOKEN = env('BEARER_TOKEN') + +user = env('DB_USER') +password = env('DB_PASSWORD') +host = env('DB_HOST') +port = env('DB_PORT') +database = env('DB_NAME') + +SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}" SQLALCHEMY_TRACK_MODIFICATIONS = False +SQLALCHEMY_POOL_RECYCLE = 621 MIGRATION_DIR = '/data/storage/migrations' INSTANCE_DIR = '/data/storage/instance' \ No newline at end of file diff --git a/Highscore-Server/highscore/run.sh b/Highscore-Server/highscore/run.sh new file mode 100644 index 0000000..bf936ad --- /dev/null +++ b/Highscore-Server/highscore/run.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +until pg_isready -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER +do + echo "Waiting for database to start... (5s)" + sleep 5 +done + +echo "Database is ready! Creating tables..." +flask --app server db init + +if [ -n flask --app server db check ]; then + echo "Database changes detected! Migrating..." + flask --app server db migrate + flask --app server db upgrade +fi + +echo "Starting server..." +gunicorn --bind highscore:8080 server:app diff --git a/Highscore-Server/highscore/server.py b/Highscore-Server/highscore/server.py index be0fe64..cc649bc 100644 --- a/Highscore-Server/highscore/server.py +++ b/Highscore-Server/highscore/server.py @@ -1,7 +1,5 @@ import os - from flask import Flask -from flask_migrate import init as migrate_init from extensions import db, migrate, cache from config import MIGRATION_DIR, INSTANCE_DIR from views import blueprint @@ -13,12 +11,7 @@ db.init_app(app) migrate.init_app(app, db, directory=MIGRATION_DIR) cache.init_app(app) -if not os.path.exists(os.path.join(INSTANCE_DIR, 'db.sqlite')): - with app.app_context(): - db.create_all() - -if not os.path.exists(MIGRATION_DIR): - with app.app_context(): - migrate_init(directory=MIGRATION_DIR) +with app.app_context(): + db.create_all() app.register_blueprint(blueprint) diff --git a/Highscore-Server/requirements.txt b/Highscore-Server/requirements.txt index 1af1c0d..ad1f54a 100644 --- a/Highscore-Server/requirements.txt +++ b/Highscore-Server/requirements.txt @@ -1,5 +1,6 @@ Flask Flask-SQLAlchemy +psycopg2-binary Flask-Migrate Flask-Caching Flask-wtf diff --git a/docker-compose.yml b/docker-compose.yml index da570df..f34d878 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,8 +11,21 @@ services: - ./Caddy/data:/data - ./Caddy/config:/config environment: - - ACME_AGREE=true - - DOMAIN=expo.leggy.dev + DOMAIN: expo.leggy.dev + links: + - highscore + + db: + image: postgres:alpine + ports: + - 5432:5432 + volumes: + - ./Postgress/data:/var/lib/postgresql/data + environment: + POSTGRES_USER: root + POSTGRES_PASSWORD: secret + POSTGRES_DB: database + POSTGRES_PORT: 5432 links: - highscore @@ -20,6 +33,15 @@ services: build: ./Highscore-Server volumes: - ./Highscore-Server/data:/data/storage + - ./Highscore-Server/logs:/data/logs + # Pass in the code to the container so I don't + # have to rebuild it every time during development + - ./Highscore-Server/highscore:/data/highscore environment: - - FLASK_KEY=secret - - BEARER_TOKEN=1234 + FLASK_KEY: secret + BEARER_TOKEN: 1234 + DB_USER: root + DB_PASSWORD: secret + DB_HOST: db + DB_PORT: 5432 + DB_NAME: database