clean up and properly structure backend

This commit is contained in:
Oskar Winkels 2020-12-07 20:57:57 +01:00 committed by Oskar
parent c5a124e054
commit 5f296849f5
5 changed files with 59 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
__pycache__/
*.egg-info/
*.log
*.db
*.json

View File

@ -0,0 +1 @@
from .app import app

View File

@ -0,0 +1,11 @@
from .app import app
if __name__ == "__main__":
@app.after_request
def add_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
return response
app.run(host='0.0.0.0', port=12345)

View File

@ -1,4 +1,4 @@
from flask import Flask, request, Response
from flask import Flask, request
import json
import string
import random
@ -7,12 +7,13 @@ import atexit
app = Flask(__name__)
filename = 'db.json'
id_length = 32
id_length = 16
data = {}
def startup():
global data
try:
fd = open(filename)
@ -21,37 +22,38 @@ def startup():
pass
@app.after_request
def add_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
return response
@app.route('/<id>', methods = ['GET'])
def get(id):
if id in data:
return data[id]
else:
return 'Error: Not found', 404
return data[id] if id in data else ('Error: Not found', 404)
def generateRandomString():
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(id_length))
def generateNewKey():
id = generateRandomString()
while id in data:
id = generateRandomString()
return id
@app.route('/', methods = ['POST'])
def post():
id = generateRandomString()
id = generateNewKey()
data[id] = request.data.decode('UTF-8')
return id, 201
def dumpDB():
if len(data) == 0:
return
fd = open(filename, 'w+')
try:
fd = open(filename, 'w+')
json.dump(data, fd)
finally:
fd.close()
@ -59,6 +61,3 @@ def dumpDB():
startup()
atexit.register(dumpDB)
if __name__ == "__main__":
app.run(host='0.0.0.0')

29
setup.py Normal file
View File

@ -0,0 +1,29 @@
import setuptools as st
with open("README.md", "r") as f:
long_description = f.read()
with open('LICENSE.md') as f:
license_text = f.read()
st.setup(
name="fs-quiz-tool-db",
version="0.1.0",
author="Oskar @ FaSTTUBe",
author_email="o.winkels@fasttube.de",
description="A tool to help FS teams train for registration qualification quizzes",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://git.fasttube.de/FaSTTUBe/fs-quiz-tool",
packages=st.find_packages(exclude=['tests', 'docs']),
install_requires=[
"flask",
],
license=license_text,
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)