forked from garethgeorge/backrest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresticui.go
158 lines (134 loc) · 3.76 KB
/
resticui.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"errors"
"flag"
"net/http"
"os"
"os/signal"
"path"
"strings"
"sync"
"syscall"
rice "github.com/GeertJohan/go.rice"
"github.com/garethgeorge/resticui/internal/api"
"github.com/garethgeorge/resticui/internal/config"
"github.com/garethgeorge/resticui/internal/oplog"
"github.com/garethgeorge/resticui/internal/orchestrator"
"github.com/garethgeorge/resticui/internal/resticinstaller"
"github.com/mattn/go-colorable"
"go.etcd.io/bbolt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
installOnly = flag.Bool("install-deps", false, "Install resticui and exit")
)
func main() {
flag.Parse()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
go onterm(cancel)
resticPath, err := resticinstaller.FindOrInstallResticBinary()
if err != nil {
zap.S().Fatalf("Error finding or installing restic: %v", err)
}
if *installOnly {
return
}
configStore := createConfigProvider()
cfg, err := configStore.Get()
if err != nil {
zap.S().Fatalf("Error loading config: %v", err)
}
var wg sync.WaitGroup
// Configure the HTTP mux
mux := http.NewServeMux()
if box, err := rice.FindBox("webui/dist"); err == nil {
mux.Handle("/", http.FileServer(box.HTTPBox()))
} else {
zap.S().Warnf("Error loading static assets, not serving UI: %v", err)
}
// Create and serve API server
oplogFile := path.Join(config.DataDir(), "oplog.boltdb")
oplog, err := oplog.NewOpLog(oplogFile)
if err != nil {
if !errors.Is(err, bbolt.ErrTimeout) {
zap.S().Fatalf("Timeout while waiting to open database, is the database open elsewhere?")
}
zap.S().Warnf("Operation log may be corrupted, if errors recur delete the file %q and restart. Your backups stored in your repos are safe.", oplogFile)
zap.S().Fatalf("Error creating oplog : %v", err)
}
defer oplog.Close()
orchestrator, err := orchestrator.NewOrchestrator(resticPath, cfg, oplog)
if err != nil {
zap.S().Fatalf("Error creating orchestrator: %v", err)
}
// Start orchestration loop. Only exits when ctx is cancelled.
go orchestrator.Run(ctx)
apiServer := api.NewServer(
configStore,
orchestrator, // TODO: eliminate default config
oplog,
)
// Serve the API
wg.Add(1)
go func() {
defer wg.Done()
err := api.ServeAPI(ctx, apiServer, mux)
if err != nil {
zap.S().Fatal("Error serving API", zap.Error(err))
}
cancel() // cancel the context when the API server exits (e.g. on fatal error)
}()
// Serve the HTTP gateway
server := &http.Server{
Addr: config.BindAddress(),
Handler: mux,
}
wg.Add(1)
go func() {
defer wg.Done()
zap.S().Infof("Starting web server %v", server.Addr)
go func() {
<-ctx.Done()
server.Shutdown(context.Background())
}()
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
zap.L().Error("Error starting server", zap.Error(err))
}
zap.L().Info("HTTP gateway shutdown")
cancel() // cancel the context when the HTTP server exits (e.g. on fatal error)
}()
wg.Wait()
}
func init() {
zap.ReplaceGlobals(zap.Must(zap.NewProduction()))
if !strings.HasPrefix(os.Getenv("ENV"), "prod") {
c := zap.NewDevelopmentEncoderConfig()
c.EncodeLevel = zapcore.CapitalColorLevelEncoder
c.EncodeTime = zapcore.ISO8601TimeEncoder
l := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(c),
zapcore.AddSync(colorable.NewColorableStdout()),
zapcore.DebugLevel,
))
zap.ReplaceGlobals(l)
}
}
func createConfigProvider() config.ConfigStore {
return &config.CachingValidatingStore{
ConfigStore: &config.JsonFileStore{Path: config.ConfigFilePath()},
}
}
func onterm(callback func()) {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt, syscall.SIGTERM)
<-sigchan
callback()
}
func findResticBin() {
resticBin := config.ResticBinPath()
if resticBin != "" {
}
}