partial deployment
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
export async function load({ fetch }) {
|
||||
const res = await fetch('/api/prefixes');
|
||||
const check_res = await fetch('/api');
|
||||
const check_json = await check_res.json();
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
return { prefixes: data, status: "Prefixes fetched successfully." }
|
||||
return { prefixes: data, status: check_json.detail }
|
||||
} else {
|
||||
return { prefixes: [], status: "Error fetching prefixes."}
|
||||
return { prefixes: [], status: check_json.detail }
|
||||
}
|
||||
}
|
||||
@@ -64,10 +64,15 @@
|
||||
<div><h2>Admin Mode:</h2></div>
|
||||
<div class="flex-row {current_prefix.color}">
|
||||
<a href="/prefixes" target="_blank" class="styled">Prefix Editor</a>
|
||||
<a href="/backuprestore" target="_blank" class="styled">Backup/Restore</a>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="status tb-margin">
|
||||
{data.status}
|
||||
</div>
|
||||
|
||||
<div class="annotation">
|
||||
<p>Ticket Auction Manager 3 by Dilan Gilluly</p>
|
||||
</div>
|
||||
|
||||
18
webapp/src/routes/api/+server.js
Normal file
18
webapp/src/routes/api/+server.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { env } from "$env/dynamic/private";
|
||||
|
||||
export async function GET() {
|
||||
if (env.TAM3_REMOTE) {
|
||||
const res = await fetch(`${env.TAM3_REMOTE}/api/?api_key=${env.TAM3_REMOTE_KEY}`);
|
||||
if (!res.ok) {
|
||||
return new Response(JSON.stringify({detail: "Server setup but is getting an error. Check TAM3_REMOTE and TAM3_REMOTE_KEY env variables."}))
|
||||
};
|
||||
const data = await res.json();
|
||||
if (data.auth) {
|
||||
return new Response(JSON.stringify({detail: "Remote connection is successful."}))
|
||||
} else {
|
||||
return new Response(JSON.stringify({detail: "Server is connecting but the api key isn't checking out."}))
|
||||
}
|
||||
} else {
|
||||
return new Response(JSON.stringify({detail: "Operating in offline mode."}))
|
||||
}
|
||||
}
|
||||
9
webapp/src/routes/api/backuprestore/local/+server.js
Normal file
9
webapp/src/routes/api/backuprestore/local/+server.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { db } from "$lib/server/db";
|
||||
import { prefixes, tickets, baskets } from "$lib/server/db/schema";
|
||||
|
||||
export async function GET() {
|
||||
const p_data = await db.select().from(prefixes);
|
||||
const t_data = await db.select().from(tickets);
|
||||
const b_data = await db.select().from(baskets);
|
||||
return new Response(JSON.stringify({prefixes: p_data, tickets: t_data, baskets: b_data}), {status: 200, statusText: "Loaded backup file successfully."})
|
||||
}
|
||||
78
webapp/src/routes/backuprestore/+page.svelte
Normal file
78
webapp/src/routes/backuprestore/+page.svelte
Normal file
@@ -0,0 +1,78 @@
|
||||
<script>
|
||||
import { browser } from "$app/environment";
|
||||
|
||||
let upload_file = $state();
|
||||
let contents = $state("");
|
||||
let results = $state("");
|
||||
|
||||
function setResult(new_result) {
|
||||
results = new_result;
|
||||
setTimeout(() => results = "", 10000);
|
||||
}
|
||||
|
||||
async function downloadBackupFile(target) {
|
||||
let fetch_url;
|
||||
if (target === "local") {
|
||||
fetch_url = "/api/backuprestore/local";
|
||||
} else if (target === "remote") {
|
||||
fetch_url = "/api/backuprestore"
|
||||
};
|
||||
const now = new Date()
|
||||
const date = {
|
||||
year: now.getFullYear(),
|
||||
month: String(now.getMonth()+1).padStart(2, '0'),
|
||||
day: String(now.getDate()).padStart(2, '0'),
|
||||
hour: String(now.getHours()).padStart(2, '0'),
|
||||
minutes: String(now.getMinutes()).padStart(2, '0')
|
||||
}
|
||||
const res = await fetch(fetch_url);
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
const jsonString = JSON.stringify(data); // Pretty print JSON
|
||||
const blob = new Blob([jsonString], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `TAM3_${date.year}-${date.month}-${date.day} ${date.hour}-${date.minutes}.json`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
async function fileUpload() {
|
||||
const reader = new FileReader();
|
||||
reader.readAsText(upload_file[0], 'utf-8');
|
||||
reader.onload = function(e) {
|
||||
contents = String(e.target.result)
|
||||
};
|
||||
setTimeout(() => {
|
||||
fetch('/api/backuprestore', {body: contents, method: 'POST', headers: {'Content-Type': 'application/json'}})
|
||||
.then(() => {setResult("File was sent. Check to see if your data exists now.")})
|
||||
.catch(() => {setResult("Error sending file.")})
|
||||
}, 100)
|
||||
}
|
||||
if (browser) {
|
||||
document.title = "TAM3 - Backup and Restore"
|
||||
}
|
||||
</script>
|
||||
|
||||
<h1>TAM3 Backup and Restore</h1>
|
||||
<div>
|
||||
<h2>Backup</h2>
|
||||
<div class="flex-row">
|
||||
<button class="styled" onclick={() => downloadBackupFile("remote")}>Download Backup from Remote (if setup)</button>
|
||||
<button class="styled" onclick={() => downloadBackupFile("local")}>Download Backup from Local Only</button>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h2>Restore</h2>
|
||||
<div class="flex-row">
|
||||
<input type="file" accept=".json" bind:files={upload_file}>
|
||||
<button class="styled" onclick={fileUpload}>Upload</button>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
{results}
|
||||
</div>
|
||||
Reference in New Issue
Block a user