changed database url to better place

This commit is contained in:
Aaro Koivukangas
2025-10-16 19:20:35 +03:00
parent 4e2415e8c1
commit 0aef3a64b7
2 changed files with 17 additions and 17 deletions
+17
View File
@@ -7,6 +7,23 @@ load_dotenv()
app = Flask(__name__) app = Flask(__name__)
app.config["SECRET_KEY"] = getenv("SECRET_KEY") app.config["SECRET_KEY"] = getenv("SECRET_KEY")
if getenv("POSTGRES_URL") and getenv("POSTGRES_URL").startswith("postgresql://"):
app.config["SQLALCHEMY_DATABASE_URI"] = getenv("POSTGRES_URL")
else:
postgres_host = getenv("POSTGRES_HOST")
postgres_db = getenv("POSTGRES_DB")
postgres_port = getenv("POSTGRES_PORT")
postgres_user = getenv("POSTGRES_USER")
postgres_password = getenv("POSTGRES_PASSWORD")
postgres_variables = [postgres_host, postgres_db, postgres_port, postgres_user, postgres_password]
for var in postgres_variables:
if not var:
raise ValueError("All environment variables not set")
database_url = f"postgresql://{postgres_user}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_db}"
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
# import flaskr.views # import flaskr.views
import flaskr.views.home import flaskr.views.home
-17
View File
@@ -3,26 +3,9 @@ from flaskr import app
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from random import randint from random import randint
if not getenv("POSTGRES_URL") or (getenv("POSTGRES_URL") and not getenv("POSTGRES_URL").startswith("postgresql://")):
postgres_host = getenv("POSTGRES_HOST")
postgres_db = getenv("POSTGRES_DB")
postgres_port = getenv("POSTGRES_PORT")
postgres_user = getenv("POSTGRES_USER")
postgres_password = getenv("POSTGRES_PASSWORD")
postgres_variables = [postgres_host, postgres_db, postgres_port, postgres_user, postgres_password]
for var in postgres_variables:
if not var:
raise ValueError("All environment variables not set")
database_url = f"postgresql://{postgres_user}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_db}"
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
else:
app.config["SQLALCHEMY_DATABASE_URI"] = getenv("POSTGRES_URL")
db = SQLAlchemy(app) db = SQLAlchemy(app)
def create_db(): def create_db():
print("Initializing the db...") print("Initializing the db...")
db.session.execute(open("sql/schema.sql", "r").read()) db.session.execute(open("sql/schema.sql", "r").read())