from flask import Flask
from flask import render_template
from flask import request
import subprocess
from subprocess import TimeoutExpired
import uuid
import os

app = Flask(__name__)


@app.route('/interpret', methods=['POST', 'GET'])
def interpret():
    if request.method == 'POST':
        dataRow = request.data.decode('utf-8')

        if dataRow == '' and request.form.get('json') is not None:
            dataRow = request.form['json']
        elif dataRow == '':
            return 'data error'

        fileName = "bin/"+str(uuid.uuid4())+".fun"
        out_file = open(fileName, "w")
        out_file.write(dataRow)
        out_file.close()

        return runCommand(['mono', 'bin/funwapi.exe', fileName], fileName)

    return 'run'


@app.route('/compile', methods=['POST', 'GET'])
def compile():
    if request.method == 'POST':
        dataRow = request.data.decode('utf-8')

        if dataRow == '' and request.form.get('json') is not None:
            dataRow = request.form['json']
        elif dataRow == '':
            return 'data error'

        fileName = "bin/"+str(uuid.uuid4())+".fun"
        out_file = open(fileName, "w")
        out_file.write(dataRow)
        out_file.close()

        fileNameOut = "bin/"+str(uuid.uuid4())+".fs"
        out = runCommand(['mono', 'bin/funwapc.exe', fileName, '-o', fileNameOut], fileName)

        try:
            with open(fileNameOut, 'r') as content_file:
                out = out + '\n' + content_file.read()
        except:
            return out

        os.remove(fileNameOut)
        return out

    return 'run'


def runCommand(command, fileName):
    try:
        p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate(timeout=30)
        err = err.decode('utf-8')
        out = out.decode('utf-8')

        if err == '':
            print('result: ' + out)
            os.remove(fileName)
            return out
        else:
            print('error: ' + err)
            os.remove(fileName)
            return "server internal error"

    except TimeoutExpired:
        p.kill()
        os.remove(fileName)
        return 'operation too long'

@@ -41,47 +41,109 @@
 t re
-nder_template%0Afrom flask import request
+quest, make_response, current_app%0Afrom functools import update_wrapper%0Afrom datetime import timedelta
 %0Aimp
@@ -242,16 +242,1535 @@
 me__)%0A%0A%0A
+def crossdomain(origin=None, methods=None, headers=None,%0A                max_age=21600, attach_to_all=True,%0A                automatic_options=True):%0A    if methods is not None:%0A        methods = ', '.join(sorted(x.upper() for x in methods))%0A    if headers is not None and not isinstance(headers, str):%0A        headers = ', '.join(x.upper() for x in headers)%0A    if not isinstance(origin, str):%0A        origin = ', '.join(origin)%0A    if isinstance(max_age, timedelta):%0A        max_age = max_age.total_seconds()%0A%0A    def get_methods():%0A        if methods is not None:%0A            return methods%0A%0A        options_resp = current_app.make_default_options_response()%0A        return options_resp.headers%5B'allow'%5D%0A%0A    def decorator(f):%0A        def wrapped_function(*args, **kwargs):%0A            if automatic_options and request.method == 'OPTIONS':%0A                resp = current_app.make_default_options_response()%0A            else:%0A                resp = make_response(f(*args, **kwargs))%0A            if not attach_to_all and request.method != 'OPTIONS':%0A                return resp%0A%0A            h = resp.headers%0A%0A            h%5B'Access-Control-Allow-Origin'%5D = origin%0A            h%5B'Access-Control-Allow-Methods'%5D = get_methods()%0A            h%5B'Access-Control-Max-Age'%5D = str(max_age)%0A            if headers is not None:%0A                h%5B'Access-Control-Allow-Headers'%5D = headers%0A            return resp%0A%0A        f.provide_automatic_options = False%0A        return update_wrapper(wrapped_function, f)%0A    return decorator%0A%0A%0A
 @app.rou
@@ -1807,24 +1807,49 @@
 T', 'GET'%5D)%0A
+@crossdomain(origin='*')%0A
 def interpre
@@ -2348,16 +2348,16 @@
 'run'%0A%0A%0A
-
 @app.rou
@@ -2396,16 +2396,41 @@
 'GET'%5D)%0A
+@crossdomain(origin='*')%0A
 def comp

add cross domain functionality