photonglass/app/__init__.py

41 lines
1.1 KiB
Python
Raw Normal View History

import logging, yaml, os
2024-12-22 21:33:36 +11:00
from flask import Flask
2024-12-26 13:01:44 +11:00
from flask_limiter import Limiter
from app.functions.utils import get_client_ip
2024-12-22 21:33:36 +11:00
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
2024-12-26 13:01:44 +11:00
limiter = Limiter(
get_client_ip,
default_limits=["100 per day", "10 per minute"],
storage_uri="memory://"
)
2024-12-22 21:33:36 +11:00
def create_app():
app = Flask(__name__, instance_path="/instance")
2024-12-26 13:01:44 +11:00
limiter.init_app(app)
config_files = ['config.yaml', 'site.yaml', 'devices.yaml', 'commands.yaml']
for config_file in config_files:
config_path = os.path.join("/instance", config_file)
# Create empty config files if they don't exist
if not os.path.exists(config_path):
with open(config_path, "w") as f:
pass
# Load the config files into the app config
with open(config_path, 'r') as file:
config_yaml = yaml.safe_load(file) or {}
app.config[config_file.split('.')[0].upper()] = config_yaml
2024-12-26 13:01:44 +11:00
2024-12-22 21:33:36 +11:00
from app.views import main
app.register_blueprint(main.bp)
app.add_url_rule('/', endpoint='index')
return app