From 7853ec0f0a8a685704714c98da46124e060ce26e Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 20 Dec 2020 14:03:53 -0500 Subject: [PATCH] Added GA --- main.py | 20 +++++++++++++------- requirements.txt | 1 + {static => templates}/index.html | 27 +++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 11 deletions(-) rename {static => templates}/index.html (94%) diff --git a/main.py b/main.py index 4b0daa3..07fd86d 100644 --- a/main.py +++ b/main.py @@ -4,14 +4,20 @@ from flask import Flask, render_template, jsonify, request, abort, send_from_dir from app.language import languages from flask_swagger import swagger from flask_swagger_ui import get_swaggerui_blueprint +from flask_limiter import Limiter parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API') -parser.add_argument('host', type=str, - help='Hostname', default="127.0.0.1") -parser.add_argument('port', type=int, - help='Port', default=5000) -parser.add_argument('--char-limit', default=-1, - help='Character limit') +parser.add_argument('--host', type=str, + help='Hostname (%(default)s)', default="127.0.0.1") +parser.add_argument('--port', type=int, + help='Port (%(default)s)', default=5000) +parser.add_argument('--char-limit', default=-1, metavar="", + help='Set character limit (%(default)s)') +parser.add_argument('--req-limit', default=-1, metavar="", + help='Set maximum number of requests per hour per client (%(default)s)') +parser.add_argument('--google-analytics', default=None, metavar="", + help='Enable Google Analytics on the API client page by providing an ID (%(default)s)') + args = parser.parse_args() boot() @@ -29,7 +35,7 @@ def server_error(e): @app.route("/") def index(): - return send_from_directory('static', 'index.html') + return render_template('index.html', gaId=args.google_analytics) @app.route("/languages") def langs(): diff --git a/requirements.txt b/requirements.txt index 1448d30..02f6d04 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ argostranslate==1.0.3 Flask==1.1.2 flask-swagger==0.2.14 flask-swagger-ui==3.36.0 +Flask-Limiter==1.4 diff --git a/static/index.html b/templates/index.html similarity index 94% rename from static/index.html rename to templates/index.html index 6aea9ae..b4acffc 100644 --- a/static/index.html +++ b/templates/index.html @@ -4,6 +4,9 @@ LibreTranslate - Free and Open Source Translation API + + + @@ -31,6 +34,18 @@ overflow: auto; } + + {% if gaId %} + + + + {% endif %}