/
client.go
96 lines (74 loc) · 1.66 KB
/
client.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
package utils
import (
"net/http"
"github.com/imroc/req/v3"
log "github.com/sirupsen/logrus"
log2 "xorm.io/xorm/log"
)
var client *req.Client
func init() {
client = req.C()
client.SetProxy(http.ProxyFromEnvironment)
if log.GetLevel() == log.DebugLevel {
client.DebugLog = true
client = client.DevMode()
}
client.SetLogger(&MyLog{})
client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
}
func GetClient() *req.Client {
return client
}
type MyLog struct {
}
func (m *MyLog) Debug(v ...interface{}) {
log.Debug(v)
}
func (m *MyLog) Error(v ...interface{}) {
log.Error(v)
}
func (m *MyLog) Info(v ...interface{}) {
log.Info(v)
}
func (m *MyLog) Infof(format string, v ...interface{}) {
log.Infof(format, v)
}
func (m *MyLog) Warn(v ...interface{}) {
log.Warn(v)
}
func (m *MyLog) Level() log2.LogLevel {
switch log.GetLevel() {
case log.InfoLevel:
return log2.LOG_INFO
case log.DebugLevel:
return log2.LOG_DEBUG
case log.WarnLevel:
return log2.LOG_WARNING
case log.ErrorLevel:
return log2.LOG_ERR
default:
return log2.LOG_UNKNOWN
}
}
func (m *MyLog) SetLevel(l log2.LogLevel) {
}
func (m *MyLog) ShowSQL(show ...bool) {
}
func (m *MyLog) IsShowSQL() bool {
return true
}
func (m MyLog) Errorf(format string, v ...interface{}) {
log.Errorf(format, v)
}
func (m MyLog) Warnf(format string, v ...interface{}) {
log.Warnf(format, v)
}
func (m MyLog) Debugf(format string, v ...interface{}) {
log.Debugf(format, v)
}
type LogWriter struct {
}
func (l *LogWriter) Write(p []byte) (n int, err error) {
log.Debugln(string(p))
return len(p), nil
}