新增数据库初始化脚本,创建外链表和用户表,更新主程序以支持数据库连接和关闭,添加外链信息的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 os
|
||||||
import requests
|
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 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 werkzeug.routing import BaseConverter
|
||||||
from utils.login import login_quark
|
from utils.login import login_quark
|
||||||
from utils.tools import get_cnb_weburl
|
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__)
|
||||||
#app = Flask(__name__,template_folder='templates') #修改模板目录
|
#app = Flask(__name__,template_folder='templates') #修改模板目录
|
||||||
app.jinja_env.auto_reload = True
|
app.jinja_env.auto_reload = True
|
||||||
@ -33,13 +53,13 @@ def index():
|
|||||||
#return render_template("index.html")
|
#return render_template("index.html")
|
||||||
return render_template('index.html')
|
return render_template('index.html')
|
||||||
|
|
||||||
@app.route("/<phone:param>")
|
# @app.route("/<phone:param>")
|
||||||
def phone(param):
|
# def phone(param):
|
||||||
return param
|
# return param
|
||||||
|
|
||||||
@app.route("/<li:param>")
|
# @app.route("/<li:param>")
|
||||||
def uner_info(param):
|
# def uner_info(param):
|
||||||
return param
|
# return param
|
||||||
|
|
||||||
#falsk中重定向
|
#falsk中重定向
|
||||||
@app.route('/profile/')
|
@app.route('/profile/')
|
||||||
@ -78,6 +98,34 @@ def qrlink(id):
|
|||||||
def admin():
|
def admin():
|
||||||
return render_template('admin.html')
|
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__":
|
if __name__ == "__main__":
|
||||||
weburl = get_cnb_weburl(5000)
|
weburl = get_cnb_weburl(5000)
|
||||||
print("Run_url:",weburl)
|
print("Run_url:",weburl)
|
||||||
|
@ -263,7 +263,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label">有效期</label>
|
<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>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@ -357,18 +357,16 @@
|
|||||||
//添加保存外链事件
|
//添加保存外链事件
|
||||||
$('#saveExlink').on('click', function() {
|
$('#saveExlink').on('click', function() {
|
||||||
// 获取表单数据
|
// 获取表单数据
|
||||||
// 确保正确获取表单元素
|
const formData = new FormData(document.querySelector('#addExlinkModal form'));
|
||||||
const exlinkForm = document.querySelector('#addExlinkModal form');
|
const formValues = {
|
||||||
if (!exlinkForm) {
|
disk_type: formData.get('disk_type'),
|
||||||
showMessage('找不到元素', 'error');
|
account_id: formData.get('account_id'),
|
||||||
return;
|
remaining_count: formData.get('remaining_count'),
|
||||||
}
|
expiry_date: formData.get('expiry_date')
|
||||||
|
};
|
||||||
const formData = new FormData(exlinkForm);
|
|
||||||
const formValues = Object.fromEntries(formData.entries());
|
|
||||||
|
|
||||||
// 检查表单是否填写完整
|
// 检查表单是否填写完整
|
||||||
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');
|
showMessage('请填写所有必填字段!', 'error');
|
||||||
return; // 停止提交
|
return; // 停止提交
|
||||||
}
|
}
|
||||||
@ -382,11 +380,11 @@
|
|||||||
console.log('准备提交的数据:', submitData);
|
console.log('准备提交的数据:', submitData);
|
||||||
// 发送数据到服务器
|
// 发送数据到服务器
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/login',
|
url: '/admin/exlink',
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
data: JSON.stringify({
|
data: JSON.stringify({
|
||||||
token: "test"
|
data: formValues
|
||||||
}),
|
}),
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
if (data.status) {
|
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()
|
res = s.get("https://api.cnb.cool/workspace/list?branch=main&status=running").json()
|
||||||
info = res["list"][0]
|
info = res["list"][0]
|
||||||
urlinfo = s.get(f"https://api.cnb.cool/{info['slug']}/-/workspace/detail/{info['sn']}").json()
|
urlinfo = s.get(f"https://api.cnb.cool/{info['slug']}/-/workspace/detail/{info['sn']}").json()
|
||||||
|
print(urlinfo)
|
||||||
#re提取cnb-id
|
#re提取cnb-id
|
||||||
pattern = r'cnb-[a-z0-9]+-[a-z0-9]+-\d+'
|
pattern = r'cnb-[a-z0-9]+-[a-z0-9]+-\d+'
|
||||||
match = re.search(pattern, urlinfo["webide"])
|
match = re.search(pattern, urlinfo["webide"])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user