BrickTracker/app.py

55 lines
1.3 KiB
Python
Raw Normal View History

2025-01-17 11:03:00 +01:00
# This need to be first
import eventlet
2025-01-17 11:03:00 +01:00
eventlet.monkey_patch()
2024-12-30 09:28:15 +01:00
2025-01-17 11:03:00 +01:00
import logging # noqa: E402
2024-06-29 17:32:47 +02:00
2025-01-17 11:03:00 +01:00
from flask import Flask # noqa: E402
2024-04-16 21:36:34 +02:00
2025-01-17 11:03:00 +01:00
from bricktracker.app import setup_app # noqa: E402
from bricktracker.socket import BrickSocket # noqa: E402
2024-02-28 20:08:16 +01:00
2025-01-17 11:03:00 +01:00
logger = logging.getLogger(__name__)
2024-12-28 13:58:01 +01:00
2024-12-28 09:34:07 +01:00
# Create the app
# Using 'app' globally interferse with the teardown handlers of Flask
def create_app(main: bool = False, /) -> Flask | BrickSocket:
# Create the Flask app
app = Flask(__name__)
2024-12-30 09:28:15 +01:00
# Setup the app
setup_app(app)
# Create the socket
s = BrickSocket(
app,
threaded=not app.config['NO_THREADED_SOCKET'],
)
if main:
return s
else:
return app
2024-12-30 09:28:15 +01:00
2025-01-17 14:36:19 +01:00
if __name__ == '__main__':
s = create_app(True)
# This never happens, but makes the linter happy
if isinstance(s, Flask):
logger.critical('Cannot run locally with a Flask object, needs a BrickSocket. Use create_app(True) to return a BrickSocket') # noqa: E501
exit(1)
2025-01-17 11:03:00 +01:00
# Run the application
logger.info('Starting BrickTracker on {host}:{port}'.format(
host=s.app.config['HOST'],
port=s.app.config['PORT'],
2025-01-17 11:03:00 +01:00
))
s.socket.run(
s.app,
host=s.app.config['HOST'],
debug=s.app.config['DEBUG'],
port=s.app.config['PORT'],
2024-12-30 09:28:15 +01:00
)