2021-02-09 02:24:34 +02:00
|
|
|
import argparse
|
2021-07-07 02:26:06 +02:00
|
|
|
import sys
|
2021-03-28 16:19:19 +02:00
|
|
|
import operator
|
2021-05-18 05:51:33 +02:00
|
|
|
|
2021-02-09 02:24:34 +02:00
|
|
|
from app.app import create_app
|
2021-07-12 22:43:06 +02:00
|
|
|
from app.default_values import DEFAULT_ARGUMENTS as DEFARGS
|
2021-02-09 02:24:34 +02:00
|
|
|
|
2021-05-18 05:41:02 +02:00
|
|
|
|
2021-02-09 02:24:34 +02:00
|
|
|
def main():
|
2021-05-18 05:41:02 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="LibreTranslate - Free and Open Source Translation API"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-07-12 22:43:06 +02:00
|
|
|
"--host", type=str, help="Hostname (%(default)s)", default=DEFARGS['HOST']
|
2021-05-18 05:41:02 +02:00
|
|
|
)
|
2021-07-12 22:43:06 +02:00
|
|
|
parser.add_argument("--port", type=int, help="Port (%(default)s)", default=DEFARGS['PORT'])
|
2021-05-18 05:41:02 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--char-limit",
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['CHAR_LIMIT'],
|
2021-05-18 05:41:02 +02:00
|
|
|
type=int,
|
|
|
|
metavar="<number of characters>",
|
|
|
|
help="Set character limit (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--req-limit",
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['REQ_LIMIT'],
|
2021-05-18 05:41:02 +02:00
|
|
|
type=int,
|
|
|
|
metavar="<number>",
|
|
|
|
help="Set the default maximum number of requests per minute per client (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--daily-req-limit",
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['DAILY_REQ_LIMI'],
|
2021-05-18 05:41:02 +02:00
|
|
|
type=int,
|
|
|
|
metavar="<number>",
|
|
|
|
help="Set the default maximum number of requests per day per client, in addition to req-limit. (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--req-flood-threshold",
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['REQ_FLOOD_THRESHOLD'],
|
2021-05-18 05:41:02 +02:00
|
|
|
type=int,
|
|
|
|
metavar="<number>",
|
|
|
|
help="Set the maximum number of request limit offences per 4 weeks that a client can exceed before being banned. (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--batch-limit",
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['BATCH_LIMIT'],
|
2021-05-18 05:41:02 +02:00
|
|
|
type=int,
|
|
|
|
metavar="<number of texts>",
|
|
|
|
help="Set maximum number of texts to translate in a batch request (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--ga-id",
|
|
|
|
type=str,
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['GA_ID'],
|
2021-05-18 05:41:02 +02:00
|
|
|
metavar="<GA ID>",
|
|
|
|
help="Enable Google Analytics on the API client page by providing an ID (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-07-12 22:43:06 +02:00
|
|
|
"--debug", default=DEFARGS['DEBUG'], action="store_true", help="Enable debug environment"
|
2021-05-18 05:41:02 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-07-12 22:43:06 +02:00
|
|
|
"--ssl", default=DEFARGS['SSL'], action="store_true", help="Whether to enable SSL"
|
2021-05-18 05:41:02 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--frontend-language-source",
|
|
|
|
type=str,
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['FRONTEND_LANGUAGE_SOURCE'],
|
2021-05-18 05:41:02 +02:00
|
|
|
metavar="<language code>",
|
|
|
|
help="Set frontend default language - source (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--frontend-language-target",
|
|
|
|
type=str,
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['FRONTEND_LANGUAGE_TARGET'],
|
2021-05-18 05:41:02 +02:00
|
|
|
metavar="<language code>",
|
|
|
|
help="Set frontend default language - target (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--frontend-timeout",
|
|
|
|
type=int,
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['FRONTEND_TIMEOUT'],
|
2021-05-18 05:41:02 +02:00
|
|
|
metavar="<milliseconds>",
|
|
|
|
help="Set frontend translation timeout (%(default)s)",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--api-keys",
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['API_KEYS'],
|
2021-05-18 05:41:02 +02:00
|
|
|
action="store_true",
|
|
|
|
help="Enable API keys database for per-user rate limits lookup",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--require-api-key-origin",
|
|
|
|
type=str,
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['REQUIRE_API_KEY_ORIGIN'],
|
2021-05-18 05:41:02 +02:00
|
|
|
help="Require use of an API key for programmatic access to the API, unless the request origin matches this domain",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--load-only",
|
|
|
|
type=operator.methodcaller("split", ","),
|
2021-07-12 22:43:06 +02:00
|
|
|
default=DEFARGS['LOAD_ONLY'],
|
2021-05-18 05:41:02 +02:00
|
|
|
metavar="<comma-separated language codes>",
|
|
|
|
help="Set available languages (ar,de,en,es,fr,ga,hi,it,ja,ko,pt,ru,zh)",
|
|
|
|
)
|
2021-02-09 02:24:34 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
2021-02-15 20:30:28 +02:00
|
|
|
app = create_app(args)
|
2021-02-09 02:24:34 +02:00
|
|
|
|
2021-07-07 02:26:06 +02:00
|
|
|
if sys.argv[0] == '--wsgi':
|
|
|
|
return app
|
2021-02-09 02:24:34 +02:00
|
|
|
else:
|
2021-07-07 02:26:06 +02:00
|
|
|
if args.debug:
|
|
|
|
app.run(host=args.host, port=args.port)
|
|
|
|
else:
|
|
|
|
from waitress import serve
|
2021-05-18 05:41:02 +02:00
|
|
|
|
2021-07-07 02:26:06 +02:00
|
|
|
serve(
|
|
|
|
app,
|
|
|
|
host=args.host,
|
|
|
|
port=args.port,
|
|
|
|
url_scheme="https" if args.ssl else "http",
|
|
|
|
)
|
2021-05-18 05:41:02 +02:00
|
|
|
|
2021-02-09 02:24:34 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|