Commit
This commit is contained in:
54
utils/http.go
Normal file
54
utils/http.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"nCovTrack-Backend/global"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ()
|
||||
|
||||
func GetWhioutHeader(url string) string {
|
||||
return GetWithHeader(url, nil)
|
||||
}
|
||||
|
||||
func GetWithHeader(url string, header map[string]string) string {
|
||||
client, err := global.GetHttpClient("")
|
||||
if err != nil {
|
||||
var logParams []interface{}
|
||||
logParams = append(logParams, "err", err)
|
||||
InitLogError("httpClient", logParams)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, url, nil)
|
||||
for k, v := range header {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
startTime := time.Now()
|
||||
res, err := client.Do(req)
|
||||
cost := time.Since(startTime)
|
||||
var logParams []interface{}
|
||||
logParams = append(logParams,
|
||||
"reqest", req,
|
||||
"cost", cost.String(),
|
||||
)
|
||||
if err != nil {
|
||||
logParams = append(logParams, "errors", err)
|
||||
UtilLogError("requestUtil", logParams...)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if cost.Seconds() > 1 {
|
||||
logParams = append(logParams, "response", res)
|
||||
UtilLogWarn("requestUtil", logParams...)
|
||||
}
|
||||
UtilLogInfo("requestUtil", logParams...)
|
||||
|
||||
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
||||
defer res.Body.Close()
|
||||
|
||||
return string(bodyBytes)
|
||||
|
||||
}
|
||||
27
utils/log.go
27
utils/log.go
@@ -14,13 +14,9 @@ func RequestLogWarn(args ...interface{}) {
|
||||
logWarn("request", args...)
|
||||
}
|
||||
|
||||
func RequestLogPanic(args ...interface{}) {
|
||||
logPanic("request", args...)
|
||||
}
|
||||
|
||||
func InitLogPanic(component string, args ...interface{}) {
|
||||
func InitLogError(component string, args ...interface{}) {
|
||||
args = append(args, "component", component)
|
||||
logPanic("initialize", args...)
|
||||
logError("initialize", args...)
|
||||
}
|
||||
|
||||
func InitLogInfo(component string, args ...interface{}) {
|
||||
@@ -28,6 +24,21 @@ func InitLogInfo(component string, args ...interface{}) {
|
||||
logInfo("initialize", args...)
|
||||
}
|
||||
|
||||
func UtilLogInfo(utilName string, args ...interface{}) {
|
||||
args = append(args, "util", utilName)
|
||||
logInfo("util", args...)
|
||||
}
|
||||
|
||||
func UtilLogError(utilName string, args ...interface{}) {
|
||||
args = append(args, "util", utilName)
|
||||
logError("util", args...)
|
||||
}
|
||||
|
||||
func UtilLogWarn(utilName string, args ...interface{}) {
|
||||
args = append(args, "util", utilName)
|
||||
logWarn("util", args...)
|
||||
}
|
||||
|
||||
func logInfo(msg string, args ...interface{}) {
|
||||
global.Logger.Infow(msg, args...)
|
||||
}
|
||||
@@ -39,7 +50,3 @@ func logError(msg string, args ...interface{}) {
|
||||
func logWarn(msg string, args ...interface{}) {
|
||||
global.Logger.Warnw(msg, args...)
|
||||
}
|
||||
|
||||
func logPanic(msg string, args ...interface{}) {
|
||||
global.Logger.Panicw("", args...)
|
||||
}
|
||||
|
||||
8
utils/map.go
Normal file
8
utils/map.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package utils
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func MapToJson(param map[string]interface{}) string {
|
||||
jsonStr, _ := json.Marshal(param)
|
||||
return string(jsonStr)
|
||||
}
|
||||
Reference in New Issue
Block a user