""" A simple Web application to serve dfr """ from flask import Flask, request, Response, send_file import msgpack # from flask_cors import CORS import dfr # configuration DEBUG = False # instantiate the app app = Flask(__name__, static_url_path='', static_folder='assets') app.config.from_object(__name__) # enable CORS # CORS(app, resources={r'/*': {'origins': '*'}}) @app.route('/', methods=['GET']) def index_client(): """ Send the single file """ return send_file("index.html", mimetype='text/html') def get_def_reg(w): """ Search a word, can deal with regex and casse problem. """ if res := dfr.get_def_sql_reg(w): return msgpack.packb(res) # Recherche du mot en minuscule elif res := dfr.get_def_sql_reg(w.lower()): return msgpack.packb(res) # Recherche du mot en nom propre elif res := dfr.get_def_sql_reg(w.title()): return msgpack.packb(res) else: return Response("", status=404) @app.route('/def', methods=['GET']) def get_def(): """ Retrieve a definition """ w = request.args.get('w') if '_' in w: return get_def_reg(w); # Recherche du mot tapé if res := dfr.get_def_sql(w): return msgpack.packb(res) # Recherche du mot en minuscule elif res := dfr.get_def_sql(w.lower()): return msgpack.packb(res) # Recherche du mot en nom propre elif res := dfr.get_def_sql(w.title()): return msgpack.packb(res) else: return Response("", status=404) return Response(msgpack.packb(request.args.get('w')), mimetype='application/msgpack') if __name__ == '__main__': app.run()