Restoring main.py as method to start WROLPi API because it is required to upgrade docker database.

This commit is contained in:
Roland 2024-05-02 10:56:43 -06:00
parent 2924867f55
commit f0e4302de8
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
4 changed files with 56 additions and 4 deletions

View file

@ -17,4 +17,5 @@ COPY main.py /opt/wrolpi/
COPY wrolpi /opt/wrolpi/wrolpi
COPY modules /opt/wrolpi/modules
CMD ["sanic", "main:api_app", "--host", "0.0.0.0", "--port", "8081"]
ENTRYPOINT [ "python3", "-OO", "/opt/wrolpi/main.py"]
CMD ["-vv", "api", "--host", "0.0.0.0"]

View file

@ -87,6 +87,10 @@ def main():
sub_commands = parser.add_subparsers(title='sub-commands', dest='sub_commands')
# Add the API parser, this will allow the user to specify host/port etc.
api_parser = sub_commands.add_parser('api')
root_api.init_parser(api_parser)
# DB Parser for running Alembic migrations
db_parser = sub_commands.add_parser('db')
db_parser.add_argument('command', help='Supported commands: upgrade, downgrade')
@ -133,6 +137,10 @@ def main():
if args.sub_commands == 'db':
return db_main(args)
# Run the API.
if args.sub_commands == 'api':
return root_api.main(args)
return 1

View file

@ -21,7 +21,7 @@ python-dotenv==1.0.0
python-magic~=0.4.27
pytz==2023.3
sanic-ext==23.6.0
sanic-testing==23.6.0
sanic-testing==22.6.0 # 23.6.0 has error with "expandtabs"
sanic==23.6.0 # This is the latest version of Sanic that avoids this bug: https://github.com/sanic-org/sanic/issues/2921
selenium==4.9.1
srt==3.5.3

View file

@ -4,7 +4,7 @@ import re
from http import HTTPStatus
import vininfo.exceptions
from sanic import response, Blueprint
from sanic import response, Blueprint, __version__ as sanic_version
from sanic.request import Request
from sanic_ext import validate
from sanic_ext.extensions.openapi import openapi
@ -31,7 +31,8 @@ from wrolpi.errors import WROLModeEnabled, HotspotError, InvalidDownload, \
from wrolpi.events import get_events, Events
from wrolpi.files import files_bp
from wrolpi.files.lib import get_file_statistics, search_file_suggestion_count
from wrolpi.vars import DOCKERIZED, IS_RPI, IS_RPI4, IS_RPI5
from wrolpi.vars import DOCKERIZED, IS_RPI, IS_RPI4, IS_RPI5, API_HOST, API_PORT, API_WORKERS, API_DEBUG, \
API_ACCESS_LOG, truthy_arg, API_AUTO_RELOAD
from wrolpi.version import __version__
logger = logger.getChild(__name__)
@ -50,6 +51,48 @@ api_app.blueprint(otp_bp)
api_app.blueprint(videos_bp)
api_app.blueprint(zim_bp)
def run_webserver(
host: str = API_HOST,
port: int = API_PORT,
workers: int = API_WORKERS,
api_debug: bool = API_DEBUG,
access_log: bool = API_ACCESS_LOG,
):
# Attach all blueprints after they have been defined.
kwargs = dict(
host=host,
port=port,
workers=workers,
debug=api_debug,
access_log=access_log,
auto_reload=DOCKERIZED,
)
logger.warning(f'Running Sanic {sanic_version} with kwargs {kwargs}')
return api_app.run(**kwargs)
def init_parser(parser):
# Called by WROLPI's main() function
parser.add_argument('-H', '--host', default=API_HOST, help='What network interface to connect webserver')
parser.add_argument('-p', '--port', default=API_PORT, type=int, help='What port to connect webserver')
parser.add_argument('-w', '--workers', default=API_WORKERS, type=int, help='How many web workers to run')
parser.add_argument('--access-log', default=API_ACCESS_LOG, type=truthy_arg, help='Enable Sanic access log')
parser.add_argument('--api-debug', default=API_DEBUG, type=truthy_arg, help='Enable Sanic debug log')
parser.add_argument('--api-auto-reload', default=API_AUTO_RELOAD, type=truthy_arg, help='Enable Sanic auto reload')
def main(args):
return run_webserver(
host=args.host,
port=args.port,
workers=args.workers,
api_debug=args.api_debug,
access_log=args.access_log,
)
index_html = '''
<html>
<body>