Compare commits

..

No commits in common. "303dda646faafa6f648d6d56eb55ec7caeca82db" and "8a5ac6dc1302673c4a368834e5ffb05f481707d0" have entirely different histories.

2 changed files with 9 additions and 52 deletions

View File

@ -2,10 +2,7 @@ package main
import (
"context"
"fmt"
"net"
"net/http"
"time"
"github.com/spf13/cobra"
@ -38,8 +35,6 @@ func init() {
cmdFlags.StringVarP(&serveOptions.Listen, "listen", "l", "localhost:3080", "set the listen host name and `address`")
}
const serverShutdownTimeout = 30 * time.Second
func runWebServer(ctx context.Context, opts ServeOptions, gopts GlobalOptions, args []string) error {
if len(args) > 0 {
return errors.Fatal("this command does not accept additional arguments")
@ -62,42 +57,10 @@ func runWebServer(ctx context.Context, opts ServeOptions, gopts GlobalOptions, a
return err
}
srv := http.Server{
BaseContext: func(l net.Listener) context.Context {
// just return the global context
return ctx
},
Handler: server.New(repo, snapshotLister, TimeFormat),
}
listener, err := net.Listen("tcp", opts.Listen)
if err != nil {
return fmt.Errorf("start listener: %v", err)
}
// wait until context is cancelled, then close listener
go func() {
<-ctx.Done()
Printf("gracefully shutting down server\n")
ctxTimeout, cancel := context.WithTimeout(context.Background(), serverShutdownTimeout)
defer cancel()
_ = srv.Shutdown(ctxTimeout)
}()
srv := server.New(repo, snapshotLister, TimeFormat)
Printf("Now serving the repository at http://%s\n", opts.Listen)
Printf("When finished, quit with Ctrl-c here.\n")
err = srv.Serve(listener)
if errors.Is(err, http.ErrServerClosed) {
err = nil
}
if err != nil {
return fmt.Errorf("serve: %v", err)
}
return nil
return http.ListenAndServe(opts.Listen, srv)
}

View File

@ -4,6 +4,7 @@ package server
import (
"context"
"fmt"
"io/fs"
"net/http"
"sort"
"strings"
@ -108,12 +109,11 @@ func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string
}
})
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
http.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,29 +125,23 @@ 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)
}
})
mux.HandleFunc("/style.css", func(rw http.ResponseWriter, req *http.Request) {
buf, err := assets.FS.ReadFile("style.css")
if err != nil {
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 {
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "error reading embedded style.css: %v\n", err)
fmt.Fprintf(rw, "error: %v", err)
return
}
rw.Header().Set("Cache-Control", "max-age=300")
rw.Header().Set("Content-Type", "text/css")
_, _ = rw.Write(buf)
})