Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a web server to browse a repository (snapshots, files) #4276

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog/unreleased/pull-4276
@@ -0,0 +1,13 @@
Enhancement: Implement web server to browse snapshots

Currently the canonical way of browsing a repository's snapshots to view
or restore files is `mount`. Unfortunately `mount` depends on fuse which
is not available on all operating systems.

The new `restic serve` command presents a web interface to browse a
repository's snapshots. It allows to view and download files individually
or as a group (as a tar archive) from snapshots.

https://github.com/restic/restic/pull/4276
https://github.com/restic/restic/issues/60

108 changes: 108 additions & 0 deletions cmd/restic/cmd_serve.go
@@ -0,0 +1,108 @@
package main

import (
"context"
"fmt"
"net"
"net/http"
"time"

"github.com/spf13/cobra"

"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/server"
)

var cmdServe = &cobra.Command{
Use: "serve",
Short: "runs a web server to browse a repository",
Long: `
The serve command runs a web server to browse a repository.
`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runWebServer(cmd.Context(), serveOptions, globalOptions, args)
},
}

type ServeOptions struct {
Listen string
}

var serveOptions ServeOptions

func init() {
cmdRoot.AddCommand(cmdServe)
cmdFlags := cmdServe.Flags()
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")
}

ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock)
if err != nil {
return err
}
defer unlock()

snapshotLister, err := restic.MemorizeList(ctx, repo, restic.SnapshotFile)
if err != nil {
return err
}

bar := newIndexProgress(gopts.Quiet, gopts.JSON)
err = repo.LoadIndex(ctx, bar)
if err != nil {
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: handler,
}

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)
}()

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
}
34 changes: 34 additions & 0 deletions internal/server/assets/index.html
@@ -0,0 +1,34 @@
<html>

<head>
<link rel="stylesheet" href="/style.css">
<title>{{.Title}} :: restic</title>
</head>

<body>
<h1>{{.Title}}</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Time</th>
<th>Host</th>
<th>Tags</th>
<th>Paths</th>
</tr>
</thead>
<tbody>
{{range .Rows}}
<tr>
<td><a href="{{.Link}}">{{.ID}}</a></td>
<td>{{.Time | FormatTime}}</td>
<td>{{.Host}}</td>
<td>{{.Tags}}</td>
<td>{{.Paths}}</td>
</tr>
{{end}}
</tbody>
</table>
</body>

</html>
40 changes: 40 additions & 0 deletions internal/server/assets/style.css
@@ -0,0 +1,40 @@
h1,
h2,
h3 {
text-align: center;
margin: 0.5em;
}

table {
margin: 0 auto;
border-collapse: collapse;
}

thead th {
text-align: left;
font-weight: bold;
}

tbody.content tr:hover {
background: #eee;
}

tbody.content a.file:before {
content: '\1F4C4'
}

tbody.content a.dir:before {
content: '\1F4C1'
}

tbody.actions td {
padding: .5em;
}

table,
td,
tr,
th {
border: 1px solid black;
padding: .1em .5em;
}
51 changes: 51 additions & 0 deletions internal/server/assets/tree.html
@@ -0,0 +1,51 @@
<html>

<head>
<link rel="stylesheet" href="/style.css">
<title>{{.Title}} :: restic</title>
</head>

<body>
<h1>{{.Title}}</h1>
<form method="post">
<table>
<thead>
<tr>
<th><input type="checkbox"
onclick="document.querySelectorAll('.content input[type=checkbox]').forEach(cb => cb.checked = this.checked)">
</th>
<th>Name</th>
<th>Type</th>
<th>Size</th>
<th>Date modified</th>
</tr>
</thead>
<tbody class="content">
{{if .Parent}}<tr>
<td></td>
<td><a href="{{.Parent}}">..</a></td>
<td>parent</td>
<td></td>
<td>
</tr>{{end}}
{{range .Rows}}
<tr>
<td><input type="checkbox" name="name" value="{{.Name}}"></td>
<td><a class="{{.Type}}" href="{{.Link}}">{{.Name}}</a></td>
<td>{{.Type}}</td>
<td>{{.Size}}</td>
<td>{{.Time | FormatTime}}</td>
</td>
</tr>
{{end}}
</tbody>
<tbody class="actions">
<tr>
<td colspan="100"><button name="action" value="dump" type="submit">Download selection</button></td>
</tr>
</tbody>
</table>
</form>
</body>

</html>