lnbits-legend/lnbits/server.py

77 lines
2.1 KiB
Python
Raw Normal View History

import asyncio
2022-10-03 22:14:07 +02:00
import uvloop
2022-10-03 22:14:07 +02:00
uvloop.install()
import contextlib
import multiprocessing as mp
import sys
import time
import click
import uvicorn
2022-10-03 23:41:04 +02:00
from lnbits.settings import set_cli_settings, settings
@click.command(
context_settings=dict(
ignore_unknown_options=True,
allow_extra_args=True,
)
)
@click.option("--port", default=settings.port, help="Port to listen on")
@click.option("--host", default=settings.host, help="Host to run LNBits on")
@click.option("--reload", is_flag=True, help="Reload LNBits on changes in code")
@click.option("--ssl-keyfile", default=None, help="Path to SSL keyfile")
@click.option("--ssl-certfile", default=None, help="Path to SSL certificate")
@click.pass_context
def main(ctx, port: int, host: str, ssl_keyfile: str, ssl_certfile: str, reload: bool):
"""Launched with `poetry run lnbits` at root level"""
2022-10-03 23:33:42 +02:00
set_cli_settings(host=host, port=port)
# this beautiful beast parses all command line arguments and passes them to the uvicorn server
d = dict()
for a in ctx.args:
item = a.split("=")
if len(item) > 1: # argument like --key=value
print(a, item)
d[item[0].strip("--").replace("-", "_")] = (
int(item[1]) # need to convert to int if it's a number
if item[1].isdigit()
else item[1]
)
else:
d[a.strip("--")] = True # argument like --key
while True:
config = uvicorn.Config(
"lnbits.__main__:app",
port=port,
host=host,
reload=reload,
2022-10-12 13:08:59 +02:00
forwarded_allow_ips="*",
ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile,
**d
)
2022-10-03 23:33:42 +02:00
server = uvicorn.Server(config=config)
process = mp.Process(target=server.run)
process.start()
server_restart.wait()
server_restart.clear()
server.should_exit = True
server.force_exit = True
process.terminate()
process.join()
time.sleep(3)
server_restart = mp.Event()
if __name__ == "__main__":
main()