Aggiorna public/index.html

This commit is contained in:
Fabio 2026-02-20 18:59:55 +08:00
parent 8ce422c5a7
commit 90619f2d99

View file

@ -16,43 +16,49 @@
<pre id="out"></pre> <pre id="out"></pre>
<script> <script>
let BASE_URL = null;
let tok = null; let tok = null;
let db = []; let db = [];
async function loadConfig() {
const res = await fetch('/config');
const cfg = await res.json();
BASE_URL = cfg.baseUrl;
}
async function start() { async function start() {
await loadConfig();
await login(); await login();
await readDB(); await readDB();
} }
async function login() { async function login() {
const res = await fetch('/auth/login', { const res = await fetch(`${BASE_URL}/auth/login`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: 'fabio@gmail.com', password: 'master66' }) body: JSON.stringify({ email: 'fabio@gmail.com', password: 'master66' })
}); });
const data = await res.json(); const data = await res.json();
tok = data.token; tok = data.token;
} }
async function readDB() { async function readDB() {
const res = await fetch('/photos', { const res = await fetch(`${BASE_URL}/photos`, {
headers: { 'Authorization': 'Bearer ' + tok } headers: { 'Authorization': 'Bearer ' + tok }
}); });
db = await res.json(); db = await res.json();
document.getElementById('out').textContent = JSON.stringify(db, null, 2); document.getElementById('out').textContent = JSON.stringify(db, null, 2);
} }
async function scan() { async function scan() {
await fetch('/scan', { await fetch(`${BASE_URL}/scan`, {
headers: { 'Authorization': 'Bearer ' + tok } headers: { 'Authorization': 'Bearer ' + tok }
}); });
await readDB(); await readDB();
} }
async function resetDB() { async function resetDB() {
await fetch('/initDB', { await fetch(`${BASE_URL}/initDB`, {
headers: { 'Authorization': 'Bearer ' + tok } headers: { 'Authorization': 'Bearer ' + tok }
}); });
await readDB(); await readDB();
@ -60,7 +66,7 @@ async function resetDB() {
async function deleteAll() { async function deleteAll() {
for (const item of db) { for (const item of db) {
await fetch('/photos/' + item.id, { await fetch(`${BASE_URL}/photos/${item.id}`, {
method: 'DELETE', method: 'DELETE',
headers: { 'Authorization': 'Bearer ' + tok } headers: { 'Authorization': 'Bearer ' + tok }
}); });
@ -71,5 +77,6 @@ async function deleteAll() {
start(); start();
</script> </script>
</body> </body>
</html> </html>