generated from pjoc-team/go-action-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (185 loc) · 5.36 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"flag"
"github.com/pjoc-team/fsync/internal/config"
"github.com/pjoc-team/fsync/pkg/fsync"
"github.com/pjoc-team/fsync/pkg/storage/api"
oss2 "github.com/pjoc-team/fsync/pkg/storage/backend/oss"
"github.com/pjoc-team/tracing/logger"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var (
confFile string
)
const (
threadPoolSize = 16
)
// Conf config struct
type Conf struct {
DataPath string `yaml:"dataPath" json:"data_path" xml:"data_path"`
Endpoint string `yaml:"endpoint" json:"endpoint" xml:"endpoint"`
Bucket string `yaml:"bucket" json:"bucket" xml:"bucket"`
SecretID string `yaml:"secretId" json:"secret_id" xml:"secret_id"`
SecretKey string `yaml:"secretKey" json:"secret_key" xml:"secret_key"`
ConfPath string `yaml:"confPath" json:"conf_path" xml:"conf_path"`
BlockSize int `yaml:"blockSize" json:"block_size" xml:"block_size"`
Debug bool `yaml:"debug" json:"debug" xml:"debug"`
InitUpload bool `yaml:"initUpload" json:"init_upload" xml:"init_upload"`
}
// Conf conf instance
var conf *Conf
func init() {
log := logger.Log()
conf = &Conf{}
confVar := "conf"
initUploadVar := "init-upload"
pathVar := "data-path"
confPathVar := "conf-path"
endpointVar := "endpoint"
bucketVar := "bucket"
secretIDVar := "secret-id"
secretKeyVar := "secret-key"
blockSizeVar := "block-size"
debugVar := "debug"
pflag.StringVar(&confFile, confVar, "", "conf file path")
pflag.BoolVar(&conf.InitUpload, initUploadVar, false, "upload all data when first time")
pflag.StringVar(&conf.DataPath, pathVar, "./data/", "upload path")
pflag.StringVar(&conf.ConfPath, confPathVar, "./conf/", "config path")
pflag.StringVar(&conf.Endpoint, endpointVar, "https://cos.ap-guangzhou.myqcloud.com", "endpoint")
pflag.StringVar(&conf.Bucket, bucketVar, "backup-1251070767", "bucket")
pflag.StringVar(&conf.SecretID, secretIDVar, "[changeSecretID]", "secretID")
pflag.StringVar(&conf.SecretKey, secretKeyVar, "[changeSecretKey]", "secretKey")
pflag.IntVar(&conf.BlockSize, blockSizeVar, 1024*1024, "block size")
pflag.BoolVar(&conf.Debug, debugVar, true, "debug oss")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err2 := viper.BindPFlags(pflag.CommandLine) // support env
if err2 != nil {
log.Fatal(err2.Error())
}
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
confFile = viper.GetString(confVar)
conf.InitUpload = viper.GetBool(initUploadVar)
conf.DataPath = viper.GetString(pathVar)
conf.ConfPath = viper.GetString(confPathVar)
conf.Endpoint = viper.GetString(endpointVar)
conf.Bucket = viper.GetString(bucketVar)
conf.SecretID = viper.GetString(secretIDVar)
conf.SecretKey = viper.GetString(secretKeyVar)
conf.BlockSize = viper.GetInt(blockSizeVar)
conf.Debug = viper.GetBool(debugVar)
log.Infof("get config: %#v", conf)
}
func main() {
rand.Seed(int64(time.Now().Nanosecond()))
ctx := context.Background()
log := logger.ContextLog(ctx)
// shutdown functions
shutdownFunctions := make([]func(context.Context), 0)
// signal
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(interrupt)
// errgroup ctx
g, ctx := errgroup.WithContext(ctx)
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
// init
conf, err := initConf()
if err != nil {
log.Fatalf("failed init file storage server, error: %v", err.Error())
}
server, err := initServer(conf)
if err != nil {
log.Fatalf("failed init file storage server, error: %v", err.Error())
}
s, err := fsync.NewServer(
ctx, conf.DataPath, server, fsync.OptionBufferSize(conf.BlockSize),
fsync.OptionConfPath(conf.ConfPath), fsync.OptionThreadPoolSize(threadPoolSize),
fsync.OptionInitUpload(conf.InitUpload),
)
if err != nil {
log.Fatalf("failed to create server, error: %v", err.Error())
}
g.Go(
func() error {
shutdownFunctions = append(
shutdownFunctions, func(ctx context.Context) {
s.Close()
},
)
err2 := s.Start()
log.Infof("fsync server done...")
cancelFunc()
return err2
},
)
select {
case <-ctx.Done():
break
case <-interrupt:
break
}
timeout, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()
for _, shutdown := range shutdownFunctions {
shutdown(timeout)
}
err = g.Wait()
if err != nil {
panic(err)
}
}
func initConf() (*Conf, error) {
if confFile != "" {
conf = &Conf{}
cs, err := config.NewConf(confFile)
if err != nil {
logger.Log().Errorf("failed to init conf", err.Error())
return nil, err
}
err = cs.UnmarshalConfig(conf)
if err != nil {
logger.Log().Errorf("failed to init conf", err.Error())
return nil, err
}
return conf, nil
}
level := logger.InfoLevel
if conf.Debug {
level = logger.DebugLevel
}
err2 := logger.MinReportCallerLevel(level)
if err2 != nil {
logger.Log().Errorf("failed to init logger", err2.Error())
}
err2 = logger.SetLevel(level)
if err2 != nil {
logger.Log().Errorf("failed to init logger", err2.Error())
}
return conf, nil
}
func initServer(conf *Conf) (api.FileStorage, error) {
oc := &oss2.Conf{
Endpoint: conf.Endpoint,
Bucket: conf.Bucket,
SecretID: conf.SecretID,
SecretKey: conf.SecretKey,
}
s, err := oss2.NewOssStorage(
oc,
conf.BlockSize,
conf.Debug,
)
return s, err
}