新增数据库初始化脚本,创建外链表和用户表,更新主程序以支持数据库连接和关闭,添加外链信息的API接口。
This commit is contained in:
parent
cc90a4b172
commit
f8da20b638
40
init_db.py
Normal file
40
init_db.py
Normal file
@ -0,0 +1,40 @@
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
# 确保数据库文件存放在正确的目录
|
||||
DB_PATH = 'database.db'
|
||||
|
||||
def init_db():
|
||||
# 连接到数据库(如果不存在则创建)
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 创建外链表
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS exlinks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
exlink_id TEXT NOT NULL,
|
||||
qr_limit INTEGER DEFAULT 3,
|
||||
drivers TEXT, -- 存储为JSON字符串,例如 '["quark","wechat"]'
|
||||
use_limit INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
''')
|
||||
|
||||
# 创建用户表(如果需要的话)
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
token TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
''')
|
||||
|
||||
# 提交更改
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_db()
|
||||
print("Database initialized successfully!")
|
60
main.py
60
main.py
@ -1,9 +1,29 @@
|
||||
import os
|
||||
import requests
|
||||
import sqlite3
|
||||
from flask import g
|
||||
from flask import Flask, render_template, request, redirect, url_for, session,make_response,send_from_directory,jsonify
|
||||
from flask.views import MethodView
|
||||
from werkzeug.routing import BaseConverter
|
||||
from utils.login import login_quark
|
||||
from utils.tools import get_cnb_weburl
|
||||
|
||||
# 数据库配置
|
||||
DATABASE = 'database.db'
|
||||
|
||||
def get_db():
|
||||
db = getattr(g, '_database', None)
|
||||
if db is None:
|
||||
db = g._database = sqlite3.connect(DATABASE)
|
||||
db.row_factory = sqlite3.Row
|
||||
return db
|
||||
|
||||
@app.teardown_appcontext
|
||||
def close_connection(exception):
|
||||
db = getattr(g, '_database', None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
app = Flask(__name__)
|
||||
#app = Flask(__name__,template_folder='templates') #修改模板目录
|
||||
app.jinja_env.auto_reload = True
|
||||
@ -33,13 +53,13 @@ def index():
|
||||
#return render_template("index.html")
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route("/<phone:param>")
|
||||
def phone(param):
|
||||
return param
|
||||
# @app.route("/<phone:param>")
|
||||
# def phone(param):
|
||||
# return param
|
||||
|
||||
@app.route("/<li:param>")
|
||||
def uner_info(param):
|
||||
return param
|
||||
# @app.route("/<li:param>")
|
||||
# def uner_info(param):
|
||||
# return param
|
||||
|
||||
#falsk中重定向
|
||||
@app.route('/profile/')
|
||||
@ -78,6 +98,34 @@ def qrlink(id):
|
||||
def admin():
|
||||
return render_template('admin.html')
|
||||
|
||||
|
||||
class Exlink(MethodView):
|
||||
def get(self):
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute('SELECT * FROM exlinks')
|
||||
exlinks = cursor.fetchall()
|
||||
return jsonify([dict(row) for row in exlinks])
|
||||
|
||||
def post(self):
|
||||
data = request.json
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute('''
|
||||
INSERT INTO exlinks (exlink_id, qr_limit, drivers, use_limit)
|
||||
VALUES (?, ?, ?, ?)
|
||||
''', (
|
||||
data.get('exlink_id'),
|
||||
data.get('qr_limit', 3),
|
||||
data.get('drivers', '["quark","wechat"]'),
|
||||
data.get('use_limit', 0)
|
||||
))
|
||||
db.commit()
|
||||
return jsonify({"status": True, "message": "success"})
|
||||
|
||||
|
||||
app.add_url_rule('/admin/exlink', view_func=Exlink.as_view('exlink'))
|
||||
|
||||
if __name__ == "__main__":
|
||||
weburl = get_cnb_weburl(5000)
|
||||
print("Run_url:",weburl)
|
||||
|
@ -263,7 +263,7 @@
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">有效期</label>
|
||||
<input type="date" class="form-control" name="access_token" required>
|
||||
<input type="date" class="form-control" name="expiry_date" required>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@ -357,18 +357,16 @@
|
||||
//添加保存外链事件
|
||||
$('#saveExlink').on('click', function() {
|
||||
// 获取表单数据
|
||||
// 确保正确获取表单元素
|
||||
const exlinkForm = document.querySelector('#addExlinkModal form');
|
||||
if (!exlinkForm) {
|
||||
showMessage('找不到元素', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData(exlinkForm);
|
||||
const formValues = Object.fromEntries(formData.entries());
|
||||
const formData = new FormData(document.querySelector('#addExlinkModal form'));
|
||||
const formValues = {
|
||||
disk_type: formData.get('disk_type'),
|
||||
account_id: formData.get('account_id'),
|
||||
remaining_count: formData.get('remaining_count'),
|
||||
expiry_date: formData.get('expiry_date')
|
||||
};
|
||||
|
||||
// 检查表单是否填写完整
|
||||
if (!formValues.account_id || !formValues.remaining_count || !formValues.access_token) {
|
||||
if (!formValues.disk_type || !formValues.remaining_count || !formValues.account_id || !formValues.expiry_date) {
|
||||
showMessage('请填写所有必填字段!', 'error');
|
||||
return; // 停止提交
|
||||
}
|
||||
@ -382,11 +380,11 @@
|
||||
console.log('准备提交的数据:', submitData);
|
||||
// 发送数据到服务器
|
||||
$.ajax({
|
||||
url: '/login',
|
||||
url: '/admin/exlink',
|
||||
type: 'POST',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify({
|
||||
token: "test"
|
||||
data: formValues
|
||||
}),
|
||||
success: function(data) {
|
||||
if (data.status) {
|
||||
|
@ -10,7 +10,7 @@ def get_cnb_weburl(port):
|
||||
res = s.get("https://api.cnb.cool/workspace/list?branch=main&status=running").json()
|
||||
info = res["list"][0]
|
||||
urlinfo = s.get(f"https://api.cnb.cool/{info['slug']}/-/workspace/detail/{info['sn']}").json()
|
||||
|
||||
print(urlinfo)
|
||||
#re提取cnb-id
|
||||
pattern = r'cnb-[a-z0-9]+-[a-z0-9]+-\d+'
|
||||
match = re.search(pattern, urlinfo["webide"])
|
||||
|
Loading…
x
Reference in New Issue
Block a user