-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (44 loc) · 1.42 KB
/
app.py
File metadata and controls
53 lines (44 loc) · 1.42 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import mysql.connector
from flask import Flask, request, jsonify
app = Flask(__name__)
DB_CONFIG = {
'host': '127.0.0.1',
'user': 'foo',
'password': 'foo', # Replace with actual password
'database': 'routes_db'
}
def get_db_connection():
try:
conn = mysql.connector.connect(**DB_CONFIG)
print("✅ Connected to MySQL")
return conn
except mysql.connector.Error as err:
print(f"❌ Database connection error: {err}")
return None
@app.route("/routes", methods=["POST"])
def get_routes_between():
data = request.json
latitude = data.get("latitude")
longitude = data.get("longitude")
if not latitude or not longitude:
return jsonify({"error": "latitude and longitude are required"}), 400
conn = get_db_connection()
if conn is None:
return jsonify({"error": "Database connection failed"}), 500
cursor = conn.cursor(dictionary=True)
try:
query = """
SELECT * FROM routes
WHERE latitude = %s AND longitude = %s
"""
cursor.execute(query, (latitude, longitude))
routes = cursor.fetchall()
return jsonify(routes)
except mysql.connector.Error as err:
print(f"❌ Database query error: {err}")
return jsonify({"error": "Query failed"}), 500
finally:
cursor.close()
conn.close()
if __name__ == "__main__":
app.run(debug=True)