Fix server

This commit is contained in:
Alexander Neumann 2024-04-26 19:36:47 +02:00
parent d463996ce9
commit 303dda646f
1 changed files with 13 additions and 7 deletions

View File

@ -4,7 +4,6 @@ package server
import (
"context"
"fmt"
"io/fs"
"net/http"
"sort"
"strings"
@ -109,11 +108,12 @@ func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string
}
})
http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(rw, req)
return
}
var rows []indexPageRow
for sn := range findFilteredSnapshots(req.Context(), snapshotLister, repo, &restic.SnapshotFilter{}, nil) {
rows = append(rows, indexPageRow{
@ -125,23 +125,29 @@ func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string
Paths: sn.Paths,
})
}
sort.Slice(rows, func(i, j int) bool {
return rows[i].Time.After(rows[j].Time)
})
if err := indexPage.Execute(rw, indexPageData{"Snapshots", rows}); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
})
http.HandleFunc("/style.css", func(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Set("Cache-Control", "max-age=300")
buf, err := fs.ReadFile(assets.FS, "style.css")
if err == nil {
mux.HandleFunc("/style.css", func(rw http.ResponseWriter, req *http.Request) {
buf, err := assets.FS.ReadFile("style.css")
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "error: %v", err)
fmt.Fprintf(rw, "error reading embedded style.css: %v\n", err)
return
}
rw.Header().Set("Cache-Control", "max-age=300")
rw.Header().Set("Content-Type", "text/css")
_, _ = rw.Write(buf)
})