mirror of
https://github.com/restic/restic.git
synced 2025-01-03 05:35:43 +00:00
serve: Change assets embedding
This commit is contained in:
parent
cd720149eb
commit
c374a4e542
3 changed files with 20 additions and 12 deletions
|
@ -62,12 +62,17 @@ func runWebServer(ctx context.Context, opts ServeOptions, gopts GlobalOptions, a
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handler, err := server.New(repo, snapshotLister, TimeFormat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
srv := http.Server{
|
srv := http.Server{
|
||||||
BaseContext: func(l net.Listener) context.Context {
|
BaseContext: func(l net.Listener) context.Context {
|
||||||
// just return the global context
|
// just return the global context
|
||||||
return ctx
|
return ctx
|
||||||
},
|
},
|
||||||
Handler: server.New(repo, snapshotLister, TimeFormat),
|
Handler: handler,
|
||||||
}
|
}
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", opts.Listen)
|
listener, err := net.Listen("tcp", opts.Listen)
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
package assets
|
|
||||||
|
|
||||||
import "embed"
|
|
||||||
|
|
||||||
//go:embed *.html *.css
|
|
||||||
var FS embed.FS
|
|
|
@ -3,7 +3,9 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -13,17 +15,24 @@ import (
|
||||||
"github.com/restic/restic/internal/dump"
|
"github.com/restic/restic/internal/dump"
|
||||||
rfs "github.com/restic/restic/internal/fs"
|
rfs "github.com/restic/restic/internal/fs"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
"github.com/restic/restic/internal/server/assets"
|
|
||||||
"github.com/restic/restic/internal/walker"
|
"github.com/restic/restic/internal/walker"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed assets/*.html assets/*.css
|
||||||
|
var assets embed.FS
|
||||||
|
|
||||||
// New returns a new HTTP server.
|
// 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{
|
funcs := template.FuncMap{
|
||||||
"FormatTime": func(time time.Time) string { return time.Format(timeFormat) },
|
"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()
|
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) {
|
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 {
|
if err != nil {
|
||||||
rw.WriteHeader(http.StatusInternalServerError)
|
rw.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
||||||
|
@ -151,7 +160,7 @@ func New(repo restic.Repository, snapshotLister restic.Lister, timeFormat string
|
||||||
_, _ = rw.Write(buf)
|
_, _ = rw.Write(buf)
|
||||||
})
|
})
|
||||||
|
|
||||||
return mux
|
return mux, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileNode struct {
|
type fileNode struct {
|
||||||
|
|
Loading…
Reference in a new issue