Skip to content

Commit

Permalink
Fix postgres compose refactor (#74)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
EduKav1813 authored Jul 13, 2024
1 parent 94f3a87 commit 99ffc8f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 36 deletions.
19 changes: 7 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -63,5 +58,5 @@ services:
- default

volumes:
sqlite:
postgres_data:
database_postgres:
driver: local
12 changes: 4 additions & 8 deletions env.sh
Original file line number Diff line number Diff line change
@@ -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
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
19 changes: 3 additions & 16 deletions whois/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 99ffc8f

Please sign in to comment.