69 lines
1.5 KiB
Python
69 lines
1.5 KiB
Python
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('/<path:path>')
|
|
def getPaste(path):
|
|
print(path)
|
|
return loadPaste(path)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
application.run()
|