-
Notifications
You must be signed in to change notification settings - Fork 0
/
runServer.py
126 lines (112 loc) · 3.87 KB
/
runServer.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from backend.connectToMySQL import MySQL, SQL
from flask import Flask, render_template, request, jsonify, redirect, url_for
import os
FRONTEND_PATH = os.path.join('frontend', 'dist')
HOST = 'localhost'
USER = 'root'
PASSWORD = 'x94jo6cl6'
DB = 'cars'
app = Flask(__name__, template_folder=FRONTEND_PATH,
static_folder=FRONTEND_PATH)
app.config.update(
DEBUG=True,
TEMPLATES_AUTO_RELOAD=True,
SEND_FILE_MAX_AGE_DEFAULT=1,
)
sql = SQL('`cars`.`data`')
mysql = MySQL(host=HOST,
user=USER,
password=PASSWORD,
db=DB
)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/api/select", methods=['GET', 'POST'])
def select():
if request.method == 'GET':
return 'select'
elif request.method == 'POST':
mysql.__init__(host=HOST,
user=USER,
password=PASSWORD,
db=DB)
print(request.json)
sqlString = sql.select(where=sql.where(request.json['conditions']),
order=sql.order(request.json['orderBy']),
limit=sql.limit(request.json['limit']['start'], request.json['limit']['each']))
mysql.execute(sqlString)
mysql.getResponse()
# mysql.close()
return jsonify(mysql.executeResult)
@app.route("/api/distinct", methods=['GET', 'POST'])
def distinct():
if request.method == 'GET':
return 'distinct'
elif request.method == 'POST':
mysql.__init__(host=HOST,
user=USER,
password=PASSWORD,
db=DB)
mysql.execute(sql.distinct(request.json['targetColumn']))
mysql.getResponse()
return jsonify([list(item.values())[0] for item in mysql.executeResult if list(item.values())[0] != ''])
@app.route("/api/countPage", methods=['GET', 'POST'])
def countPage():
# TODO: 修改之
if request.method == 'GET':
return 'countPage'
elif request.method == 'POST':
mysql.__init__(host=HOST,
user=USER,
password=PASSWORD,
db=DB)
mysql.execute(sql.count(where=sql.where(request.json['conditions']),
order=sql.order(request.json['orderBy']),
limit=sql.limit(request.json['limit']['start'], request.json['limit']['each'])))
mysql.getResponse()
mysql.commit()
mysql.close()
return jsonify(mysql.executeResult)
@app.route("/api/insert", methods=['GET', 'POST'])
def insert():
# TODO
if request.method == 'GET':
return 'insert'
elif request.method == 'POST':
mysql.__init__(host=HOST,
user=USER,
password=PASSWORD,
db=DB)
mysql.execute(sql.insert(request.json.keys(), request.json.values()))
mysql.commit()
mysql.close()
@app.route("/api/update", methods=['GET', 'POST'])
def update():
# TODO
if request.method == 'GET':
return 'update'
elif request.method == 'POST':
mysql.__init__(host=HOST,
user=USER,
password=PASSWORD,
db=DB)
mysql.execute(sql.update(request.json, request.json['id']))
mysql.commit()
mysql.close()
return redirect(url_for('index'))
@app.route("/api/delete", methods=['GET', 'POST'])
def delete():
# TODO
if request.method == 'GET':
return 'delete'
elif request.method == 'POST':
mysql.__init__(host=HOST,
user=USER,
password=PASSWORD,
db=DB)
mysql.execute(sql.delete(request.json['id']))
mysql.commit()
mysql.close()
if __name__ == "__main__":
app.run(host='0.0.0.0')