Merge branch 'master' into kd-live

This commit is contained in:
2023-06-27 10:06:42 +09:00

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"reflect" "reflect"
@ -108,6 +109,29 @@ func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string
var noauth = flagx.Bool("noauth", false, "") var noauth = flagx.Bool("noauth", false, "")
type respWriteTracker struct {
inner http.ResponseWriter
reqUrlValues url.Values
body []byte
}
func (rt *respWriteTracker) Header() http.Header {
return rt.inner.Header()
}
func (rt *respWriteTracker) Write(bt []byte) (int, error) {
rt.body = append(rt.body, bt...)
return rt.inner.Write(bt)
}
func (rt *respWriteTracker) WriteHeader(statusCode int) {
if statusCode != http.StatusOK {
logger.Println()
}
}
var logApiCallFlag = flagx.Bool("logapicall", false, "")
func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() { defer func() {
s := recover() s := recover()
@ -157,10 +181,8 @@ func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var operation string var operation string
if r.Method == "POST" { if r.Method == "POST" {
operation = r.FormValue("operation") operation = r.FormValue("operation")
logger.Println("api called :", userinfo["email"], r.Form)
} else { } else {
operation = r.URL.Query().Get("operation") operation = r.URL.Query().Get("operation")
logger.Println("api called :", userinfo["email"], r.URL.Query())
} }
if len(operation) == 0 { if len(operation) == 0 {
@ -176,6 +198,27 @@ func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
if *logApiCallFlag {
var urlvalues url.Values
if r.Method == "POST" {
urlvalues = r.Form
} else {
urlvalues = r.URL.Query()
}
tracker := &respWriteTracker{
inner: w,
reqUrlValues: urlvalues,
}
defer func() {
logger.Println("api called :", userinfo["email"], urlvalues)
logger.Println("-->", string(tracker.body))
}()
w = http.ResponseWriter(tracker)
}
args := []reflect.Value{ args := []reflect.Value{
reflect.ValueOf(h), reflect.ValueOf(h),
reflect.ValueOf(w), reflect.ValueOf(w),