forked from metalogical/BigFiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
128 lines (103 loc) · 2.72 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/metalogical/BigFiles/auth"
"github.com/metalogical/BigFiles/config"
"github.com/metalogical/BigFiles/server"
"github.com/sirupsen/logrus"
)
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 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
}
if err := server.Init(cfg.ValidateConfig); 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
}
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,
})
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)
}
}