From fe889a7a2c2357628ab6b594711347db08326048 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Mon, 24 Mar 2025 10:38:24 +1100 Subject: [PATCH] more logging --- server/handler/fallback.go | 35 +++++++++++++++++++++++++++++++++++ server/handler/getIncident.go | 8 +++++--- server/router/router.go | 3 ++- server/server.go | 2 +- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 server/handler/fallback.go diff --git a/server/handler/fallback.go b/server/handler/fallback.go new file mode 100644 index 0000000..22b3f19 --- /dev/null +++ b/server/handler/fallback.go @@ -0,0 +1,35 @@ +package handler + +import ( + "bytes" + "encoding/json" + "io" + "net/http" +) + +func (h *Handler) Fallback(w http.ResponseWriter, r *http.Request) { + h.Logger.Debug("Fallback Request received", "method", r.Method, "path", r.URL.Path) + + body, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, "Unable to read body", http.StatusBadRequest) + return + } + defer r.Body.Close() + + // Pretty-print JSON to console + var prettyJSON bytes.Buffer + if err := json.Indent(&prettyJSON, body, "", " "); err != nil { + http.Error(w, "Invalid JSON", http.StatusBadRequest) + return + } + + h.Logger.Debug("Received JSON payload", "payload", prettyJSON.String()) + //fmt.Println("Received JSON payload:") + //fmt.Println(prettyJSON.String()) + + // Set content type and write back the same JSON + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write(prettyJSON.Bytes()) +} diff --git a/server/handler/getIncident.go b/server/handler/getIncident.go index 46d2a66..cbd38e9 100644 --- a/server/handler/getIncident.go +++ b/server/handler/getIncident.go @@ -1,19 +1,19 @@ package handler import ( - "fmt" "net/http" "strings" ) // GetIncident responds to the link generated in the response to a New Snow func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) { - + h.Logger.Debug("GetIncident Request received", "method", r.Method, "path", r.URL.Path) // TODO path := r.URL.Path // Expected format: /api/now/table/incident/{id} parts := strings.Split(path, "/") + h.Logger.Debug("Request path", "parts", parts) if len(parts) != 6 || parts[1] != "api" || parts[2] != "now" || parts[3] != "table" || parts[4] != "incident" { http.NotFound(w, r) @@ -21,5 +21,7 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) { } id := parts[5] // Extract {id} - fmt.Fprintf(w, "Incident ID: %s", id) + h.Logger.Debug("GetIncident called", "id", id) + //fmt.Fprintf(w, "Incident ID: %s", id) + w.WriteHeader(http.StatusOK) } diff --git a/server/router/router.go b/server/router/router.go index 24ebe73..837d763 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -26,13 +26,14 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st mux := http.NewServeMux() mux.Handle("/assets/", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir)))) - mux.HandleFunc("/", h.Home) + mux.HandleFunc("/about", h.Home) mux.HandleFunc("/api/now/import/x_dusa2_itom_inc_imp", h.NewSnow) mux.HandleFunc("/api/now/table/incident/", h.GetIncident) mux.HandleFunc("/api/print", h.RenderIncomingTable) // TODO - fallback route that will just echo incoming payload + mux.HandleFunc("/", h.Fallback) // mux.HandleFunc("/api/event/vm/create", h.VmCreateEvent) // mux.HandleFunc("/api/event/vm/modify", h.VmModifyEvent) diff --git a/server/server.go b/server/server.go index 871d861..c839ca9 100644 --- a/server/server.go +++ b/server/server.go @@ -55,7 +55,7 @@ func New(logger *slog.Logger, cron gocron.Scheduler, cancel context.CancelFunc, srv: srv, logger: logger, cron: cron, - //cancel: cancel, + cancel: cancel, } // Apply any options