-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package yar | ||
package logger | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package yar | ||
package logger | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package request | ||
|
||
import ( | ||
"context" | ||
"github.com/flyhope/go-yar/logger" | ||
"net/http" | ||
) | ||
|
||
type Handler interface { | ||
SetLog(logger.LogTrace) | ||
Do(ctx context.Context, req *http.Request) ([]byte, error) | ||
} | ||
|
||
type Abstract struct { | ||
logger.LogTrace | ||
} | ||
|
||
func (a *Abstract) SetLog(l logger.LogTrace) { | ||
a.LogTrace = l | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package request | ||
|
||
import ( | ||
"context" | ||
"github.com/flyhope/go-yar/logger" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type Http struct { | ||
Abstract | ||
Client *http.Client | ||
} | ||
|
||
func (h *Http) Do(ctx context.Context, req *http.Request) ([]byte, error) { | ||
// 发送请求 | ||
timeStart := time.Now() | ||
resp, err := h.Client.Do(req.WithContext(ctx)) | ||
|
||
// 通过接口记录跟踪日志 | ||
if h.LogTrace != nil { | ||
timeEnd := time.Now() | ||
traceData := &logger.LogTraceData{ | ||
TimeStart: timeStart, | ||
TimeEnd: timeEnd, | ||
Request: req, | ||
Err: err, | ||
} | ||
h.LogTrace.Trace(traceData) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ioutil.ReadAll(resp.Body) | ||
} |