-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
35 lines (28 loc) · 1.09 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
from flask import Flask, render_template, request, redirect, url_for
from flask_mysqldb import MySQL
app = Flask(__name__)
# Configure MySQL from environment variables
app.config['MYSQL_HOST'] = os.environ.get('MYSQL_HOST', 'localhost')
app.config['MYSQL_USER'] = os.environ.get('MYSQL_USER', 'default_user')
app.config['MYSQL_PASSWORD'] = os.environ.get('MYSQL_PASSWORD', 'default_password')
app.config['MYSQL_DB'] = os.environ.get('MYSQL_DB', 'default_db')
# Initialize MySQL
mysql = MySQL(app)
@app.route('/')
def hello():
cur = mysql.connection.cursor()
cur.execute('SELECT message FROM messages')
messages = cur.fetchall()
cur.close()
return render_template('index.html', messages=messages)
@app.route('/submit', methods=['POST'])
def submit():
new_message = request.form.get('new_message')
cur = mysql.connection.cursor()
cur.execute('INSERT INTO messages (message) VALUES (%s)', [new_message])
mysql.connection.commit()
cur.close()
return redirect(url_for('hello'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)