from flask import Flask, request, jsonify, json, Response, render_template
from flask_cors import CORS
from text_sim import parse


app = Flask(__name__)
app.secret_key = "SECRET KEY"
app.config.from_object(__name__)
CORS(app)

# HTTP Errors handlers
@app.errorhandler(404)
def url_error(e):
    return "404 Error: Page Not Found"
@app.errorhandler(500)
def server_error(e):
    return "500 Error: Internal Server Error"


@app.route('/', methods = ['GET'])
def root():
    return render_template('base.html')

@app.route('/apple', methods = ['GET'])
def apple():
    with open('licenses/apple_fixed.txt', 'r') as f:
        fl = f.read()
    return fl

# API route
@app.route('/api', methods = ['POST'])
def api():
    info = request.get_json()
    val1 = info.get("text")
    #print("data: {}".format(val1))
    result = parse(val1)
    print("sending off: {}".format(result))
    return jsonify(data=result)

if __name__ == '__main__':
    app.run()
