Skip to content

Commit 00c6601

Browse files
committed
改為前端取得資料
1 parent 4424336 commit 00c6601

File tree

8 files changed

+160
-109
lines changed

8 files changed

+160
-109
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ setting.json
22
test.py
33
key.txt
44
api.py
5+
sus.json
6+
sus.html
7+
sus.py

app.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,17 @@ def w_log(text):
180180
f.write(text)
181181

182182

183-
@app.route("/work")
184-
def work():
185-
print(add_work)
186-
return redirect(f"/")
187183

184+
@app.route("/api/servers")
185+
def web_get_user_server():
186+
access_token = session.get("access_token")
187+
188+
if not access_token:
189+
return redirect(f"/")
190+
191+
bearer_client = APIClient(access_token, bearer=True)
192+
current_user = bearer_client.users.get_current_user()
193+
return jsonify(get_user_server(current_user))
188194

189195
@app.route("/rc")
190196
def rc():
@@ -203,12 +209,12 @@ def home():
203209
bearer_client = APIClient(access_token, bearer=True)
204210
current_user = bearer_client.users.get_current_user()
205211

206-
get = get_user_server(current_user)
207212
if not request.values.get("new") == None:
208213
new = request.values.get("new")
209-
return render_template("index.html", pas=new, resource=get["resource"], user=current_user, server=get["server"], now=get["now"])
214+
return render_template("index.html", pas=new, user=current_user)
210215
else:
211-
return render_template("index.html", resource=get["resource"], user=current_user, server=get["server"], now=get["now"])
216+
return render_template("index.html",user=current_user)
217+
#return render_template("index.html", resource=get["resource"], user=current_user, server=get["server"], now=get["now"])
212218

213219

214220
@app.route("/server/add", methods=["GET", "POST"])
@@ -352,7 +358,6 @@ def edit(id):
352358
udata = json.load(f)
353359
bearer_client = APIClient(access_token, bearer=True)
354360
current_user = bearer_client.users.get_current_user()
355-
get = get_user_server(current_user)
356361
key = config["pterodactyl"]["key"]
357362
url = f'{config["pterodactyl"]["url"]}api/application/servers/{id}'
358363
headers = {
@@ -370,6 +375,7 @@ def edit(id):
370375
if not response.json()["attributes"]["user"] == udata[str(current_user.id)]["id"]:
371376
return redirect(f"/")
372377
if request.method == "POST":
378+
get=get_user_server(current_user)
373379
resource = get["resource"]
374380
now = get["now"]
375381
if int(request.form["cpu"]) > resource["cpu"]-now["cpu"]+response.json()["attributes"]["limits"]["cpu"] or int(request.form["cpu"]) == 0:
@@ -421,9 +427,9 @@ def edit(id):
421427

422428
if not request.values.get("error") == None:
423429
error = request.values.get("error")
424-
return render_template("edit.html", name=name, resource=get["resource"], user=current_user, server=get["server"], now=get["now"], error=error)
430+
return render_template("edit.html", name=name, user=current_user, error=error)
425431
else:
426-
return render_template("edit.html", name=name, resource=get["resource"], user=current_user, server=get["server"], now=get["now"])
432+
return render_template("edit.html", name=name, user=current_user)
427433

428434

429435
@app.route("/rpa")
@@ -505,12 +511,10 @@ def shop():
505511

506512
bearer_client = APIClient(access_token, bearer=True)
507513
current_user = bearer_client.users.get_current_user()
508-
509-
get = get_user_server(current_user)
510514
with open(f"data/user.json", "r")as f:
511515
data = json.load(f)
512516
money = data[str(current_user.id)]["money"]
513-
return render_template("shop.html", money=money, shop=config["shop"], resource=get["resource"], user=current_user, server=get["server"], now=get["now"])
517+
return render_template("shop.html", money=money, shop=config["shop"], user=current_user)
514518

515519

516520
@app.route("/shop/<mode>", methods=["POST"])
@@ -522,8 +526,6 @@ def shopmode(mode):
522526

523527
bearer_client = APIClient(access_token, bearer=True)
524528
current_user = bearer_client.users.get_current_user()
525-
526-
get = get_user_server(current_user)
527529
with open(f"data/user.json", "r")as f:
528530
data = json.load(f)
529531
nmode = mode
@@ -548,7 +550,6 @@ def codes():
548550
return redirect(f"/")
549551
bearer_client = APIClient(access_token, bearer=True)
550552
current_user = bearer_client.users.get_current_user()
551-
get = get_user_server(current_user)
552553
if request.method == "POST":
553554
with open(f"data/code.json", "r")as f:
554555
data = json.load(f)
@@ -571,7 +572,7 @@ def codes():
571572
return redirect(f"/shop")
572573
except:
573574
return redirect(f"/code?error=錯誤的代碼")
574-
return render_template("code.html", shop=config["shop"], resource=get["resource"], user=current_user, server=get["server"], now=get["now"])
575+
return render_template("code.html", shop=config["shop"], user=current_user)
575576

576577

577578
@app.route("/api/code", methods=["POST"])

static/assets/js/get_server.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const url = '/api/servers';
2+
fetch(url)
3+
.then(response => response.json())
4+
.then(jsonData => {
5+
const memoryTag = document.getElementById('memory');
6+
const memorytextToUpdate = `${jsonData.now.memory}/${jsonData.resource.memory} MB`;
7+
memoryTag.textContent = memorytextToUpdate;
8+
9+
const cpuTag = document.getElementById('cpu');
10+
const cputextToUpdate = `${jsonData.now.cpu}/${jsonData.resource.cpu}%`;
11+
cpuTag.textContent = cputextToUpdate;
12+
13+
const diskTag = document.getElementById('disk');
14+
const disktextToUpdate = `${jsonData.now.disk}/${jsonData.resource.disk} MB`;
15+
diskTag.textContent = disktextToUpdate;
16+
17+
const serversTag = document.getElementById('servers');
18+
const serverstextToUpdate = `${jsonData.now.servers}/${jsonData.resource.servers}`;
19+
serversTag.textContent = serverstextToUpdate;
20+
21+
// 获取表格的tbody元素
22+
const serverTableBody = document.getElementById('serverTableBody');
23+
24+
// 遍历服务器对象并动态生成HTML代码
25+
for (const serverId in jsonData.server) {
26+
const server = jsonData.server[serverId];
27+
28+
// 创建一个新的<tr>元素
29+
const row = document.createElement('tr');
30+
row.classList.add('text-gray-700', 'dark:text-gray-400');
31+
32+
// 创建<td>元素并添加内容
33+
const nameCell = document.createElement('td');
34+
nameCell.classList.add('px-4', 'py-3');
35+
nameCell.innerHTML = `
36+
<div class="flex items-center text-sm">
37+
<div>
38+
<p class="font-semibold">${server.name}</p>
39+
<p class="text-xs text-gray-600 dark:text-gray-400">${server.description}</p>
40+
</div>
41+
</div>
42+
`;
43+
row.appendChild(nameCell);
44+
45+
const cpuCell = document.createElement('td');
46+
cpuCell.classList.add('px-4', 'py-3', 'text-sm');
47+
cpuCell.textContent = `${server.cpu}%`;
48+
row.appendChild(cpuCell);
49+
50+
const memoryCell = document.createElement('td');
51+
memoryCell.classList.add('px-4', 'py-3', 'text-sm');
52+
memoryCell.textContent = `${server.memory}MB`;
53+
row.appendChild(memoryCell);
54+
55+
const diskCell = document.createElement('td');
56+
diskCell.classList.add('px-4', 'py-3', 'text-sm');
57+
diskCell.textContent = `${server.disk}MB`;
58+
row.appendChild(diskCell);
59+
60+
const actionCell = document.createElement('td');
61+
actionCell.classList.add('px-4', 'py-3', 'text-xs');
62+
actionCell.innerHTML = `
63+
<a href='${server.url}'>
64+
<span class="px-2 py-1 font-semibold leading-tight text-green-700 bg-green-100 rounded-full dark:bg-green-700 dark:text-green-100">
65+
查看
66+
</span>
67+
</a>
68+
<a href='/server/edit/${server.id}'>
69+
<span class="px-2 py-1 font-semibold leading-tight text-gray-700 bg-gray-100 rounded-full dark:text-gray-100 dark:bg-gray-700">
70+
編輯
71+
</span>
72+
</a>
73+
<a href='/server/del/${server.id}'>
74+
<span class="px-2 py-1 font-semibold leading-tight text-red-700 bg-red-100 rounded-full dark:text-red-100 dark:bg-red-700">
75+
刪除
76+
</span>
77+
</a>
78+
`;
79+
row.appendChild(actionCell);
80+
81+
// 将新的<tr>元素添加到表格中
82+
serverTableBody.appendChild(row);
83+
}
84+
})

templates/add.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js" defer></script>
1414
<script src="/static/assets/js/charts-lines.js" defer></script>
1515
<script src="/static/assets/js/charts-pie.js" defer></script>
16+
<script src="/static/assets/js/get_server.js" defer></script>
1617
</head>
1718

1819
<body>
@@ -310,8 +311,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
310311
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
311312
CPU
312313
</p>
313-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
314-
{{now["cpu"]}}/{{resource["cpu"]}}%
314+
<p id="cpu" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
315+
Loading
315316
</p>
316317
</div>
317318
</div>
@@ -336,8 +337,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
336337
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
337338
記憶體
338339
</p>
339-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
340-
{{now["memory"]}}/{{resource["memory"]}}MB
340+
<p id="memory" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
341+
Loading
341342
</p>
342343
</div>
343344
</div>
@@ -369,8 +370,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
369370
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
370371
空間
371372
</p>
372-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
373-
{{now["disk"]}}/{{resource["disk"]}}MB
373+
<p id="disk" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
374+
Loading
374375
</p>
375376
</div>
376377
</div>
@@ -394,8 +395,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
394395
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
395396
數量
396397
</p>
397-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
398-
{{now["servers"]}}/{{resource["servers"]}}個
398+
<p id="servers" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
399+
Loading
399400
</p>
400401
</div>
401402
</div>

templates/code.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js" defer></script>
1414
<script src="/static/assets/js/charts-lines.js" defer></script>
1515
<script src="/static/assets/js/charts-pie.js" defer></script>
16+
<script src="/static/assets/js/get_server.js" defer></script>
1617
</head>
1718

1819
<body>
@@ -313,8 +314,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
313314
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
314315
CPU
315316
</p>
316-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
317-
{{now["cpu"]}}/{{resource["cpu"]}}%
317+
<p id="cpu" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
318+
Loading
318319
</p>
319320
</div>
320321
</div>
@@ -339,8 +340,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
339340
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
340341
記憶體
341342
</p>
342-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
343-
{{now["memory"]}}/{{resource["memory"]}}MB
343+
<p id="memory" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
344+
Loading
344345
</p>
345346
</div>
346347
</div>
@@ -372,8 +373,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
372373
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
373374
空間
374375
</p>
375-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
376-
{{now["disk"]}}/{{resource["disk"]}}MB
376+
<p id="disk" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
377+
Loading
377378
</p>
378379
</div>
379380
</div>
@@ -397,8 +398,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
397398
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
398399
數量
399400
</p>
400-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
401-
{{now["servers"]}}/{{resource["servers"]}}個
401+
<p id="servers" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
402+
Loading
402403
</p>
403404
</div>
404405
</div>

templates/edit.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js" defer></script>
1414
<script src="/static/assets/js/charts-lines.js" defer></script>
1515
<script src="/static/assets/js/charts-pie.js" defer></script>
16+
<script src="/static/assets/js/get_server.js" defer></script>
1617
</head>
1718

1819
<body>
@@ -308,8 +309,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
308309
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
309310
CPU
310311
</p>
311-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
312-
{{now["cpu"]}}/{{resource["cpu"]}}%
312+
<p id="cpu" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
313+
Loading
313314
</p>
314315
</div>
315316
</div>
@@ -334,8 +335,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
334335
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
335336
記憶體
336337
</p>
337-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
338-
{{now["memory"]}}/{{resource["memory"]}}MB
338+
<p id="memory" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
339+
Loading
339340
</p>
340341
</div>
341342
</div>
@@ -367,8 +368,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
367368
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
368369
空間
369370
</p>
370-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
371-
{{now["disk"]}}/{{resource["disk"]}}MB
371+
<p id="disk" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
372+
Loading
372373
</p>
373374
</div>
374375
</div>
@@ -392,8 +393,8 @@ <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
392393
<p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
393394
數量
394395
</p>
395-
<p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
396-
{{now["servers"]}}/{{resource["servers"]}}個
396+
<p id="servers" class="text-lg font-semibold text-gray-700 dark:text-gray-200">
397+
Loading
397398
</p>
398399
</div>
399400
</div>
@@ -424,6 +425,7 @@ <h4 class="mb-4 text-lg font-semibold text-gray-600 dark:text-gray-300">
424425
class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
425426
placeholder="512" />
426427
</label>
428+
427429

428430
<label class="block text-sm">
429431
<span class="text-gray-700 dark:text-gray-400">空間</span>

0 commit comments

Comments
 (0)