feedleware/feedleware/__init__.py

44 lines
1.1 KiB
Python
Raw Permalink Normal View History

2021-09-12 13:42:57 +00:00
import configparser
import logging
import sys
from os import environ
from flask import Flask
2023-05-13 16:35:40 +00:00
from . import twitch, youtube, openalex
2021-09-12 13:42:57 +00:00
2021-09-12 13:42:57 +00:00
logger = logging.getLogger(__name__)
blueprints = {
"twitch": twitch,
2021-09-12 22:00:24 +00:00
"youtube": youtube,
2023-05-13 16:35:40 +00:00
"openalex": openalex,
}
2021-09-12 13:42:57 +00:00
def create_app():
"""Read the app configuration and instantiate service blueprints."""
debug = environ.get("FLASK_ENV", "production") == "development"
level = logging.DEBUG if debug else logging.INFO
logging.basicConfig(level=level)
2021-09-12 13:42:57 +00:00
config_path = environ.get("FEEDLEWARE_CONFIG")
2021-09-12 13:42:57 +00:00
if not config_path:
print(
"Please set the FEEDLEWARE_CONFIG environment variable",
file=sys.stderr,
)
sys.exit(1)
2021-09-12 13:42:57 +00:00
config = configparser.ConfigParser()
config.read(config_path)
app = Flask(__name__)
for section in config.sections():
if section in blueprints:
blueprint = blueprints[section].create_blueprint(config[section])
app.register_blueprint(blueprint, url_prefix="/" + section)
else:
logger.warning("Unknown service '%s'", section)
2021-09-12 13:42:57 +00:00
return app