mirror of
https://github.com/iritmaximus/timetrials.git
synced 2026-10-12 19:13:48 +03:00
changed database url to better place
This commit is contained in:
@@ -7,6 +7,23 @@ load_dotenv()
|
||||
app = Flask(__name__)
|
||||
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.home
|
||||
|
||||
@@ -3,26 +3,9 @@ from flaskr import app
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
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)
|
||||
|
||||
|
||||
def create_db():
|
||||
print("Initializing the db...")
|
||||
db.session.execute(open("sql/schema.sql", "r").read())
|
||||
|
||||
Reference in New Issue
Block a user