~fluix/tilde

ref: 36aec7f1fc9ff0d1cca5e29bda1698fd1dcd2fc9 tilde/app.py -rw-r--r-- 1.3 KiB
36aec7f1Steven Guikal Switch to pyfile config files 9 months ago
                                                                                
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
# SPDX-FileCopyrightText: 2021 Steven Guikal <void@fluix.one>
#
# SPDX-License-Identifier: AGPL-3.0-only

import logging
import core
import redis
from flask import Flask, render_template
from flask_login import LoginManager
from auth.views import auth
from auth.models import User
from flatpages.views import flatpages

logger = logging.getLogger(__name__)

app = Flask(__name__, subdomain_matching=True)
app.config.from_pyfile("config.defaults")
for location in ["config", "/etc/tilde/config"]:
    try:
        app.config.from_pyfile(location)
    except FileNotFoundError:
        logger.warning(f"Could not find configuration file at '{location}'.")
    else:
        logger.info(f"Loaded configuration file at '{location}'.")
        break
if not app.config["SECRET_KEY"]:
    logger.fatal("SECRET_KEY must defined in some configuration file.")
    exit(1)

core.redis = redis.Redis(**app.config.get("REDIS"))

app.register_blueprint(flatpages)
app.register_blueprint(auth, subdomain="auth")

login_manager = LoginManager()
login_manager.login_view = "auth.login"
login_manager.login_message = "Please log in to access that page."
login_manager.init_app(app)


@login_manager.user_loader
def load_user(username):
    """Check is_active property to log out users that are no longer active."""
    user = User.get(username)
    return user if user.is_active else None