Skip to content

Commit 75f47a2

Browse files
committed
export sync status from api
1 parent 00fdbc3 commit 75f47a2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

server.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package putiosync
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
7+
"net"
68
"net/http"
79
"time"
810

@@ -23,6 +25,10 @@ func newServer(addr string) *httpServer {
2325
m := http.NewServeMux()
2426
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("putio-sync")) })
2527
m.HandleFunc("/syncing", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(fmt.Sprintf("%v", syncing))) })
28+
m.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
29+
b, _ := json.Marshal(map[string]string{"status": syncStatus})
30+
_, _ = w.Write(b)
31+
})
2632
s := &httpServer{
2733
srv: &http.Server{
2834
Addr: addr,
@@ -39,8 +45,13 @@ func (s *httpServer) Close() {
3945
}
4046

4147
func (s *httpServer) Start() {
48+
l, err := net.Listen("tcp4", s.srv.Addr)
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
log.Infoln("Server is listening on", l.Addr().String())
4253
go func() {
43-
if err := s.srv.ListenAndServe(); err != http.ErrServerClosed {
54+
if err := s.srv.Serve(l); err != http.ErrServerClosed {
4455
log.Fatal(err)
4556
}
4657
}()

sync.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
tempDirPath string
3535
uploader *tus.Uploader
3636
syncing bool
37+
syncStatus = "Starting sync..."
3738
)
3839

3940
func Sync(ctx context.Context, config Config) error {
@@ -74,7 +75,8 @@ REPEAT_LOOP:
7475
}
7576
log.Error(err)
7677
} else {
77-
log.Infoln("Sync finished successfully")
78+
syncStatus = "Sync finished successfully"
79+
log.Infoln(syncStatus)
7880
}
7981
if cfg.Repeat == 0 {
8082
break
@@ -165,7 +167,8 @@ func syncRoots(ctx context.Context) error {
165167
syncing = true
166168
defer func() { syncing = false }()
167169
for _, job := range jobs {
168-
log.Infoln(job.String())
170+
syncStatus = job.String()
171+
log.Infoln(syncStatus)
169172
if cfg.DryRun {
170173
continue
171174
}

0 commit comments

Comments
 (0)