From faa13839f898c60ff5618be6e916ad2e60958468 Mon Sep 17 00:00:00 2001 From: nil0j Date: Sat, 21 Sep 2024 10:17:23 +0200 Subject: init --- python/.gitignore | 1 + python/README.md | 5 +++++ python/database.py | 20 ++++++++++++++++++++ python/main.py | 10 ++++++++++ python/routes.py | 24 ++++++++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 python/.gitignore create mode 100644 python/README.md create mode 100644 python/database.py create mode 100644 python/main.py create mode 100644 python/routes.py (limited to 'python') diff --git a/python/.gitignore b/python/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/python/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..4a30035 --- /dev/null +++ b/python/README.md @@ -0,0 +1,5 @@ +[X] - Flask return json +[X] - Get parameters +[X] - Create database +[ ] - Interact with database +[ ] - diff --git a/python/database.py b/python/database.py new file mode 100644 index 0000000..5bc8b5f --- /dev/null +++ b/python/database.py @@ -0,0 +1,20 @@ +import mysql.connector + + +def getConnector(): + return mysql.connector.connect( + host="localhost", + user="root", + ) + + +def getCursor(connector): + return connector.cursor() + + +def runQuery(connector, query): + cursor = getCursor(connector) + cursor.execute(query) + result = cursor.fetchall() + cursor.close() + return result diff --git a/python/main.py b/python/main.py new file mode 100644 index 0000000..c778f8f --- /dev/null +++ b/python/main.py @@ -0,0 +1,10 @@ +import routes +import database + +connector = database.getConnector() +database.runQuery(connector, "SHOW DATABASES") + +app = routes.createApp() +routes.createRoutes(app, connector) + +app.run() diff --git a/python/routes.py b/python/routes.py new file mode 100644 index 0000000..a60584d --- /dev/null +++ b/python/routes.py @@ -0,0 +1,24 @@ +from flask import Flask, jsonify, request +import database + + +def createApp(): + return Flask(__name__) + + +def createRoutes(app, connector): + @app.route("/", methods=['GET']) + def hello_world(): + arg1 = request.args.get('arg1') + arg2 = request.args.get('arg2') + return jsonify({'arg1': arg1, + 'arg2': arg2}) + + @app.route("/db", methods=['GET']) + def database_hello(): + return jsonify( + database.runQuery( + connector, + "SHOW DATABASES" + ) + ) -- cgit v1.2.3