commit 83e78fa7259148886018c96c0e4ba2a483e5fff8 Author: Benjamyn Date: Sat May 2 06:37:40 2020 -0400 Initial diff --git a/.config template b/.config template new file mode 100644 index 0000000..aa603a8 --- /dev/null +++ b/.config template @@ -0,0 +1,5 @@ +[mysql] +Host = localhost +Username = SQLUSERNAME +Password = SQLPASSWORD +Database = SQLDATABASE \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f9c719 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +env/ +.config diff --git a/db.py b/db.py new file mode 100644 index 0000000..a93ed71 --- /dev/null +++ b/db.py @@ -0,0 +1,52 @@ +import mysql.connector +import configparser + +config = configparser.ConfigParser() + +try: + config.read(".config") +except Exception as E: + print(E) + + +def dbConnect(): + mydb = mysql.connector.connect( + host=config["mysql"]["Host"], + user=config["mysql"]["Username"], + passwd=config["mysql"]["Password"], + database=config["mysql"]["Database"] + ) + return mydb + + +def runQuery(query, data=None): + mydb = dbConnect() + c = mydb.cursor() + if data is not None: + c.execute(query, data) + else: + c.execute(query) + if query.lower().startswith("select"): + ret = c.fetchall() + else: + ret = [] + mydb.commit() + mydb.close() + return ret + + +def checkIfPasteExists(id): + query = "SELECT id from pastes where id=%s" + if runQuery(query, (id,)): + return True + return False + + +def insertPaste(id, paste): + query = "INSERT into pastes (id, paste) values (%s, %s)" + runQuery(query, (id, paste)) + + +def getPaste(id): + query = "select paste from pastes where id=%s" + return runQuery(query, (id,)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6d6b677 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +flask-cors +mysql-connector \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..44b78c4 --- /dev/null +++ b/server.py @@ -0,0 +1,68 @@ +from flask import Flask, request, make_response +from flask_cors import CORS +from pprint import pprint +import random +import string +import db + +nameLength = 10 +pastes = {} + +application = Flask(__name__) +CORS(application) +application.config['DEBUG'] = False +letters = string.ascii_uppercase + string.ascii_lowercase + string.digits + + +def randomName(): + name = "" + for x in range(0, nameLength): + x = x + num = random.randint(0, len(letters)) + # print(num) + name += letters[num - 1] + return name + + +def savePaste(id, paste): + db.insertPaste(id, paste) + + +def loadPaste(id): + paste = db.getPaste(id)[0][0] + return paste + + +def generateUniqeName(id, works=False): + if id == None: + id = randomName() + if works == False: + id = randomName() + if db.checkIfPasteExists(id): + id = generateUniqeName(id, False) + return id + + +@application.route('/', methods=['GET', 'POST']) +def index(): + if request.method == 'POST': + if request.headers.get('Content-Type') == 'application/json': + data = request.json + id = randomName() + id = generateUniqeName(id, True) + savePaste(id, data['paste']) + resp = make_response(id) + resp.headers['Content-Type'] = 'application/json' + return resp + + return '' + + +@application.route('/') +def getPaste(path): + print(path) + return loadPaste(path) + + +if __name__ == '__main__': + application.run()