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__/ __pycache__/
*.egg-info/
*.log *.log
*.db *.db
*.json *.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 json
import string import string
import random import random
@ -7,12 +7,13 @@ import atexit
app = Flask(__name__) app = Flask(__name__)
filename = 'db.json' filename = 'db.json'
id_length = 32 id_length = 16
data = {} data = {}
def startup(): def startup():
global data global data
try: try:
fd = open(filename) fd = open(filename)
@ -21,37 +22,38 @@ def startup():
pass 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']) @app.route('/<id>', methods = ['GET'])
def get(id): def get(id):
if id in data: return data[id] if id in data else ('Error: Not found', 404)
return data[id]
else:
return 'Error: Not found', 404
def generateRandomString(): def generateRandomString():
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(id_length)) 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']) @app.route('/', methods = ['POST'])
def post(): def post():
id = generateRandomString()
id = generateNewKey()
data[id] = request.data.decode('UTF-8') data[id] = request.data.decode('UTF-8')
return id, 201 return id, 201
def dumpDB(): def dumpDB():
if len(data) == 0: if len(data) == 0:
return return
fd = open(filename, 'w+')
try: try:
fd = open(filename, 'w+')
json.dump(data, fd) json.dump(data, fd)
finally: finally:
fd.close() fd.close()
@ -59,6 +61,3 @@ def dumpDB():
startup() startup()
atexit.register(dumpDB) 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',
)