Skip to content

Commit

Permalink
serve: Change assets embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
fd0 committed Apr 28, 2024
1 parent cd72014 commit c374a4e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
7 changes: 6 additions & 1 deletion cmd/restic/cmd_serve.go
Expand Up @@ -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 {

Check warning on line 71 in cmd/restic/cmd_serve.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'l' seems to be unused, consider removing or renaming it as _ (revive)
// just return the global context
return ctx
},
Handler: server.New(repo, snapshotLister, TimeFormat),
Handler: handler,
}

listener, err := net.Listen("tcp", opts.Listen)
Expand Down
6 changes: 0 additions & 6 deletions internal/server/assets/fs.go

This file was deleted.

19 changes: 14 additions & 5 deletions internal/server/server.go
Expand Up @@ -3,7 +3,9 @@ package server

import (
"context"
"embed"
"fmt"
"io/fs"
"net/http"
"sort"
"strings"
Expand All @@ -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()

Expand Down Expand Up @@ -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) {

Check warning on line 147 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")
buf, err := fs.ReadFile(assetsFS, "style.css")
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)

Expand All @@ -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 {
Expand Down

0 comments on commit c374a4e

Please sign in to comment.