Skip to content

Commit d76f715

Browse files
committed
commit
1 parent 4213f2d commit d76f715

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

admin.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package main
33
import (
44
"database/sql"
55
"fmt"
6+
"io"
67
"log"
78
"net/http"
9+
"os"
810
"slices"
911
"strconv"
1012
"strings"
@@ -268,6 +270,36 @@ func (r *Router) adminUsers(w http.ResponseWriter, req *http.Request) {
268270
}
269271
}
270272

273+
const databaseFile = "backup.db"
274+
275+
func (r *Router) adminDownloadDatabase(w http.ResponseWriter, req *http.Request) {
276+
_, err := r.db.ExecContext(req.Context(), "VACUUM")
277+
if err != nil {
278+
log.Println("Failed to vacuum database:", err)
279+
http.Error(w, "Failed to vacuum database", http.StatusInternalServerError)
280+
return
281+
}
282+
_ = os.Remove(databaseFile)
283+
_, err = r.db.ExecContext(req.Context(), "VACUUM main INTO ?", databaseFile)
284+
if err != nil {
285+
log.Println("Failed to backup database to file:", err)
286+
http.Error(w, "Failed to backup database to file", http.StatusInternalServerError)
287+
return
288+
}
289+
f, err := os.Open(databaseFile)
290+
if err != nil {
291+
log.Println("Failed to open database file:", err)
292+
http.Error(w, "Failed to open database file", http.StatusInternalServerError)
293+
return
294+
}
295+
_, err = io.Copy(w, f)
296+
if err != nil {
297+
log.Println("Failed to write database to writer:", err)
298+
http.Error(w, "Failed to write database to writer", http.StatusInternalServerError)
299+
return
300+
}
301+
}
302+
271303
func (r *Router) adminUserEdit(w http.ResponseWriter, req *http.Request) {
272304
ny, _ := time.LoadLocation("America/New_York")
273305

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ func main() {
588588
mux.Handle("/discord/callback", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(http.HandlerFunc(router.DiscordCallback))))
589589
mux.Handle("/admin", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(router.EnforceAdminMiddleware(http.HandlerFunc(router.admin)))))
590590
mux.Handle("/admin/users", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(router.EnforceAdminMiddleware(http.HandlerFunc(router.adminUsers)))))
591+
mux.Handle("/admin/auth.db", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(router.EnforceAdminMiddleware(http.HandlerFunc(router.adminDownloadDatabase)))))
591592
mux.Handle("/admin/users/{user_id}", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(router.EnforceAdminMiddleware(http.HandlerFunc(router.adminUserEdit)))))
592593
mux.Handle("/admin/vote", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(router.EnforceAdminMiddleware(http.HandlerFunc(router.adminVote)))))
593594
mux.Handle("GET /admin/vote/new", router.InjectJwtMiddleware(router.EnforceJwtMiddleware(router.EnforceAdminMiddleware(http.HandlerFunc(router.adminVoteEdit)))))

templates/admin.html.tpl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515
{{ if eq .path `/admin/vote` }}bg-gray-100{{ end }}"
1616
>Vote</a
1717
>
18+
<a
19+
href="/admin/auth.db"
20+
target="_top"
21+
class="rounded-sm px-4 py-2 hover:bg-gray-100 inline-flex justify-between
22+
{{ if eq .path `/admin/download` }}bg-gray-100{{ end }}"
23+
>
24+
<span>Database</span>
25+
<svg
26+
xmlns="http://www.w3.org/2000/svg"
27+
width="24"
28+
height="24"
29+
viewBox="0 0 24 24"
30+
fill="none"
31+
stroke="currentColor"
32+
stroke-width="2"
33+
stroke-linecap="round"
34+
stroke-linejoin="round"
35+
class="lucide lucide-download-icon lucide-download"
36+
>
37+
<path d="M12 15V3" />
38+
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
39+
<path d="m7 10 5 5 5-5" />
40+
</svg>
41+
</a
42+
>
1843
</div>
1944
<div class="flex-1 min-w-0">{{ block "admin" . }}{{ end }}</div>
2045
</div>

0 commit comments

Comments
 (0)