2022-10-03 16:46:46 +02:00
|
|
|
import multiprocessing as mp
|
2022-08-03 13:16:50 +02:00
|
|
|
import time
|
2023-05-09 10:04:27 +02:00
|
|
|
from pathlib import Path
|
2022-08-03 13:16:50 +02:00
|
|
|
|
2022-07-27 16:45:17 +02:00
|
|
|
import click
|
2022-07-08 10:24:54 +02:00
|
|
|
import uvicorn
|
2023-08-16 12:22:14 +02:00
|
|
|
from uvicorn.supervisors import ChangeReload
|
2022-07-08 10:24:54 +02:00
|
|
|
|
2022-10-03 23:41:04 +02:00
|
|
|
from lnbits.settings import set_cli_settings, settings
|
2022-08-03 13:16:50 +02:00
|
|
|
|
2022-07-08 10:24:54 +02:00
|
|
|
|
2022-08-03 13:16:50 +02:00
|
|
|
@click.command(
|
2024-04-03 17:56:05 +02:00
|
|
|
context_settings={
|
|
|
|
"ignore_unknown_options": True,
|
|
|
|
"allow_extra_args": True,
|
|
|
|
}
|
2022-08-03 13:16:50 +02:00
|
|
|
)
|
2022-10-03 16:46:46 +02:00
|
|
|
@click.option("--port", default=settings.port, help="Port to listen on")
|
2024-06-19 08:27:26 +02:00
|
|
|
@click.option("--host", default=settings.host, help="Host to run LNbits on")
|
2022-11-24 14:53:11 +01:00
|
|
|
@click.option(
|
|
|
|
"--forwarded-allow-ips",
|
|
|
|
default=settings.forwarded_allow_ips,
|
|
|
|
help="Allowed proxy servers",
|
|
|
|
)
|
2022-08-03 13:16:50 +02:00
|
|
|
@click.option("--ssl-keyfile", default=None, help="Path to SSL keyfile")
|
|
|
|
@click.option("--ssl-certfile", default=None, help="Path to SSL certificate")
|
2024-04-19 13:23:56 +02:00
|
|
|
@click.option(
|
|
|
|
"--reload", is_flag=True, default=False, help="Enable auto-reload for development"
|
|
|
|
)
|
2022-11-19 21:41:37 +01:00
|
|
|
def main(
|
|
|
|
port: int,
|
|
|
|
host: str,
|
|
|
|
forwarded_allow_ips: str,
|
|
|
|
ssl_keyfile: str,
|
|
|
|
ssl_certfile: str,
|
2024-04-19 13:23:56 +02:00
|
|
|
reload: bool,
|
2022-11-19 21:41:37 +01:00
|
|
|
):
|
2022-07-08 10:24:54 +02:00
|
|
|
"""Launched with `poetry run lnbits` at root level"""
|
2022-10-03 23:33:42 +02:00
|
|
|
|
2023-05-09 10:04:27 +02:00
|
|
|
# create data dir if it does not exist
|
|
|
|
Path(settings.lnbits_data_folder).mkdir(parents=True, exist_ok=True)
|
2023-10-12 11:24:05 +02:00
|
|
|
Path(settings.lnbits_data_folder, "logs").mkdir(parents=True, exist_ok=True)
|
2023-05-09 10:04:27 +02:00
|
|
|
|
2023-09-25 12:44:29 +02:00
|
|
|
# create `extensions`` dir if it does not exist
|
|
|
|
Path(settings.lnbits_extensions_path, "extensions").mkdir(
|
|
|
|
parents=True, exist_ok=True
|
|
|
|
)
|
2023-07-18 13:46:50 +02:00
|
|
|
|
2022-11-24 14:35:32 +01:00
|
|
|
set_cli_settings(host=host, port=port, forwarded_allow_ips=forwarded_allow_ips)
|
2022-10-03 23:33:42 +02:00
|
|
|
|
2022-10-03 16:46:46 +02:00
|
|
|
while True:
|
|
|
|
config = uvicorn.Config(
|
|
|
|
"lnbits.__main__:app",
|
2022-10-21 10:00:47 +02:00
|
|
|
loop="uvloop",
|
2022-10-03 16:46:46 +02:00
|
|
|
port=port,
|
|
|
|
host=host,
|
2022-11-24 14:35:32 +01:00
|
|
|
forwarded_allow_ips=forwarded_allow_ips,
|
2022-10-03 16:46:46 +02:00
|
|
|
ssl_keyfile=ssl_keyfile,
|
|
|
|
ssl_certfile=ssl_certfile,
|
2024-04-19 13:23:56 +02:00
|
|
|
reload=reload or False,
|
2022-10-03 16:46:46 +02:00
|
|
|
)
|
2022-10-03 23:33:42 +02:00
|
|
|
|
2022-10-03 16:46:46 +02:00
|
|
|
server = uvicorn.Server(config=config)
|
2023-08-03 09:53:36 +02:00
|
|
|
|
|
|
|
if config.should_reload:
|
|
|
|
sock = config.bind_socket()
|
|
|
|
run = ChangeReload(config, target=server.run, sockets=[sock]).run
|
|
|
|
else:
|
|
|
|
run = server.run
|
|
|
|
|
|
|
|
process = mp.Process(target=run)
|
2022-10-03 16:46:46 +02:00
|
|
|
process.start()
|
|
|
|
server_restart.wait()
|
|
|
|
server_restart.clear()
|
|
|
|
server.should_exit = True
|
|
|
|
server.force_exit = True
|
2022-10-21 10:00:47 +02:00
|
|
|
time.sleep(3)
|
2022-10-03 16:46:46 +02:00
|
|
|
process.terminate()
|
|
|
|
process.join()
|
2022-10-21 10:00:47 +02:00
|
|
|
time.sleep(1)
|
2022-10-03 16:46:46 +02:00
|
|
|
|
2022-07-08 10:24:54 +02:00
|
|
|
|
2022-10-03 16:46:46 +02:00
|
|
|
server_restart = mp.Event()
|
2022-07-27 16:45:17 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-07-08 10:24:54 +02:00
|
|
|
main()
|