-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (50 loc) · 1.69 KB
/
app.py
File metadata and controls
62 lines (50 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import logging
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from sqlalchemy.orm import DeclarativeBase
# Set up logging
if os.environ.get('RAILWAY_ENVIRONMENT') == 'production':
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig(level=logging.DEBUG)
# Set up database base class
class Base(DeclarativeBase):
pass
# Initialize extensions
db = SQLAlchemy(model_class=Base)
login_manager = LoginManager()
# Create the app
app = Flask(__name__)
# Configure secret key
app.secret_key = os.environ.get("SECRET_KEY") or os.environ.get("SESSION_SECRET") or "change_me_in_production"
# Handle potential Railway PostgreSQL database URL format
database_url = os.environ.get("DATABASE_URL")
if database_url and database_url.startswith("postgres://"):
database_url = database_url.replace("postgres://", "postgresql://", 1)
# Configure the database
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_recycle": 300,
"pool_pre_ping": True,
}
app.config["UPLOAD_FOLDER"] = "static/uploads"
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024 # 16MB max upload
# Initialize extensions with app
db.init_app(app)
login_manager.init_app(app)
login_manager.login_view = 'login'
# Create upload directory if it doesn't exist
with app.app_context():
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
# Import models and create tables
import models
db.create_all()
# Import routes
from routes import *
# Set up login manager
@login_manager.user_loader
def load_user(user_id):
from models import User
return User.query.get(int(user_id))