changed db to postgres with password stored in env

This commit is contained in:
Andreas Jönsson 2025-11-16 17:22:25 +01:00
parent 2844b4de4b
commit 86a07ad8bd
1278 changed files with 1097 additions and 23 deletions

29
app.py
View File

@ -6,22 +6,15 @@ import os
app = Flask(__name__)
# Ensure instance directory exists
# Ensure instance and database directories exist
app.config["SECRET_KEY"] = os.environ.get(
"FLASK_SECRET_KEY", "your-secret-key-here" # Fallback for development only
)
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_DATABASE_URI", "sqlite:///instance/database.db") # Read from environment
app.config["SECRET_KEY"] = os.environ["FLASK_SECRET_KEY"]
from urllib.parse import quote_plus
# Ensure database directory exists AFTER config is set
with app.app_context():
db_path = app.config["SQLALCHEMY_DATABASE_URI"].replace("sqlite:///", "")
try:
os.makedirs(os.path.dirname(db_path), exist_ok=True, mode=0o755)
print(f"Ensured database directory exists at {os.path.dirname(db_path)}")
except Exception as e:
print(f"Error creating database directory: {str(e)}")
raise
# Get database credentials from environment
db_password = os.environ['DB_PASSWORD']
# URL-encode all special characters including @ and $
encoded_password = quote_plus(db_password, safe='')
app.logger.info(f"Using database password: {'*' * len(db_password)}")
app.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql://flaskuser:{db_password}@db:5432/flaskdb"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
@ -32,11 +25,9 @@ db = SQLAlchemy(app)
def init_db():
import os
db_path = app.config["SQLALCHEMY_DATABASE_URI"].replace("sqlite:///", "")
os.makedirs(os.path.dirname(db_path), exist_ok=True, mode=0o755)
with app.app_context():
db.create_all()
print(f"Database created at {db_path}")
print("Database initialized")
login_manager = LoginManager(app)
@ -46,7 +37,7 @@ login_manager.login_view = "login"
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
password = db.Column(db.String(200)) # Increased for password hash
@login_manager.user_loader

1
db_password.secret Normal file
View File

@ -0,0 +1 @@
pg_5UP3r$3cr3t_P@ssw0rd!

View File

@ -1,15 +1,43 @@
version: '3.8'
version: "3"
services:
db:
image: "postgres:latest"
volumes:
- ./postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: flaskuser
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: flaskdb
healthcheck:
test: ["CMD-SHELL", "pg_isready -U flaskuser -d flaskdb"]
interval: 5s
timeout: 5s
retries: 5
start_period: 10s
ports:
- "5432:5432"
web:
build: .
depends_on:
db:
condition: service_healthy
ports:
- "5001:5001"
environment:
- FLASK_ENV=production
- FLASK_SECRET_KEY=${FLASK_SECRET_KEY}
- SQLALCHEMY_DATABASE_URI=sqlite:////app/instance/database.db
- DB_PASSWORD=${DB_PASSWORD}
volumes:
- ./instance:/app/instance
command: >
sh -c "flask init-db && gunicorn --bind 0.0.0.0:5001 --workers 4 app:app"
command: sh -c "sleep 2 && flask init-db && gunicorn --bind 0.0.0.0:5001 --workers 4 app:app"
volumes:
postgres_data:

1
postgres_data/PG_VERSION Normal file
View File

@ -0,0 +1 @@
17

BIN
postgres_data/base/1/112 Normal file

Binary file not shown.

BIN
postgres_data/base/1/113 Normal file

Binary file not shown.

BIN
postgres_data/base/1/1247 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/1249 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/1255 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/1259 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/13402 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
postgres_data/base/1/13406 Normal file

Binary file not shown.

BIN
postgres_data/base/1/13407 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
postgres_data/base/1/13411 Normal file

Binary file not shown.

BIN
postgres_data/base/1/13412 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
postgres_data/base/1/13416 Normal file

Binary file not shown.

BIN
postgres_data/base/1/13417 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
postgres_data/base/1/13421 Normal file

Binary file not shown.

View File

View File

BIN
postgres_data/base/1/174 Normal file

Binary file not shown.

BIN
postgres_data/base/1/175 Normal file

Binary file not shown.

BIN
postgres_data/base/1/2187 Normal file

Binary file not shown.

View File

BIN
postgres_data/base/1/2228 Normal file

Binary file not shown.

View File

View File

BIN
postgres_data/base/1/2337 Normal file

Binary file not shown.

BIN
postgres_data/base/1/2579 Normal file

Binary file not shown.

BIN
postgres_data/base/1/2600 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2601 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2602 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2603 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
postgres_data/base/1/2605 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2606 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2607 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2608 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2609 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2610 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
postgres_data/base/1/2612 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
postgres_data/base/1/2615 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2616 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2617 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2618 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
postgres_data/base/1/2619 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More