From 99ffc8f8e7dd445685695d89f7630ed02a8ee8e5 Mon Sep 17 00:00:00 2001 From: Eduard Kaverinskyi <115407897+EduKav1813@users.noreply.github.com> Date: Sat, 13 Jul 2024 16:24:29 +0200 Subject: [PATCH] Fix postgres compose refactor (#74) * Add SQLite3 driver for peewee as a dependency * Add volumes for both sqlite and postgres * Implement creating DB form APP_DB_URL * Add default sqlite database url --- docker-compose.yml | 19 +++++++------------ env.sh | 12 ++++-------- pyproject.toml | 1 + whois/database.py | 19 +++---------------- 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a903f73..10182db 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,23 +6,18 @@ services: environment: # you should change secret key - SECRET_KEY=example123 + - APP_DB_URL="postgresql://whohacks:S3cret@localhost:5432/whohacks" + - OAUTH_OPENID=http://oauth.localhost:8080/issuer1/.well-known/openid-configuration + - OAUTH_CLIENT_SECRET=12345678 - APP_IP_MASK=172.18.0.1 - - APP_DB_DIALECT=postgresql - - APP_DB_NAME=whohacks - - APP_DB_USER=whohacks - - APP_DB_PASSWORD=S3cret - - APP_DB_HOST=postgres - - APP_DB_PORT=5432 - OAUTH_CLIENT_ID=fake-development-client-id - - OAUTH_CLIENT_SECRET=12345678 - - APP_OAUTH_OPENID=http://oauth.localhost:8080/issuer1/.well-known/openid-configuration ports: # use 127.0.0.1:8000:8000 - "8000:8000" # network_mode: "host" volumes: - - sqlite:/data + - ./data:/sqlite - /etc/localtime:/etc/localtime:ro depends_on: - postgres @@ -35,7 +30,7 @@ services: ports: - 5432:5432 volumes: - - postgres_data:/var/lib/postgresql/data + - database_postgres:/var/lib/postgresql/data environment: - POSTGRES_PASSWORD=S3cret - POSTGRES_USER=whohacks @@ -63,5 +58,5 @@ services: - default volumes: - sqlite: - postgres_data: + database_postgres: + driver: local diff --git a/env.sh b/env.sh index 2691edc..ece44a9 100644 --- a/env.sh +++ b/env.sh @@ -1,8 +1,4 @@ -export APP_DB_DIALECT="postgresql" -export APP_DB_NAME="whohacks" -export APP_DB_USER="whohacks" -export APP_DB_PASSWORD="S3cret" -export APP_DB_HOST="localhost" -export APP_DB_PORT="5432" -export APP_OAUTH_OPENID="http://sso.hsp.sh/auth/realms/hsp/.well-known/openid-configuration" -env | grep APP_ > .env \ No newline at end of file +export APP_DB_URL="postgresql://whohacks:S3cret@localhost:5432/whohacks" +export OAUTH_OPENID="http://sso.hsp.sh/auth/realms/hsp/.well-known/openid-configuration" +env | grep APP_ > .env +env | grep OUATH_ > .env \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 670f50f..9c01f67 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ urllib3 = "1.26.18" werkzeug = "1.0.1" pytz = "^2024.1" psycopg2-binary = "^2.9.9" +pysqlite3 = "^0.5.3" [tool.poetry.dev-dependencies] diff --git a/whois/database.py b/whois/database.py index cab5a2a..856976a 100644 --- a/whois/database.py +++ b/whois/database.py @@ -3,24 +3,11 @@ import peewee as pw from werkzeug.security import check_password_hash, generate_password_hash +from playhouse.db_url import connect -db_dialect = os.environ.get("APP_DB_DIALECT", "sqlite") - -if db_dialect == "sqlite": - db_path = os.environ.get("APP_DB_PATH", "whoisdevices.db") - db = pw.SqliteDatabase(db_path) -elif db_dialect == "postgresql": - db_name = os.environ.get("APP_DB_NAME", "whohacks") - db_user = os.environ.get("APP_DB_USER", "whohacks") - db_password = os.environ.get("APP_DB_PASSWORD") - db_host = os.environ.get("APP_DB_HOST", "localhost") - db_port = os.environ.get("APP_DB_PORT", "5432") - db = pw.PostgresqlDatabase( - db_name, user=db_user, password=db_password, host=db_host, port=db_port - ) -else: - raise RuntimeError(f"Unknown db dialect '{db_dialect}' (envvar APP_DB_DIALECT)") +db_url = os.environ.get("APP_DB_URL", "sqlite:///whohacks.sqlite") +db = connect(db_url) class User(pw.Model):