Niansuh commited on
Commit
05d4c21
1 Parent(s): 1d5d7cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify
2
+ import mysql.connector
3
+ import os
4
+ from mysql.connector import Error
5
+
6
+ app = Flask(__name__)
7
+
8
+ # Get MySQL connection details from environment variables
9
+ MYSQL_HOST = os.getenv('MYSQL_HOST', 'localhost') # Default to localhost if not set
10
+ MYSQL_USER = os.getenv('MYSQL_USER', 'root')
11
+ MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', 'password')
12
+ MYSQL_DB = os.getenv('MYSQL_DB', 'your_database') # Optional database name
13
+
14
+ @app.route('/mysql/status', methods=['GET'])
15
+ def check_mysql_status():
16
+ try:
17
+ # Try connecting to MySQL
18
+ connection = mysql.connector.connect(
19
+ host=MYSQL_HOST,
20
+ user=MYSQL_USER,
21
+ password=MYSQL_PASSWORD,
22
+ database=MYSQL_DB
23
+ )
24
+
25
+ # If connection is successful, return status
26
+ if connection.is_connected():
27
+ return jsonify({
28
+ "status": "OK",
29
+ "message": "MySQL Server is up and running!",
30
+ "host": MYSQL_HOST
31
+ }), 200
32
+ else:
33
+ return jsonify({
34
+ "status": "Error",
35
+ "message": "Failed to connect to MySQL server."
36
+ }), 500
37
+
38
+ except Error as e:
39
+ return jsonify({
40
+ "status": "Error",
41
+ "message": f"Database connection failed: {str(e)}"
42
+ }), 500
43
+
44
+ finally:
45
+ if connection.is_connected():
46
+ connection.close()
47
+
48
+ if __name__ == '__main__':
49
+ # Run the app (make it accessible on all IPs, port 5000)
50
+ app.run(debug=True, host='0.0.0.0', port=5000)