2024-04-02 23:55:28 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-26 19:21:32 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2024-04-02 23:55:28 +02:00
|
|
|
"net/http"
|
2024-04-26 19:21:32 +02:00
|
|
|
"time"
|
2024-04-02 23:55:28 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2024-04-25 21:40:46 +02:00
|
|
|
"github.com/restic/restic/internal/server"
|
2024-04-02 23:55:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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`")
|
|
|
|
}
|
|
|
|
|
2024-04-26 19:21:32 +02:00
|
|
|
const serverShutdownTimeout = 30 * time.Second
|
|
|
|
|
2024-04-02 23:55:28 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-04-03 00:24:08 +02:00
|
|
|
ctx, repo, unlock, err := openWithExclusiveLock(ctx, gopts, gopts.NoLock)
|
2024-04-02 23:55:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-03 00:24:08 +02:00
|
|
|
defer unlock()
|
2024-04-02 23:55:28 +02:00
|
|
|
|
2024-04-03 00:24:08 +02:00
|
|
|
snapshotLister, err := restic.MemorizeList(ctx, repo, restic.SnapshotFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-02 23:55:28 +02:00
|
|
|
}
|
2024-04-03 00:53:38 +02:00
|
|
|
|
2024-04-03 00:24:08 +02:00
|
|
|
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
|
|
|
err = repo.LoadIndex(ctx, bar)
|
2024-04-02 23:55:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-26 19:21:32 +02:00
|
|
|
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)
|
|
|
|
}()
|
2024-04-02 23:55:28 +02:00
|
|
|
|
|
|
|
Printf("Now serving the repository at http://%s\n", opts.Listen)
|
|
|
|
Printf("When finished, quit with Ctrl-c here.\n")
|
|
|
|
|
2024-04-26 19:21:32 +02:00
|
|
|
err = srv.Serve(listener)
|
|
|
|
|
|
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("serve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-04-02 23:55:28 +02:00
|
|
|
}
|