2023-03-10 06:07:12 +02:00
|
|
|
import re
|
|
|
|
import sys
|
2022-12-26 23:10:43 +02:00
|
|
|
|
2023-07-09 12:29:11 +02:00
|
|
|
from prometheus_client import multiprocess
|
|
|
|
|
|
|
|
|
2022-12-26 23:10:43 +02:00
|
|
|
def child_exit(server, worker):
|
2023-03-10 06:07:12 +02:00
|
|
|
multiprocess.mark_process_dead(worker.pid)
|
|
|
|
|
|
|
|
def on_starting(server):
|
|
|
|
# Parse command line arguments
|
|
|
|
proc_name = server.cfg.default_proc_name
|
|
|
|
kwargs = {}
|
|
|
|
if proc_name.startswith("wsgi:app"):
|
2024-08-12 19:58:32 +02:00
|
|
|
str_args = re.sub(r'wsgi:app\s*\(\s*(.*)\s*\)', '\\1', proc_name).strip().split(",")
|
2023-03-10 06:07:12 +02:00
|
|
|
for a in str_args:
|
|
|
|
if "=" in a:
|
|
|
|
k,v = a.split("=")
|
|
|
|
k = k.strip()
|
|
|
|
v = v.strip()
|
|
|
|
|
|
|
|
if v.lower() in ["true", "false"]:
|
|
|
|
v = v.lower() == "true"
|
2024-08-12 19:58:32 +02:00
|
|
|
if not v:
|
|
|
|
continue
|
2023-03-10 06:07:12 +02:00
|
|
|
elif v[0] == '"':
|
|
|
|
v = v[1:-1]
|
|
|
|
kwargs[k] = v
|
|
|
|
|
|
|
|
from libretranslate.main import get_args
|
|
|
|
sys.argv = ['--wsgi']
|
2024-08-12 19:58:32 +02:00
|
|
|
|
2023-03-10 06:07:12 +02:00
|
|
|
for k in kwargs:
|
|
|
|
ck = k.replace("_", "-")
|
|
|
|
if isinstance(kwargs[k], bool) and kwargs[k]:
|
|
|
|
sys.argv.append("--" + ck)
|
|
|
|
else:
|
|
|
|
sys.argv.append("--" + ck)
|
|
|
|
sys.argv.append(kwargs[k])
|
|
|
|
|
|
|
|
args = get_args()
|
2023-07-23 13:24:15 +02:00
|
|
|
|
2023-07-09 12:29:11 +02:00
|
|
|
from libretranslate import flood, scheduler, secret, storage
|
2023-03-10 06:07:12 +02:00
|
|
|
storage.setup(args.shared_storage)
|
|
|
|
scheduler.setup(args)
|
|
|
|
flood.setup(args)
|
|
|
|
secret.setup(args)
|