logapicall 플래그 추가

This commit is contained in:
2023-06-27 10:06:26 +09:00
parent 01b4782e78
commit 6d319f2fa1

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"reflect"
@ -108,6 +109,29 @@ func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string
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) {
defer func() {
s := recover()
@ -157,10 +181,8 @@ func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var operation string
if r.Method == "POST" {
operation = r.FormValue("operation")
logger.Println("api called :", userinfo["email"], r.Form)
} else {
operation = r.URL.Query().Get("operation")
logger.Println("api called :", userinfo["email"], r.URL.Query())
}
if len(operation) == 0 {
@ -176,6 +198,27 @@ func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
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{
reflect.ValueOf(h),
reflect.ValueOf(w),