Initial commit: BalanceBoard - Reddit-style content aggregator
- Flask-based web application with PostgreSQL - User authentication and session management - Content moderation and filtering - Docker deployment with docker-compose - Admin interface for content management 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
53
database.py
Normal file
53
database.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
Database Configuration
|
||||
SQLAlchemy setup for PostgreSQL connection.
|
||||
"""
|
||||
|
||||
import os
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
# Initialize SQLAlchemy instance
|
||||
db = SQLAlchemy()
|
||||
|
||||
|
||||
def init_db(app):
|
||||
"""
|
||||
Initialize database with Flask app.
|
||||
|
||||
Args:
|
||||
app: Flask application instance
|
||||
"""
|
||||
# Get database URL from environment variable
|
||||
database_url = os.getenv('DATABASE_URL')
|
||||
|
||||
if not database_url:
|
||||
# Fallback to individual environment variables
|
||||
db_user = os.getenv('POSTGRES_USER', 'balanceboard')
|
||||
db_password = os.getenv('POSTGRES_PASSWORD', 'changeme')
|
||||
db_host = os.getenv('POSTGRES_HOST', 'localhost')
|
||||
db_port = os.getenv('POSTGRES_PORT', '5432')
|
||||
db_name = os.getenv('POSTGRES_DB', 'balanceboard')
|
||||
|
||||
database_url = f'postgresql+psycopg2://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}'
|
||||
|
||||
# Configure Flask app
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = database_url
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
||||
'pool_size': 10,
|
||||
'pool_recycle': 3600,
|
||||
'pool_pre_ping': True, # Verify connections before using
|
||||
}
|
||||
|
||||
# Initialize db with app
|
||||
db.init_app(app)
|
||||
|
||||
# Create tables
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
print("✓ Database tables created")
|
||||
|
||||
|
||||
def get_db():
|
||||
"""Get database instance"""
|
||||
return db
|
||||
Reference in New Issue
Block a user