-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
110 lines (98 loc) · 3.46 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from flask import request
from flask_api import FlaskAPI
from typing import Tuple, List
from database.interface import DBInterface, Table
import argparse
app = FlaskAPI(__name__)
app.config['HOST'] = '0.0.0.0'
app.config['DEBUG'] = True
app.config['ENV'] = 'development'
dbi: DBInterface = None
@app.route('/api/fetch/<table_str>', methods=['GET']) # Get class. Optionally filter by id
def fetch(table_str):
try:
table_str: str = table_str.upper()
table: Table = Table[table_str] # Naive security control along with mapping function
except:
return "table arg is invalid", 400
id = request.args.get('upper_id')
if id is not None:
try:
id = int(id)
except:
return "id arg type error", 406
try:
result: List[Tuple] = None
if table is Table.CLASS:
result = dbi.list_classes()
elif table is Table.FAMILY:
result = dbi.list_families(id)
elif table is Table.SPECIES:
result = dbi.list_species(id)
elif table is Table.SPECIMEN:
result = dbi.list_specimens(id)
return {
'content': result,
'columns': dbi.get_columns()
}
except:
return "internal error", 500
# @app.route('/api/fetch/<table>/<id>', methods=['GET'])
# def fetch_id(table_str, id):
# try:
# table_str: str = table_str.upper()
# table: Table = Table[table_str] # Naive security control along with mapping function
# except:
# return "table arg is invalid", 400
#
# try:
# id_parsed: int = int(id)
# except:
# return "id arg type error", 406
#
# try:
# result: List[Tuple] = None
# if table is Table.CLASS:
# result = dbi.list_classes(id_parsed)
# elif table is Table.FAMILY:
# result = dbi.list_families(id_parsed)
# elif table is Table.SPECIES:
# result = dbi.list_species(id_parsed)
# elif table is Table.SPECIMEN:
# result = dbi.list_specimens(id_parsed)
#
# return {
# 'content': result,
# 'columns': dbi.get_columns()
# }
# except:
# return "internal error", 500
@app.route("/api/fetch/hierarchy", methods=["GET"])
def fetch_hierarchy():
return {
'content': dbi.list_hierarchy(),
'columns': dbi.get_columns()
}
@app.after_request
def add_header(response):
response.headers['Access-Control-Allow-Origin'] = 'http://localhost:3000'
return response
# @app.route('/get_everything_from', methods=['GET'])
# def get_everything_from():
# table: str = request.args.get("table").upper()
# search_field: str = request.args.get("search_field")
# search_field_equals_to: str = request.args.get("search_field_equals_to")
#
# try:
# if search_field is None:
# return json.dumps(dbi.get_everything_from(Table[table], search_field_equals_to))
# else:
# return json.dumps(dbi.get_everything_from(Table[table], search_field_equals_to, search_field))
# except QueryFailed:
# return "Query failed", 400
if __name__ == '__main__':
parser: argparse.ArgumentParser = argparse.ArgumentParser()
parser.add_argument("-p", "--password", help="MariaDB (MySQL) password", type=str, dest="password")
args = parser.parse_args()
dbi = DBInterface("127.0.0.1", 3306, "writeUser", args.password, "conchiglie")
app.run(host='0.0.0.0', port=5000, debug=True)