from flask import Flask
import json
import logging
import mariadb
from os import environ

config = {
    'host': environ.get('DB_HOST'),
    'port': int(environ.get('DB_PORT')),
    'user': environ.get('DB_USER'),
    'password': environ.get('DB_PASS'),
    'database': environ.get('DB_NAME')
}

app = Flask(__name__)

gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
app.logger.setLevel(logging.INFO)

# queries the Menu table and returns the output in json format.

@app.route('/', methods=['GET'])
def index():
   #connection for MariaDB
   conn = mariadb.connect(**config)
#   conn = mysql.connector.connect(**config)
   # create a connection cursor
   cur = conn.cursor()
   # execute a SQL statement
   cur.execute("select * from Employees")

   # serialize results into JSON
   row_headers=[x[0] for x in cur.description]
   rv = cur.fetchall()
   json_data=[]
   for result in rv:
        json_data.append(dict(zip(row_headers,result)))

   # return the results!
   return json.dumps(json_data)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)