forked from metalogical/BigFiles
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (126 loc) · 3.4 KB
/
main.go
File metadata and controls
154 lines (126 loc) · 3.4 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
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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
"github.com/sirupsen/logrus"
"github.com/metalogical/BigFiles/auth"
"github.com/metalogical/BigFiles/config"
"github.com/metalogical/BigFiles/db"
"github.com/metalogical/BigFiles/server"
)
type options struct {
service ServiceOptions
enableDebug bool
}
type ServiceOptions struct {
ConfigFile string
RemoveCfg bool
}
// Validate checks if the ServiceOptions are valid.
// It returns an error if the config file is missing.
func (o *ServiceOptions) Validate() error {
if o.ConfigFile == "" {
return fmt.Errorf("missing config-file")
}
return nil
}
// AddFlags adds flags for ServiceOptions to the provided FlagSet.
func (o *ServiceOptions) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&o.ConfigFile, "config-file", "", "Path to config file.")
fs.BoolVar(&o.RemoveCfg, "rm-cfg", false, "whether remove the cfg file after initialized .")
}
// Validate validates the options and returns an error if any validation fails.
func (o *options) Validate() error {
return o.service.Validate()
}
func gatherOptions(fs *flag.FlagSet, args ...string) (options, error) {
var o options
o.service.AddFlags(fs)
fs.BoolVar(
&o.enableDebug, "enable_debug", false, "whether to enable debug model.",
)
err := fs.Parse(args)
return o, err
}
func initConfig(cfg *config.Config) {
if err := server.Init(cfg); err != nil {
logrus.Errorf("load ValidateConfig, err:%s", err.Error())
return
}
if err := auth.Init(cfg); err != nil {
logrus.Errorf("load gitee config, err:%s", err.Error())
return
}
if err := db.Init(cfg.DBConfig); err != nil {
logrus.Errorf("init database config, err:%s", err.Error())
return
}
}
func initObsClient(cfg *config.Config) {
var err error
server.ObsClient, err = obs.New(cfg.ObsAccessKeyId, cfg.ObsSecretAccessKey,
cfg.ObsRegion, obs.WithSignature(obs.SignatureObs))
server.Bucket = cfg.LfsBucket
server.Prefit = cfg.Prefix
if err != nil {
logrus.Errorf("failed to initialize OBS client: %v", err.Error())
return
}
}
func main() {
o, err := gatherOptions(
flag.NewFlagSet(os.Args[0], flag.ExitOnError),
os.Args[1:]...,
)
if err != nil {
logrus.Errorf("new options failed, err:%s", err.Error())
return
}
if err := o.Validate(); err != nil {
logrus.Errorf("Invalid options, err:%s", err.Error())
return
}
if o.enableDebug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("debug enable.")
}
//cfg
cfg := new(config.Config)
if err := config.LoadConfig(o.service.ConfigFile, cfg, o.service.RemoveCfg); err != nil {
logrus.Errorf("load config, err:%s", err.Error())
return
}
initObsClient(cfg)
initConfig(cfg)
s, err := server.New(server.Options{
Prefix: cfg.Prefix,
Bucket: cfg.LfsBucket,
Endpoint: cfg.ObsRegion,
CdnDomain: cfg.CdnDomain,
AccessKeyID: cfg.ObsAccessKeyId,
S3Accelerate: true,
IsAuthorized: auth.GiteeAuth(),
SecretAccessKey: cfg.ObsSecretAccessKey,
})
go server.StartScheduledTask()
go server.ScheduledCheckOidAndFileName()
srv := &http.Server{
Addr: "0.0.0.0:5000",
Handler: s,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
}
if err != nil {
log.Fatalln(err)
}
log.Println("serving on http://0.0.0.0:5000 ...")
if err := srv.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}