-
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.
* response writer interface update * mod wrap method * add hlog package for logging at the http layer
- Loading branch information
Showing
3 changed files
with
153 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package hlog | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
// limitBuffer is used to pipe response body information from the | ||
// response writer to a certain limit amount. The idea is to read | ||
// a portion of the response body such as an error response so we | ||
// may log it. | ||
type limitBuffer struct { | ||
*bytes.Buffer | ||
limit int | ||
} | ||
|
||
func newLimitBuffer(size int) io.ReadWriter { | ||
return limitBuffer{ | ||
Buffer: bytes.NewBuffer(make([]byte, 0, size)), | ||
limit: size, | ||
} | ||
} | ||
|
||
func (b limitBuffer) Write(p []byte) (n int, err error) { | ||
if b.Buffer.Len() >= b.limit { | ||
return len(p), nil | ||
} | ||
limit := b.limit | ||
if len(p) < limit { | ||
limit = len(p) | ||
} | ||
return b.Buffer.Write(p[:limit]) | ||
} | ||
|
||
func (b limitBuffer) Read(p []byte) (n int, err error) { | ||
return b.Buffer.Read(p) | ||
} |
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