58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import os
|
|
from datetime import timedelta
|
|
|
|
from flask import Flask
|
|
from flask_jwt_extended import JWTManager
|
|
from flask_cors import CORS
|
|
|
|
from app.services.auth_service import AuthService
|
|
from app.database import init_db
|
|
|
|
# Global auth service instance
|
|
auth_service = AuthService()
|
|
|
|
|
|
def create_app():
|
|
"""Create and configure the Flask application."""
|
|
app = Flask(__name__)
|
|
|
|
# Configure Flask secret key (required for sessions used by OAuth)
|
|
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "dev-secret-key")
|
|
|
|
# Configure SQLAlchemy database
|
|
database_url = os.environ.get("DATABASE_URL", "sqlite:///soundboard.db")
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
# Configure Flask-JWT-Extended
|
|
app.config["JWT_SECRET_KEY"] = os.environ.get("JWT_SECRET_KEY", "jwt-secret-key")
|
|
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(minutes=15)
|
|
app.config["JWT_REFRESH_TOKEN_EXPIRES"] = timedelta(days=7)
|
|
app.config["JWT_TOKEN_LOCATION"] = ["cookies"]
|
|
app.config["JWT_COOKIE_SECURE"] = False # Set to True in production
|
|
app.config["JWT_COOKIE_CSRF_PROTECT"] = False
|
|
app.config["JWT_ACCESS_COOKIE_PATH"] = "/api/"
|
|
app.config["JWT_REFRESH_COOKIE_PATH"] = "/api/auth/refresh"
|
|
|
|
# Initialize CORS
|
|
CORS(app,
|
|
origins=["http://localhost:3000"], # Frontend URL
|
|
supports_credentials=True, # Allow cookies
|
|
allow_headers=["Content-Type", "Authorization"],
|
|
methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
|
|
|
|
# Initialize JWT manager
|
|
jwt = JWTManager(app)
|
|
|
|
# Initialize database
|
|
init_db(app)
|
|
|
|
# Initialize authentication service with app
|
|
auth_service.init_app(app)
|
|
|
|
# Register blueprints
|
|
from app.routes import main, auth
|
|
app.register_blueprint(main.bp, url_prefix="/api")
|
|
app.register_blueprint(auth.bp, url_prefix="/api/auth")
|
|
|
|
return app |