|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | +from flask import request |
| 5 | +from flask import Flask, render_template |
| 6 | +import mysql.connector |
| 7 | +from mysql.connector import errorcode |
| 8 | + |
| 9 | + |
| 10 | +application = Flask(__name__) |
| 11 | +app = application |
| 12 | + |
| 13 | + |
| 14 | +def get_db_creds(): |
| 15 | + db = os.environ.get("DB", None) or os.environ.get("database", None) |
| 16 | + username = os.environ.get("USER", None) or os.environ.get("username", None) |
| 17 | + password = os.environ.get("PASSWORD", None) or os.environ.get("password", None) |
| 18 | + hostname = os.environ.get("HOST", None) or os.environ.get("dbhost", None) |
| 19 | + return db, username, password, hostname |
| 20 | + |
| 21 | + |
| 22 | +def create_table(): |
| 23 | + # Check if table exists or not. Create and populate it only if it does not exist. |
| 24 | + db, username, password, hostname = get_db_creds() |
| 25 | + table_ddl = 'CREATE TABLE message(id INT UNSIGNED NOT NULL AUTO_INCREMENT, greeting TEXT, PRIMARY KEY (id))' |
| 26 | + |
| 27 | + cnx = '' |
| 28 | + try: |
| 29 | + cnx = mysql.connector.connect(user=username, password=password, |
| 30 | + host=hostname, |
| 31 | + database=db) |
| 32 | + except Exception as exp: |
| 33 | + print(exp) |
| 34 | + import MySQLdb |
| 35 | + #try: |
| 36 | + cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db) |
| 37 | + #except Exception as exp1: |
| 38 | + # print(exp1) |
| 39 | + |
| 40 | + cur = cnx.cursor() |
| 41 | + |
| 42 | + try: |
| 43 | + cur.execute(table_ddl) |
| 44 | + cnx.commit() |
| 45 | + populate_data() |
| 46 | + except mysql.connector.Error as err: |
| 47 | + if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: |
| 48 | + print("already exists.") |
| 49 | + else: |
| 50 | + print(err.msg) |
| 51 | + |
| 52 | + |
| 53 | +def populate_data(): |
| 54 | + |
| 55 | + db, username, password, hostname = get_db_creds() |
| 56 | + |
| 57 | + print("Inside populate_data") |
| 58 | + print("DB: %s" % db) |
| 59 | + print("Username: %s" % username) |
| 60 | + print("Password: %s" % password) |
| 61 | + print("Hostname: %s" % hostname) |
| 62 | + |
| 63 | + cnx = '' |
| 64 | + try: |
| 65 | + cnx = mysql.connector.connect(user=username, password=password, |
| 66 | + host=hostname, |
| 67 | + database=db) |
| 68 | + except Exception as exp: |
| 69 | + print(exp) |
| 70 | + import MySQLdb |
| 71 | + cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db) |
| 72 | + |
| 73 | + cur = cnx.cursor() |
| 74 | + cur.execute("INSERT INTO message (greeting) values ('Hello, World!')") |
| 75 | + cnx.commit() |
| 76 | + print("Returning from populate_data") |
| 77 | + |
| 78 | + |
| 79 | +def query_data(): |
| 80 | + |
| 81 | + db, username, password, hostname = get_db_creds() |
| 82 | + |
| 83 | + print("Inside query_data") |
| 84 | + print("DB: %s" % db) |
| 85 | + print("Username: %s" % username) |
| 86 | + print("Password: %s" % password) |
| 87 | + print("Hostname: %s" % hostname) |
| 88 | + |
| 89 | + cnx = '' |
| 90 | + try: |
| 91 | + cnx = mysql.connector.connect(user=username, password=password, |
| 92 | + host=hostname, |
| 93 | + database=db) |
| 94 | + except Exception as exp: |
| 95 | + print(exp) |
| 96 | + import MySQLdb |
| 97 | + cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db) |
| 98 | + |
| 99 | + cur = cnx.cursor() |
| 100 | + |
| 101 | + cur.execute("SELECT greeting FROM message") |
| 102 | + entries = [dict(greeting=row[0]) for row in cur.fetchall()] |
| 103 | + return entries |
| 104 | + |
| 105 | +try: |
| 106 | + print("---------" + time.strftime('%a %H:%M:%S')) |
| 107 | + print("Before create_table global") |
| 108 | + create_table() |
| 109 | + print("After create_data global") |
| 110 | +except Exception as exp: |
| 111 | + print("Got exception %s" % exp) |
| 112 | + conn = None |
| 113 | + |
| 114 | + |
| 115 | +@app.route('/add_to_db', methods=['POST']) |
| 116 | +def add_to_db(): |
| 117 | + print("Received request.") |
| 118 | + print(request.form['message']) |
| 119 | + msg = request.form['message'] |
| 120 | + |
| 121 | + db, username, password, hostname = get_db_creds() |
| 122 | + |
| 123 | + cnx = '' |
| 124 | + try: |
| 125 | + cnx = mysql.connector.connect(user=username, password=password, |
| 126 | + host=hostname, |
| 127 | + database=db) |
| 128 | + except Exception as exp: |
| 129 | + print(exp) |
| 130 | + import MySQLdb |
| 131 | + cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db) |
| 132 | + |
| 133 | + cur = cnx.cursor() |
| 134 | + cur.execute("INSERT INTO message (greeting) values ('" + msg + "')") |
| 135 | + cnx.commit() |
| 136 | + return hello() |
| 137 | + |
| 138 | + |
| 139 | +@app.route("/") |
| 140 | +def hello(): |
| 141 | + print("Inside hello") |
| 142 | + print("Printing available environment variables") |
| 143 | + print(os.environ) |
| 144 | + print("Before displaying index.html") |
| 145 | + entries = query_data() |
| 146 | + print("Entries: %s" % entries) |
| 147 | + return render_template('index.html', entries=entries) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + app.debug = True |
| 152 | + app.run(host='0.0.0.0') |
0 commit comments