-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestic-api.go
More file actions
56 lines (48 loc) · 1.12 KB
/
Copy pathrestic-api.go
File metadata and controls
56 lines (48 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"io"
"net/http"
"os"
"os/exec"
)
var (
BUF_LEN = 1024
)
func handler(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("restic", "-q", "--json", "cat", "blob", "a9083042452d72a1e36cdc3116c48d1e3330b572b0c7a6382b926a52f5dc1b59")
// cmd := exec.Command("restic", "-q", "--json", "snapshots")
// cmd := exec.Command("restic", "-q", "--json", "ls", "01380af8")
env := os.Environ()
env = append(env, "RESTIC_PASSWORD=test")
env = append(env, "RESTIC_REPOSITORY=/tmp/backup")
cmd.Env = env
pipeReader, pipeWriter := io.Pipe()
cmd.Stdout = pipeWriter
cmd.Stderr = pipeWriter
go writeCmdOutput(w, pipeReader)
cmd.Run()
pipeWriter.Close()
}
func writeCmdOutput(res http.ResponseWriter, pipeReader *io.PipeReader) {
buffer := make([]byte, BUF_LEN)
for {
n, err := pipeReader.Read(buffer)
if err != nil {
pipeReader.Close()
break
}
data := buffer[0:n]
res.Write(data)
if f, ok := res.(http.Flusher); ok {
f.Flush()
}
//reset buffer
for i := 0; i < n; i++ {
buffer[i] = 0
}
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}