feat: add /server/log/{prefix} endpoint
/{prefix} is optional
Sample usage: kapow set /server/log/myapp "$msg"
Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
"net/textproto"
|
||||
"strconv"
|
||||
|
||||
"github.com/BBVA/kapow/internal/logger"
|
||||
"github.com/BBVA/kapow/internal/server/httperror"
|
||||
"github.com/BBVA/kapow/internal/server/model"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -231,3 +232,17 @@ func setResponseBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
||||
}
|
||||
h.SentBytes += n
|
||||
}
|
||||
|
||||
func setServerLog(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
||||
msg, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
handlerId := mux.Vars(r)["handlerID"]
|
||||
if prefix := mux.Vars(r)["prefix"]; prefix == "" {
|
||||
logger.L.Printf("%s %s\n", handlerId, msg)
|
||||
} else {
|
||||
logger.L.Printf("%s %s: %s\n", handlerId, prefix, msg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,10 @@ func Run(bindAddr string, wg *sync.WaitGroup) {
|
||||
{"/handlers/{handlerID}/response/cookies/{name}", "PUT", lockResponseWriter(setResponseCookies)},
|
||||
{"/handlers/{handlerID}/response/body", "PUT", lockResponseWriter(setResponseBody)},
|
||||
{"/handlers/{handlerID}/response/stream", "PUT", lockResponseWriter(setResponseBody)},
|
||||
|
||||
// logging
|
||||
{"/handlers/{handlerID}/server/log/{prefix}", "PUT", setServerLog},
|
||||
{"/handlers/{handlerID}/server/log", "PUT", setServerLog},
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", bindAddr)
|
||||
|
||||
Reference in New Issue
Block a user