-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
156 lines (138 loc) · 4.34 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/mylxsw/asteria/color"
"github.com/mylxsw/asteria/formatter"
"github.com/mylxsw/asteria/level"
"github.com/mylxsw/asteria/log"
"github.com/mylxsw/asteria/writer"
"github.com/mylxsw/glacier"
"github.com/mylxsw/sync/api"
"github.com/mylxsw/sync/collector"
"github.com/mylxsw/sync/config"
"github.com/mylxsw/sync/queue"
"github.com/mylxsw/sync/rpc"
"github.com/mylxsw/sync/scheduler"
"github.com/mylxsw/sync/server"
"github.com/mylxsw/sync/storage"
"github.com/mylxsw/sync/utils"
"github.com/urfave/cli"
"github.com/urfave/cli/altsrc"
)
var Version string
var GitCommit string
func main() {
log.DefaultDynamicModuleName(true)
app := glacier.Create(fmt.Sprintf("%s (%s)", Version, GitCommit[:8]))
app.AddFlags(altsrc.NewInt64Flag(cli.Int64Flag{
Name: "file_transfer_buffer_size",
Usage: "file transfer buffer size for rpc stream",
Value: 10240,
}))
app.AddFlags(altsrc.NewStringFlag(cli.StringFlag{
Name: "rpc_listen_addr",
Usage: "GRPC server listen addr for internal server communication",
Value: ":8818",
}))
app.AddFlags(altsrc.NewStringFlag(cli.StringFlag{
Name: "rpc_token",
Usage: "GRPC access token",
Value: "",
}))
app.AddFlags(altsrc.NewStringFlag(cli.StringFlag{
Name: "api_token",
Usage: "API Token for api access control",
Value: "",
}))
app.AddFlags(altsrc.NewStringFlag(cli.StringFlag{
Name: "db",
Usage: "local database storage path",
Value: "/data/sync_db",
}))
app.AddFlags(altsrc.NewIntFlag(cli.IntFlag{
Name: "file_sync_worker_num",
Usage: "worker count for file sync",
Value: 3,
}))
app.AddFlags(altsrc.NewInt64Flag(cli.Int64Flag{
Name: "job_history_keep_size",
Usage: "job history's count to keep",
Value: 100,
}))
app.AddFlags(altsrc.NewBoolTFlag(cli.BoolTFlag{
Name: "console_color",
Usage: "log colorful for console",
}))
app.AddFlags(altsrc.NewBoolFlag(cli.BoolFlag{
Name: "use_local_dashboard",
Usage: "whether using local dashboard, this is used when development",
}))
app.AddFlags(altsrc.NewStringSliceFlag(cli.StringSliceFlag{
Name: "allow_files",
Usage: "limit files or directories can be sync",
Value: &cli.StringSlice{"/data",},
}))
app.BeforeInitialize(func(c *cli.Context) error {
log.DefaultLogFormatter(formatter.NewDefaultFormatter(c.Bool("console_color")))
return nil
})
app.Singleton(func(c *cli.Context) *config.Config {
return &config.Config{
FileTransferBufferSize: c.Int64("file_transfer_buffer_size"),
RPCListenAddr: c.String("rpc_listen_addr"),
DB: c.String("db"),
FileSyncWorkerNum: c.Int("file_sync_worker_num"),
JobHistoryKeepSize: c.Int64("job_history_keep_size"),
RPCToken: c.String("rpc_token"),
APIToken: c.String("api_token"),
UseLocalDashboard: c.Bool("use_local_dashboard"),
AllowFiles: utils.StringArrayUnique(c.StringSlice("allow_files")),
CommandTimeout: 60 * time.Second,
}
})
app.WithHttpServer(":8819")
app.Provider(&server.ServiceProvider{})
app.Provider(&rpc.ServiceProvider{})
app.Provider(&api.ServiceProvider{})
app.Provider(&storage.ServiceProvider{})
app.Provider(&queue.ServiceProvider{})
app.Provider(&scheduler.ServiceProvider{})
app.Provider(&collector.ServiceProvider{})
app.Main(func(conf *config.Config, messageFactory storage.MessageFactory) {
stackWriter := writer.NewStackWriter()
stackWriter.PushWithLevels(writer.NewStdoutWriter())
stackWriter.PushWithLevels(
NewErrorCollectorWriter(messageFactory.MessageStore(storage.MessageErrorType)),
level.Error,
level.Emergency,
level.Critical,
)
log.All().LogWriter(stackWriter)
log.WithFields(log.Fields{
"config": conf,
}).Debug("configuration")
})
if err := app.Run(os.Args); err != nil {
log.Errorf("exit: %s", err)
}
}
type ErrorCollectorWriter struct {
errorStore storage.MessageStore
}
func NewErrorCollectorWriter(errorStore storage.MessageStore) *ErrorCollectorWriter {
return &ErrorCollectorWriter{
errorStore: errorStore,
}
}
func (e *ErrorCollectorWriter) Write(le level.Level, module string, message string) error {
return e.errorStore.Record(strings.ReplaceAll(message, "\n", color.TextWrap(color.Green, "↙")))
}
func (e *ErrorCollectorWriter) ReOpen() error {
return nil
}
func (e *ErrorCollectorWriter) Close() error {
return nil
}