mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-05-06 18:29:24 +08:00
Merge branch 'dev1'
This commit is contained in:
commit
31aa97deca
92
db.py
92
db.py
@ -1,7 +1,18 @@
|
|||||||
import pymysql , config
|
import pymysql , config , uuid
|
||||||
|
|
||||||
|
def dbIsOK():
|
||||||
|
#打开数据库连接
|
||||||
|
try:
|
||||||
|
db = pymysql.connect(host=config.readConf()["db"]["host"],
|
||||||
|
port=config.readConf()["db"]["port"],
|
||||||
|
user=config.readConf()["db"]["user"],
|
||||||
|
password=config.readConf()["db"]["passwd"],
|
||||||
|
database=config.readConf()["db"]["database"])
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
def userSurplus(userkey):
|
def userSurplus(userkey): #查询userkey剩余配额
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
db = pymysql.connect(host=config.readConf()["db"]["host"],
|
db = pymysql.connect(host=config.readConf()["db"]["host"],
|
||||||
port=config.readConf()["db"]["port"],
|
port=config.readConf()["db"]["port"],
|
||||||
@ -46,7 +57,7 @@ def reduce_value(userkey, value): # 减去对应的值
|
|||||||
new_value = current_value - value
|
new_value = current_value - value
|
||||||
|
|
||||||
# 更新数据库中的值
|
# 更新数据库中的值
|
||||||
cursor.execute(f"UPDATE usersurplus SET surplus= %s WHERE userkey=%s",[new_value,userkey])
|
cursor.execute(f"UPDATE usersurplus SET surplus= %s WHERE userkey= %s;",[new_value,userkey])
|
||||||
|
|
||||||
# 提交事务
|
# 提交事务
|
||||||
db.commit()
|
db.commit()
|
||||||
@ -55,4 +66,77 @@ def reduce_value(userkey, value): # 减去对应的值
|
|||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
# 返回新值
|
# 返回新值
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def getAllKey():
|
||||||
|
#打开数据库连接
|
||||||
|
db = pymysql.connect(host=config.readConf()["db"]["host"],
|
||||||
|
port=config.readConf()["db"]["port"],
|
||||||
|
user=config.readConf()["db"]["user"],
|
||||||
|
password=config.readConf()["db"]["passwd"],
|
||||||
|
database=config.readConf()["db"]["database"])
|
||||||
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
|
cursor.execute(f"SELECT * FROM usersurplus ;")
|
||||||
|
# 使用 fetchall() 方法获取结果集
|
||||||
|
data = cursor.fetchall()
|
||||||
|
|
||||||
|
# 关闭连接
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def delKey(userkey):
|
||||||
|
#打开数据库连接
|
||||||
|
db = pymysql.connect(host=config.readConf()["db"]["host"],
|
||||||
|
port=config.readConf()["db"]["port"],
|
||||||
|
user=config.readConf()["db"]["user"],
|
||||||
|
password=config.readConf()["db"]["passwd"],
|
||||||
|
database=config.readConf()["db"]["database"])
|
||||||
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
|
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = %s;", [userkey])
|
||||||
|
|
||||||
|
# 提交事务
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
if cursor.rowcount > 0:
|
||||||
|
db.close() # 使用 rowcount() 方法查询受影响行数
|
||||||
|
return True
|
||||||
|
db.close()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def createKey(quota,number=1,key="null"):
|
||||||
|
#打开数据库连接
|
||||||
|
db = pymysql.connect(host=config.readConf()["db"]["host"],
|
||||||
|
port=config.readConf()["db"]["port"],
|
||||||
|
user=config.readConf()["db"]["user"],
|
||||||
|
password=config.readConf()["db"]["passwd"],
|
||||||
|
database=config.readConf()["db"]["database"])
|
||||||
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
|
output = []
|
||||||
|
if key == "null":
|
||||||
|
for i in range(int(number)):
|
||||||
|
key = str(uuid.uuid1())
|
||||||
|
output.append(key)
|
||||||
|
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
|
||||||
|
else:
|
||||||
|
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
|
||||||
|
output.append(key)
|
||||||
|
|
||||||
|
# 提交事务
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
61
main.py
61
main.py
@ -5,6 +5,7 @@ from apiModule import qwenTurbo , chatglmTurbo , gpt35Turbo , gpt4Turbo
|
|||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
CORS(app,origins="*")
|
CORS(app,origins="*")
|
||||||
|
app.secret_key = b'SQ-{kJE;m(jEBi|{yq]v'
|
||||||
|
|
||||||
@app.route('/api/user', methods=['POST'])
|
@app.route('/api/user', methods=['POST'])
|
||||||
def post_data():
|
def post_data():
|
||||||
@ -49,10 +50,70 @@ def post_data():
|
|||||||
db.reduce_value(userRequest['userkey'], tokenUsed)
|
db.reduce_value(userRequest['userkey'], tokenUsed)
|
||||||
return {"code":code,"output":output,"surplus":surplusToken}
|
return {"code":code,"output":output,"surplus":surplusToken}
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
return flask.render_template('index.html')
|
return flask.render_template('index.html')
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/login', methods=['POST','GET'])
|
||||||
|
def login():
|
||||||
|
if flask.request.method == 'GET':
|
||||||
|
return flask.render_template('login.html')
|
||||||
|
userRequest = flask.request.form
|
||||||
|
if userRequest["password"] != config.readConf()["appconf"]["adminkey"]:
|
||||||
|
return flask.render_template('login.html')
|
||||||
|
flask.session["admin"] = True
|
||||||
|
return flask.redirect(flask.url_for('admin'))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/admin')
|
||||||
|
def admin():
|
||||||
|
if "admin" in flask.session :
|
||||||
|
status = {}
|
||||||
|
status["db"] = db.dbIsOK()
|
||||||
|
return flask.render_template("status.html" ,status=status)
|
||||||
|
return "未登录"
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/admin/list')
|
||||||
|
def adminList():
|
||||||
|
if "admin" in flask.session :
|
||||||
|
data = db.getAllKey()
|
||||||
|
return flask.render_template("keylist.html",data=data)
|
||||||
|
return "未登录 "
|
||||||
|
|
||||||
|
@app.route('/admin/createkey', methods=['POST','GET'])
|
||||||
|
def createkey():
|
||||||
|
if "admin" in flask.session :
|
||||||
|
if flask.request.method == "GET":
|
||||||
|
return flask.render_template("createKey.html",resq="null")
|
||||||
|
if "number" in flask.request.form: # 创建单个还是多个
|
||||||
|
resq = db.createKey(flask.request.form["quota"], flask.request.form["number"])
|
||||||
|
elif "key" in flask.request.form:
|
||||||
|
resq = db.createKey(flask.request.form["quota"],1,flask.request.form["key"])
|
||||||
|
|
||||||
|
return flask.render_template("createKey.html",resq=resq)
|
||||||
|
return "未登录 "
|
||||||
|
|
||||||
|
@app.route('/admin/lookupkey', methods=['POST','GET'])
|
||||||
|
def lookupkey():
|
||||||
|
if "admin" in flask.session :
|
||||||
|
if flask.request.method == "GET":
|
||||||
|
return flask.render_template("lookupKey.html",resq="null")
|
||||||
|
resq = db.userSurplus(flask.request.form["key"])
|
||||||
|
return flask.render_template("lookupKey.html",resq=resq)
|
||||||
|
return "未登录 "
|
||||||
|
|
||||||
|
@app.route('/admin/operate', methods=['POST','GET'])
|
||||||
|
def operate():
|
||||||
|
if "admin" in flask.session :
|
||||||
|
if flask.request.args['type'] == "del":
|
||||||
|
if db.delKey(flask.request.args['target']):
|
||||||
|
return "成功 <a href='javascript:;' onclick='self.location=document.referrer;'>返回上一页并刷新</a>"
|
||||||
|
return "失败 <a href='javascript:;' onclick='self.location=document.referrer;'>返回上一页并刷新</a>"
|
||||||
|
return "拒绝访问"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"])
|
app.run(debug=bool(config.readConf()["appconf"]["debug"]),host=config.readConf()["appconf"]["host"],port=config.readConf()["appconf"]["port"])
|
61
templates/createKey.html
Normal file
61
templates/createKey.html
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>创建密钥</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #f7f7f7;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
background-color: #fff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>创建单个密钥</h2>
|
||||||
|
<form method="post">
|
||||||
|
<span>密钥</span>
|
||||||
|
<input name="key" required>
|
||||||
|
<span>配额</span>
|
||||||
|
<input name="quota" required>
|
||||||
|
<button type="submit">创建</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<hr>
|
||||||
|
<div class="container">
|
||||||
|
<h2>批量创建密钥</h2>
|
||||||
|
<form method="post">
|
||||||
|
<input type="radio" id="type" name="type" value="uuid" checked="true">
|
||||||
|
<label for="type">使用UUID</label><br>
|
||||||
|
<span>个数</span>
|
||||||
|
<input name="number" required>
|
||||||
|
<span>配额</span>
|
||||||
|
<input name="quota" required>
|
||||||
|
<button type="submit">批量创建</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% if resq != "null" %}
|
||||||
|
<hr>
|
||||||
|
<div class="container">
|
||||||
|
<h2>执行结果</h2>
|
||||||
|
{% for i in resq %}
|
||||||
|
<h4>{{ i }}</h4>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
<script>
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
68
templates/keylist.html
Normal file
68
templates/keylist.html
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>列出密钥</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #f7f7f7;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
background-color: #fff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
|
||||||
|
th,
|
||||||
|
td {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
background-color: #f2f2f2;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>列出密钥</h2>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Key</th>
|
||||||
|
<th>剩余Tokens</th>
|
||||||
|
<th>操作</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for item in data %}
|
||||||
|
<tr>
|
||||||
|
<td>{{item[0]}}</td>
|
||||||
|
<td>{{item[1]}}</td>
|
||||||
|
<td><a href="/admin/operate?type=del&target={{item[0]}}">删除</a></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// 在这里可以添加一些交互逻辑,例如点击某个状态显示更多信息
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
64
templates/login.html
Normal file
64
templates/login.html
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>登录页面</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
height: 100vh;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-container {
|
||||||
|
padding: 20px;
|
||||||
|
width: 300px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-container h2 {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.password-input {
|
||||||
|
padding: 10px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 3px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-button {
|
||||||
|
padding: 10px 20px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 3px;
|
||||||
|
background-color: #5cb85c;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-button:hover {
|
||||||
|
background-color: #4cae4c;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="login-container">
|
||||||
|
<h2>登录</h2>
|
||||||
|
<form action="login" method="post">
|
||||||
|
<input name="password" type="password" id="password" class="password-input" placeholder="输入密码" required />
|
||||||
|
<button type="submit" class="login-button">登录</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
48
templates/lookupKey.html
Normal file
48
templates/lookupKey.html
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>查询密钥</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #f7f7f7;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
background-color: #fff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>查询密钥</h2>
|
||||||
|
<form method="post">
|
||||||
|
<span>UserKey</span>
|
||||||
|
<input name="key" required>
|
||||||
|
<button type="submit">查询</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% if resq != "null" %}
|
||||||
|
<hr>
|
||||||
|
<div class="container">
|
||||||
|
<h2>执行结果</h2>
|
||||||
|
{% if resq == -99999 %}
|
||||||
|
<h4>未找到UserKey</h4>
|
||||||
|
{% else %}
|
||||||
|
<h4>配额剩余 {{ resq }}</h4>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
<script>
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
112
templates/status.html
Normal file
112
templates/status.html
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>后台管理</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #f7f7f7;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
background-color: #fff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
|
||||||
|
th,
|
||||||
|
td {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
background-color: #f2f2f2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.green {
|
||||||
|
color: green;
|
||||||
|
}
|
||||||
|
|
||||||
|
.red {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nodecoration{
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>状态</h2>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>项目</th>
|
||||||
|
<th>状态</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>开发者心态</td>
|
||||||
|
<td class="green">正常</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>数据库连接</td>
|
||||||
|
{% if status["db"] == True %}
|
||||||
|
<td class="green">正常</td>
|
||||||
|
{% else %}
|
||||||
|
<td class="red">连接异常</td>
|
||||||
|
{% endif %}
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>开发者大脑负载</td>
|
||||||
|
<td class="red">较高</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<hr/>
|
||||||
|
<div class="container">
|
||||||
|
<h2>管理</h2>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>项目</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td><a href="/admin/list" class="nodecoration">列出所有Key</a></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><a href="/admin/lookupkey" class="nodecoration">查询密钥</a></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><a href="/admin/createkey" class="nodecoration">创建密钥</a></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>阿巴阿巴</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user