serve: Change assets embedding

This commit is contained in:
Alexander Neumann 2024-04-28 22:10:31 +02:00
parent cd720149eb
commit c374a4e542
3 changed files with 20 additions and 12 deletions

View File

@ -62,12 +62,17 @@ func runWebServer(ctx context.Context, opts ServeOptions, gopts GlobalOptions, a
return err
}
handler, err := server.New(repo, snapshotLister, TimeFormat)
if err != nil {
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),
Handler: handler,
}
listener, err := net.Listen("tcp", opts.Listen)

View File

@ -1,6 +0,0 @@
package assets
import "embed"
//go:embed *.html *.css
var FS embed.FS

View File

@ -3,7 +3,9 @@ package server
import (
"context"
"embed"
"fmt"
"io/fs"
"net/http"
"sort"
"strings"
@ -13,17 +15,24 @@ import (
"github.com/restic/restic/internal/dump"
rfs "github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/server/assets"
"github.com/restic/restic/internal/walker"
)
//go:embed assets/*.html assets/*.css
var assets embed.FS
// New returns a new HTTP server.
func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string) http.Handler {
func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string) (http.Handler, error) {
assetsFS, err := fs.Sub(assets, "assets")
if err != nil {
return nil, fmt.Errorf("derive subdir fs for assets: %w", err)
}
funcs := template.FuncMap{
"FormatTime": func(time time.Time) string { return time.Format(timeFormat) },
}
templates := template.Must(template.New("").Funcs(funcs).ParseFS(assets.FS, "*.html"))
templates := template.Must(template.New("").Funcs(funcs).ParseFS(assetsFS, "*.html"))
mux := http.NewServeMux()
@ -136,7 +145,7 @@ func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string
})
mux.HandleFunc("/style.css", func(rw http.ResponseWriter, req *http.Request) {
buf, err := assets.FS.ReadFile("style.css")
buf, err := fs.ReadFile(assetsFS, "style.css")
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
@ -151,7 +160,7 @@ func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string
_, _ = rw.Write(buf)
})
return mux
return mux, nil
}
type fileNode struct {