From 094b650be51228fe9cb1ada2f1d11150a01f9ca9 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 20 Dec 2020 15:05:22 -0500 Subject: [PATCH] Waitress support --- app/app.py | 221 ++++++++++++++++++++++++ {templates => app/templates}/index.html | 17 +- main.py | 204 ++-------------------- requirements.txt | 1 + 4 files changed, 247 insertions(+), 196 deletions(-) create mode 100644 app/app.py rename {templates => app/templates}/index.html (97%) diff --git a/app/app.py b/app/app.py new file mode 100644 index 0000000..18c88d6 --- /dev/null +++ b/app/app.py @@ -0,0 +1,221 @@ +from app.init import boot +from flask import Flask, render_template, jsonify, request, abort, send_from_directory +from app.language import languages +from flask_swagger import swagger +from flask_swagger_ui import get_swaggerui_blueprint +from flask_limiter.util import get_remote_address + + +def create_app(char_limit=-1, req_limit=-1, google_analytics=None, debug=False): + boot() + app = Flask(__name__) + + if debug: + app.config['TEMPLATES_AUTO_RELOAD'] = True + + if req_limit > 0: + from flask_limiter import Limiter + limiter = Limiter( + app, + key_func=get_remote_address, + default_limits=["%s per minute" % req_limit] + ) + + @app.errorhandler(400) + def invalid_api(e): + return jsonify({"error": str(e.description)}), 400 + + @app.errorhandler(500) + def server_error(e): + return jsonify({"error": str(e.description)}), 500 + + @app.errorhandler(429) + def slow_down_error(e): + return jsonify({"error": "Slowdown: " + str(e.description)}), 429 + + @app.route("/") + def index(): + return render_template('index.html', gaId=google_analytics) + + @app.route("/languages") + def langs(): + """ + Retrieve list of supported languages + --- + tags: + - translate + responses: + 200: + description: List of languages + content: + application/json: + schema: + type: array + items: + type: object + properties: + code: + type: string + description: Language code + name: + type: string + description: Human-readable language name (in English) + charLimit: + type: string + description: Character input limit for this language (-1 indicates no limit) + 429: + description: Slow down + content: + application/json: + schema: + type: object + properties: + error: + type: string + description: Reason for slow down + """ + return jsonify([{'code': l.code, 'name': l.name, 'charLimit': char_limit } for l in languages]) + + # Add cors + @app.after_request + def after_request(response): + response.headers.add('Access-Control-Allow-Origin','*') + response.headers.add('Access-Control-Allow-Headers', "Authorization, Content-Type") + response.headers.add('Access-Control-Expose-Headers', "Authorization") + response.headers.add('Access-Control-Allow-Methods', "GET, POST") + response.headers.add('Access-Control-Allow-Credentials', "true") + response.headers.add('Access-Control-Max-Age', 60 * 60 * 24 * 20) + return response + + + @app.route("/translate", methods=['POST']) + def translate(): + """ + Translate text from a language to another + --- + tags: + - translate + parameters: + - in: formData + name: q + schema: + type: string + example: Hello world! + required: true + description: Text to translate + - in: formData + name: source + schema: + type: string + example: en + required: true + description: Source language code + - in: formData + name: target + schema: + type: string + example: es + required: true + description: Target language code + responses: + 200: + description: Translated text + content: + application/json: + schema: + type: object + properties: + translatedText: + type: string + description: Translated text + 400: + description: Invalid request + content: + application/json: + schema: + type: object + properties: + error: + type: string + description: Error message + 500: + description: Translation error + content: + application/json: + schema: + type: object + properties: + error: + type: string + description: Error message + 429: + description: Slow down + content: + application/json: + schema: + type: object + properties: + error: + type: string + description: Reason for slow down + """ + + if request.is_json: + json = request.get_json() + q = json.get('q') + source_lang = json.get('source') + target_lang = json.get('target') + else: + q = request.values.get("q") + source_lang = request.values.get("source") + target_lang = request.values.get("target") + + if not q: + abort(400, description="Invalid request: missing q parameter") + if not source_lang: + abort(400, description="Invalid request: missing source parameter") + if not target_lang: + abort(400, description="Invalid request: missing target parameter") + + if char_limit != -1: + q = q[:char_limit] + + src_lang = next(iter([l for l in languages if l.code == source_lang]), None) + tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) + + if src_lang is None: + abort(400, description="%s is not supported" % source_lang) + if tgt_lang is None: + abort(400, description="%s is not supported" % target_lang) + + translator = src_lang.get_translation(tgt_lang) + try: + return jsonify({"translatedText": translator.translate(q) }) + except Exception as e: + abort(500, description="Cannot translate text: %s" % str(e)) + + + swag = swagger(app) + swag['info']['version'] = "1.0" + swag['info']['title'] = "LibreTranslate" + + @app.route("/spec") + def spec(): + return jsonify(swag) + + SWAGGER_URL = '/docs' # URL for exposing Swagger UI (without trailing '/') + API_URL = 'http://petstore.swagger.io/v2/swagger.json' # Our API url (can of course be a local resource) + + # Call factory function to create our blueprint + swaggerui_blueprint = get_swaggerui_blueprint( + SWAGGER_URL, + "", + config={ # Swagger UI config overrides + 'app_name': "LibreTranslate", + "spec": swag + } + ) + + app.register_blueprint(swaggerui_blueprint) + + return app \ No newline at end of file diff --git a/templates/index.html b/app/templates/index.html similarity index 97% rename from templates/index.html rename to app/templates/index.html index b4acffc..cc50423 100644 --- a/templates/index.html +++ b/app/templates/index.html @@ -371,17 +371,19 @@ document.addEventListener('DOMContentLoaded', function(){ request.open('POST', BaseUrl + '/translate', true); request.onload = function() { - if (this.status >= 200 && this.status < 400) { + try{ + var res = JSON.parse(this.response); // Success! - var res = JSON.parse(this.response); if (res.translatedText !== undefined){ self.translatedText = res.translatedText; self.loadingTranslation = false; + self.output = JSON.stringify(res, null, 4); + }else{ + throw new Error(res.error || "Unknown error"); } - self.output = JSON.stringify(res, null, 4); - } else { - self.error = "Cannot call /translate"; - self.loadingTranslation = false; + }catch(e){ + self.error = e.message; + self.loadingTranslation = false; } }; @@ -399,8 +401,5 @@ document.addEventListener('DOMContentLoaded', function(){ }); -{% if gaId %} - -{% endif %} \ No newline at end of file diff --git a/main.py b/main.py index 07fd86d..b524dbc 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,5 @@ -from app.init import boot import argparse -from flask import Flask, render_template, jsonify, request, abort, send_from_directory -from app.language import languages -from flask_swagger import swagger -from flask_swagger_ui import get_swaggerui_blueprint -from flask_limiter import Limiter +from app.app import create_app parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API') parser.add_argument('--host', type=str, @@ -13,190 +8,25 @@ 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="", +parser.add_argument('--req-limit', default=-1, type=int, metavar="", + help='Set maximum number of requests per minute per client (%(default)s)') +parser.add_argument('--google-analytics', type=str, default=None, metavar="", help='Enable Google Analytics on the API client page by providing an ID (%(default)s)') +parser.add_argument('--debug', default=False, action="store_true", + help="Enable debug environment") +parser.add_argument('--ssl', default=None, action="store_true", + help="Whether to enable SSL") args = parser.parse_args() -boot() -app = Flask(__name__) -app.config['TEMPLATES_AUTO_RELOAD'] = True - - -@app.errorhandler(400) -def invalid_api(e): - return jsonify({"error": str(e.description)}), 400 - -@app.errorhandler(500) -def server_error(e): - return jsonify({"error": str(e.description)}), 500 - -@app.route("/") -def index(): - return render_template('index.html', gaId=args.google_analytics) - -@app.route("/languages") -def langs(): - """ - Retrieve list of supported languages - --- - tags: - - translate - responses: - 200: - description: List of languages - content: - application/json: - schema: - type: array - items: - type: object - properties: - code: - type: string - description: Language code - name: - type: string - description: Human-readable language name (in English) - charLimit: - type: string - description: Character input limit for this language (-1 indicates no limit) - """ - return jsonify([{'code': l.code, 'name': l.name, 'charLimit': args.char_limit } for l in languages]) - -# Add cors -@app.after_request -def after_request(response): - response.headers.add('Access-Control-Allow-Origin','*') - response.headers.add('Access-Control-Allow-Headers', "Authorization, Content-Type") - response.headers.add('Access-Control-Expose-Headers', "Authorization") - response.headers.add('Access-Control-Allow-Methods', "GET, POST") - response.headers.add('Access-Control-Allow-Credentials', "true") - response.headers.add('Access-Control-Max-Age', 60 * 60 * 24 * 20) - return response - - -@app.route("/translate", methods=['POST']) -def translate(): - """ - Translate text from a language to another - --- - tags: - - translate - parameters: - - in: formData - name: q - schema: - type: string - example: Hello world! - required: true - description: Text to translate - - in: formData - name: source - schema: - type: string - example: en - required: true - description: Source language code - - in: formData - name: target - schema: - type: string - example: es - required: true - description: Target language code - responses: - 200: - description: Translated text - content: - application/json: - schema: - type: object - properties: - translatedText: - type: string - description: Translated text - 400: - description: Invalid request - content: - application/json: - schema: - type: object - properties: - error: - type: string - description: Error message - 500: - description: Translation error - content: - application/json: - schema: - type: object - properties: - error: - type: string - description: Error message - """ - - if request.is_json: - json = request.get_json() - q = json.get('q') - source_lang = json.get('source') - target_lang = json.get('target') - else: - q = request.values.get("q") - source_lang = request.values.get("source") - target_lang = request.values.get("target") - - if not q: - abort(400, description="Invalid request: missing q parameter") - if not source_lang: - abort(400, description="Invalid request: missing source parameter") - if not target_lang: - abort(400, description="Invalid request: missing target parameter") - - if args.char_limit != -1: - q = q[:args.char_limit] - - src_lang = next(iter([l for l in languages if l.code == source_lang]), None) - tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) - - if src_lang is None: - abort(400, description="%s is not supported" % source_lang) - if tgt_lang is None: - abort(400, description="%s is not supported" % target_lang) - - translator = src_lang.get_translation(tgt_lang) - try: - return jsonify({"translatedText": translator.translate(q) }) - except Exception as e: - abort(500, description="Cannot translate text: %s" % str(e)) - - -swag = swagger(app) -swag['info']['version'] = "1.0" -swag['info']['title'] = "LibreTranslate" - -@app.route("/spec") -def spec(): - return jsonify(swag) - -SWAGGER_URL = '/docs' # URL for exposing Swagger UI (without trailing '/') -API_URL = 'http://petstore.swagger.io/v2/swagger.json' # Our API url (can of course be a local resource) - -# Call factory function to create our blueprint -swaggerui_blueprint = get_swaggerui_blueprint( - SWAGGER_URL, - "", - config={ # Swagger UI config overrides - 'app_name': "LibreTranslate", - "spec": swag - } -) - -app.register_blueprint(swaggerui_blueprint) if __name__ == "__main__": - app.run(host=args.host) + app = create_app(char_limit=args.char_limit, + req_limit=args.req_limit, + google_analytics=args.google_analytics, + debug=args.debug) + if args.debug: + app.run(host=args.host, port=args.port) + else: + from waitress import serve + serve(app, host=args.host, port=args.port, url_scheme='https' if args.ssl else 'http') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 02f6d04..500c95e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ Flask==1.1.2 flask-swagger==0.2.14 flask-swagger-ui==3.36.0 Flask-Limiter==1.4 +waitress==1.4.4