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