-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
81 lines (71 loc) · 1.86 KB
/
config.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
package pixiv
import (
"net/http"
"net/url"
"os"
"github.com/minio/minio-go"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/shiena/ansicolor"
)
var (
// global config
globalConfig *viper.Viper = viper.New()
client *http.Client
myLog = logrus.New()
)
func init() {
initLog()
readConfig()
proxyConfig()
}
func initLog() {
myLog.SetOutput(ansicolor.NewAnsiColorWriter(os.Stdout))
myLog.SetLevel(logrus.DebugLevel)
myLog.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
ForceQuote:true,
TimestampFormat:"2006-01-02 15:04:05",
FullTimestamp:true,
})
}
func readConfig() {
globalConfig.SetConfigName("config")
globalConfig.SetConfigType("yaml")
globalConfig.AddConfigPath(".")
globalConfig.AddConfigPath("../")
globalConfig.AddConfigPath("./config")
err := globalConfig.ReadInConfig()
if err != nil {
myLog.WithField("place", "config").WithError(err).Fatalf("read config file %s failed", "config.yaml Or config/*.yaml")
}
}
func proxyConfig() {
proxyHost := globalConfig.GetString("proxy.Host")
proxyPort := globalConfig.GetString("proxy.Port")
rawURL := "http://" + proxyHost + ":" + proxyPort
proxyURL, err := url.Parse(rawURL)
if err != nil {
myLog.WithField("place", "config").Fatalf("URL: [%s] Parse FAILED", proxyURL)
}
trans := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client = &http.Client{
Transport: trans,
}
}
func initMinio() *minio.Client {
var err error
client, err := minio.New(
globalConfig.GetString("upload.endPoint"),
globalConfig.GetString("upload.accessKeyID"),
globalConfig.GetString("secretAccessKey"),
globalConfig.GetBool("useSSL"),
)
if err != nil {
myLog.WithField("place", "config").WithError(err).Fatalln("Fail to Create minio Client, please check your config")
}
myLog.WithField("place", "config").Infoln("minio init successfully")
return client
}