-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move API to separate Python script, run as part of PM2
- Loading branch information
Showing
10 changed files
with
141 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import argparse | ||
|
||
from flask import ( | ||
Flask, | ||
) | ||
|
||
import modules.logs as logging | ||
from modules.errors import determine_exit_code | ||
from consts import ( | ||
DEFAULT_LOG_DIR, | ||
DEFAULT_DATABASE_PATH, | ||
CONSOLE_LOG_LEVEL, | ||
FILE_LOG_LEVEL, | ||
FLASK_ADDRESS, | ||
FLASK_PORT, | ||
FLASK_DATABASE_PATH, | ||
) | ||
from api.routes.index import index | ||
from api.routes.webhooks.tautulli.index import webhooks_tautulli | ||
|
||
APP_NAME = "API" | ||
|
||
# Parse CLI arguments | ||
parser = argparse.ArgumentParser(description="Tauticord API - API for Tauticord") | ||
""" | ||
Bot will use config, in order: | ||
1. Explicit config file path provided as CLI argument, if included, or | ||
2. Default config file path, if exists, or | ||
3. Environmental variables | ||
""" | ||
parser.add_argument("-l", "--log", help="Log file directory", default=DEFAULT_LOG_DIR) | ||
parser.add_argument("-d", "--database", help="Path to database file", default=DEFAULT_DATABASE_PATH) | ||
args = parser.parse_args() | ||
|
||
|
||
def run_with_potential_exit_on_error(func): | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception as e: | ||
logging.fatal(f"Fatal error occurred. Shutting down: {e}") | ||
exit_code = determine_exit_code(exception=e) | ||
logging.fatal(f"Exiting with code {exit_code}") | ||
exit(exit_code) | ||
|
||
return wrapper | ||
|
||
|
||
@run_with_potential_exit_on_error | ||
def set_up_logging(): | ||
logging.init(app_name=APP_NAME, | ||
console_log_level=CONSOLE_LOG_LEVEL, | ||
log_to_file=True, | ||
log_file_dir=args.log, | ||
file_log_level=FILE_LOG_LEVEL) | ||
|
||
|
||
# Register Flask blueprints | ||
application = Flask(APP_NAME) | ||
application.config[FLASK_DATABASE_PATH] = args.database | ||
|
||
application.register_blueprint(index) | ||
application.register_blueprint(webhooks_tautulli) | ||
|
||
if __name__ == "__main__": | ||
set_up_logging() | ||
|
||
application.run(host=FLASK_ADDRESS, port=FLASK_PORT, debug=False, use_reloader=False) |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from flask import ( | ||
Blueprint, | ||
request, | ||
Response as FlaskResponse, | ||
) | ||
|
||
from consts import ( | ||
FLASK_POST, | ||
FLASK_GET, | ||
) | ||
|
||
index = Blueprint("index", __name__, url_prefix="") | ||
|
||
|
||
@index.route("/ping", methods=[FLASK_GET]) | ||
def ping(): | ||
return 'Pong!', 200 | ||
|
||
|
||
@index.route("/hello", methods=[FLASK_GET]) | ||
def hello_world(): | ||
return 'Hello, World!', 200 | ||
|
||
|
||
@index.route("/health", methods=[FLASK_GET]) | ||
def health_check(): | ||
return 'OK', 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from flask import ( | ||
Blueprint, | ||
request as flask_request, | ||
Response as FlaskResponse, | ||
current_app, | ||
) | ||
|
||
from api.controllers.webhook_processor import WebhookProcessor | ||
from consts import ( | ||
FLASK_POST, | ||
FLASK_GET, | ||
FLASK_DATABASE_PATH, | ||
) | ||
|
||
webhooks_tautulli = Blueprint("tautulli", __name__, url_prefix="/webhooks/tautulli") | ||
|
||
|
||
@webhooks_tautulli.route("/recently_added", methods=[FLASK_POST]) | ||
def tautulli_webhook(): | ||
database_path = current_app.config[FLASK_DATABASE_PATH] | ||
return WebhookProcessor.process_tautulli_recently_added_webhook(request=flask_request, | ||
database_path=database_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters