51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_login import LoginManager
|
|
import os
|
|
|
|
db = SQLAlchemy()
|
|
login_manager = LoginManager()
|
|
|
|
def create_app():
|
|
# Frontend path
|
|
base_dir = os.path.abspath(os.path.dirname(__file__))
|
|
template_dir = os.path.join(base_dir, '..', 'frontend', 'templates')
|
|
static_dir = os.path.join(base_dir, '..', 'frontend', 'static')
|
|
|
|
# DB path
|
|
instance_path = os.path.join(base_dir, '..', 'instance')
|
|
os.makedirs(instance_path, exist_ok=True)
|
|
|
|
app = Flask(
|
|
__name__,
|
|
template_folder=template_dir,
|
|
static_folder=static_dir,
|
|
instance_path=os.path.abspath(instance_path)
|
|
)
|
|
|
|
app.config['SECRET_KEY'] = 'IddAhxI%$G%2X3joI04i'
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{os.path.join(app.instance_path, 'users.db')}"
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
# Init extensions
|
|
db.init_app(app)
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = "login"
|
|
|
|
# Import models for user_loader
|
|
from backend.models import User
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return User.query.get(int(user_id))
|
|
|
|
# Import routes and give app
|
|
from backend import routes
|
|
routes.init_app(app)
|
|
|
|
# Create DB if needed
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
return app
|