Created
August 19, 2020 06:22
-
-
Save arran4/3f78132dd9a0dd08c322f3b06e990bb8 to your computer and use it in GitHub Desktop.
Keep on writing something like this.
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
package main | |
import ( | |
"bytes" | |
"io" | |
"log" | |
"net/http" | |
) | |
type LogHandler struct { | |
logChan chan *bytes.Buffer | |
} | |
func (l LogHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { | |
b := bytes.NewBuffer(nil) | |
request.Write(b) | |
io.Copy(b, request.Body) | |
l.logChan <- b | |
} | |
func main() { | |
logChan := make(chan *bytes.Buffer) | |
go func() { | |
for b := range logChan { | |
log.Printf("==================\n%s\n\n==============", b.String()) | |
} | |
}() | |
http.ListenAndServe(":9090", &LogHandler{logChan: logChan}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment