Extract LogBody() function

This commit is contained in:
Michael Hohn
2024-02-09 14:34:11 -08:00
committed by =Michael Hohn
parent 5f235371f5
commit 70768469d6
2 changed files with 14 additions and 21 deletions

View File

@@ -189,12 +189,13 @@
cd ~/local/gh-mrva cd ~/local/gh-mrva
# Build it # Build it
go clean
go build . go build .
./gh-mrva -h ./gh-mrva -h
# In log-submit-the-mrva-job.log after edit # In log-submit-the-mrva-job.log after edit
SN=11 SN=14
./gh-mrva submit --language cpp --session mirva-session-$SN \ ./gh-mrva submit --language cpp --session mirva-session-$SN \
--list mirva-list \ --list mirva-list \
--query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql >& log-$SN.out & --query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql >& log-$SN.out &

32
main.go
View File

@@ -49,24 +49,28 @@ func main() {
func LogRequestDump(req *http.Request) { func LogRequestDump(req *http.Request) {
log.Printf(">> %s %s", req.Method, req.URL) log.Printf(">> %s %s", req.Method, req.URL)
// TODO: as function
// TODO: show index for pk zip archives // TODO: show index for pk zip archives
// TODO: show json ?toc? for // TODO: show json ?toc? for
// 2024/02/08 14:54:14 >> Request body: {"repositories":["google/flatbuffers"], // 2024/02/08 14:54:14 >> Request body: {"repositories":["google/flatbuffers"],
// "language":"cpp","query_pack":"H4sIAAAA...","action_repo_ref":"main"} // "language":"cpp","query_pack":"H4sIAAAA...","action_repo_ref":"main"}
if req.Body != nil { req.Body = LogBody(req.Body, "request")
buf, err := io.ReadAll(req.Body) }
func LogBody(body io.ReadCloser, from string) io.ReadCloser {
if body != nil {
buf, err := io.ReadAll(body)
if err != nil { if err != nil {
var w http.ResponseWriter var w http.ResponseWriter
log.Printf("Error reading request body: %v", err.Error()) log.Fatalf("Error reading %s body: %v", from, err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return nil
} }
log.Printf(">> Request body: %v", string(buf)) log.Printf(">> %s body: %v", from, string(buf))
reader := io.NopCloser(bytes.NewBuffer(buf)) reader := io.NopCloser(bytes.NewBuffer(buf))
req.Body = reader return reader
} }
return body
} }
type contextKey struct { type contextKey struct {
@@ -84,17 +88,5 @@ func LogResponseDump(resp *http.Response) {
log.Printf("<< %d %s", resp.StatusCode, resp.Request.URL) log.Printf("<< %d %s", resp.StatusCode, resp.Request.URL)
} }
if resp.Body != nil { resp.Body = LogBody(resp.Body, "response")
buf, err := io.ReadAll(resp.Body)
if err != nil {
var w http.ResponseWriter
log.Printf("Error reading response body: %v", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf(">> Response body: %v", string(buf))
reader := io.NopCloser(bytes.NewBuffer(buf))
resp.Body = reader
}
} }