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__) app = Flask(__name__)
# Ensure instance directory exists app.config["SECRET_KEY"] = os.environ["FLASK_SECRET_KEY"]
# Ensure instance and database directories exist from urllib.parse import quote_plus
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
# Ensure database directory exists AFTER config is set # Get database credentials from environment
with app.app_context(): db_password = os.environ['DB_PASSWORD']
db_path = app.config["SQLALCHEMY_DATABASE_URI"].replace("sqlite:///", "") # URL-encode all special characters including @ and $
try: encoded_password = quote_plus(db_password, safe='')
os.makedirs(os.path.dirname(db_path), exist_ok=True, mode=0o755) app.logger.info(f"Using database password: {'*' * len(db_password)}")
print(f"Ensured database directory exists at {os.path.dirname(db_path)}") app.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql://flaskuser:{db_password}@db:5432/flaskdb"
except Exception as e:
print(f"Error creating database directory: {str(e)}")
raise
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app) db = SQLAlchemy(app)
@ -32,11 +25,9 @@ db = SQLAlchemy(app)
def init_db(): def init_db():
import os 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(): with app.app_context():
db.create_all() db.create_all()
print(f"Database created at {db_path}") print("Database initialized")
login_manager = LoginManager(app) login_manager = LoginManager(app)
@ -46,7 +37,7 @@ login_manager.login_view = "login"
class User(UserMixin, db.Model): class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=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 @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: 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: web:
build: . build: .
depends_on:
db:
condition: service_healthy
ports: ports:
- "5001:5001" - "5001:5001"
environment: environment:
- FLASK_ENV=production - FLASK_ENV=production
- FLASK_SECRET_KEY=${FLASK_SECRET_KEY} - FLASK_SECRET_KEY=${FLASK_SECRET_KEY}
- SQLALCHEMY_DATABASE_URI=sqlite:////app/instance/database.db - DB_PASSWORD=${DB_PASSWORD}
volumes: volumes:
- ./instance:/app/instance - ./instance:/app/instance
command: > command: sh -c "sleep 2 && flask init-db && gunicorn --bind 0.0.0.0:5001 --workers 4 app:app"
sh -c "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