Skip to content

Commit 008609a

Browse files
serve configuration files on http endpoint
1 parent 74f1107 commit 008609a

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,11 @@ func (c *Config) getProfilePath(key string) string {
701701
return c.flatKey(constants.SectionConfigurationProfiles, key)
702702
}
703703

704+
// HasRemote returns true if the remote exists in the configuration
705+
func (c *Config) HasRemote(remoteName string) bool {
706+
return c.IsSet(c.flatKey(constants.SectionConfigurationRemotes, remoteName))
707+
}
708+
704709
func (c *Config) GetRemote(remoteName string) (*Remote, error) {
705710
if c.GetVersion() < Version02 {
706711
return nil, ErrNotSupportedInVersion1

serve.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,68 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"net/http"
8+
"os"
9+
"os/signal"
10+
"time"
11+
12+
"github.com/creativeprojects/clog"
713
)
814

915
func serveCommand(w io.Writer, cmdCtx commandContext) error {
1016
if len(cmdCtx.flags.resticArgs) < 2 {
1117
return fmt.Errorf("missing argument: port")
1218
}
13-
handler := http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
14-
resp.Write([]byte("Hello, World!"))
19+
handler := http.NewServeMux()
20+
handler.HandleFunc("GET /configuration/{remote}", func(resp http.ResponseWriter, req *http.Request) {
21+
remoteName := req.PathValue("remote")
22+
if !cmdCtx.config.HasRemote(remoteName) {
23+
resp.WriteHeader(http.StatusNotFound)
24+
resp.Write([]byte("remote not found"))
25+
return
26+
}
27+
remote, err := cmdCtx.config.GetRemote(remoteName)
28+
if err != nil {
29+
resp.WriteHeader(http.StatusBadRequest)
30+
resp.Write([]byte(err.Error()))
31+
return
32+
}
33+
34+
clog.Infof("sending configuration for %q", remoteName)
35+
resp.Header().Set("Content-Type", "application/x-tar")
36+
resp.WriteHeader(http.StatusOK)
37+
sendFiles(resp, remote.SendFiles)
1538
})
39+
40+
quit := make(chan os.Signal, 1)
41+
signal.Notify(quit, os.Interrupt)
42+
defer signal.Stop(quit)
43+
1644
server := &http.Server{
1745
Addr: fmt.Sprintf("localhost:%s", cmdCtx.flags.resticArgs[1]),
1846
Handler: handler,
1947
}
48+
49+
// put the shutdown code in a goroutine
50+
go func(server *http.Server, quit chan os.Signal) {
51+
<-quit
52+
53+
clog.Info("shutting down the server")
54+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
55+
defer cancel()
56+
57+
err := server.Shutdown(ctx)
58+
if err != nil {
59+
clog.Errorf("error while shutting down the server: %v", err)
60+
}
61+
62+
}(server, quit)
63+
64+
// we want to return the server error if any so we need to keep it in the main thread.
65+
clog.Infof("listening on %s", server.Addr)
2066
err := server.ListenAndServe()
2167
if err != nil && err != http.ErrServerClosed {
2268
return err

0 commit comments

Comments
 (0)