Skip to content

Commit

Permalink
Fix server
Browse files Browse the repository at this point in the history
  • Loading branch information
fd0 committed Apr 26, 2024
1 parent d463996 commit 303dda6
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package server
import (
"context"
"fmt"
"io/fs"
"net/http"
"sort"
"strings"
Expand Down Expand Up @@ -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{
Expand All @@ -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) {

Check warning on line 138 in internal/server/server.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)
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)
})

Expand Down

0 comments on commit 303dda6

Please sign in to comment.