├── .gitignore ├── README.md └── app.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Web Application 2 | 3 | This is a simple web application using [Python Flask](http://flask.pocoo.org/) and [MySQL](https://www.mysql.com/) database. 4 | This is used in the demonstration of development of Ansible Playbooks. 5 | 6 | Below are the steps required to get this working on a base linux system. 7 | 8 | - Install all required dependencies 9 | - Install and Configure Database 10 | - Start Database Service 11 | - Install and Configure Web Server 12 | - Start Web Server 13 | 14 | ## 1. Install all required dependencies 15 | 16 | Python and its dependencies 17 | 18 | apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb 19 | 20 | 21 | ## 2. Install and Configure Database 22 | 23 | Install MySQL database 24 | 25 | apt-get install -y mysql-server mysql-client 26 | 27 | ## 3. Start Database Service 28 | - Start the database service 29 | 30 | service mysql start 31 | 32 | - Create database and database users 33 | 34 | # mysql -u -p 35 | 36 | mysql> CREATE DATABASE employee_db; 37 | mysql> GRANT ALL ON *.* to db_user@'%' IDENTIFIED BY 'Passw0rd'; 38 | mysql> USE employee_db; 39 | mysql> CREATE TABLE employees (name VARCHAR(20)); 40 | 41 | - Insert some test data 42 | 43 | mysql> INSERT INTO employees VALUES ('JOHN'); 44 | 45 | ## 4. Install and Configure Web Server 46 | 47 | Install Python Flask dependency 48 | 49 | pip install flask 50 | pip install flask-mysql 51 | 52 | - Copy app.py or download it from source repository 53 | - Configure database credentials and parameters 54 | 55 | ## 5. Start Web Server 56 | 57 | Start web server 58 | 59 | FLASK_APP=app.py flask run --host=0.0.0.0 60 | 61 | ## 6. Test 62 | 63 | Open a browser and go to URL 64 | 65 | http://:5000 => Welcome 66 | http://:5000/how%20are%20you => I am good, how about you? 67 | http://:5000/read%20from%20database => JOHN 68 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import os 2 | from flask import Flask 3 | from flaskext.mysql import MySQL # For newer versions of flask-mysql 4 | # from flask.ext.mysql import MySQL # For older versions of flask-mysql 5 | app = Flask(__name__) 6 | 7 | mysql = MySQL() 8 | 9 | mysql_database_host = 'MYSQL_DATABASE_HOST' in os.environ and os.environ['MYSQL_DATABASE_HOST'] or 'localhost' 10 | 11 | # MySQL configurations 12 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 13 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 14 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 15 | app.config['MYSQL_DATABASE_HOST'] = mysql_database_host 16 | mysql.init_app(app) 17 | 18 | conn = mysql.connect() 19 | 20 | cursor = conn.cursor() 21 | 22 | @app.route("/") 23 | def main(): 24 | return "Welcome!" 25 | 26 | @app.route('/how are you') 27 | def hello(): 28 | return 'I am good, how about you?' 29 | 30 | @app.route('/read from database') 31 | def read(): 32 | cursor.execute("SELECT * FROM employees") 33 | row = cursor.fetchone() 34 | result = [] 35 | while row is not None: 36 | result.append(row[0]) 37 | row = cursor.fetchone() 38 | 39 | return ",".join(result) 40 | 41 | if __name__ == "__main__": 42 | app.run() 43 | --------------------------------------------------------------------------------