This commit is contained in:
35
server/handler/fallback.go
Normal file
35
server/handler/fallback.go
Normal file
@@ -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())
|
||||||
|
}
|
@@ -1,19 +1,19 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetIncident responds to the link generated in the response to a New Snow
|
// GetIncident responds to the link generated in the response to a New Snow
|
||||||
func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
||||||
|
h.Logger.Debug("GetIncident Request received", "method", r.Method, "path", r.URL.Path)
|
||||||
// TODO
|
// TODO
|
||||||
|
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
// Expected format: /api/now/table/incident/{id}
|
// Expected format: /api/now/table/incident/{id}
|
||||||
parts := strings.Split(path, "/")
|
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" {
|
if len(parts) != 6 || parts[1] != "api" || parts[2] != "now" || parts[3] != "table" || parts[4] != "incident" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
@@ -21,5 +21,7 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
id := parts[5] // Extract {id}
|
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)
|
||||||
}
|
}
|
||||||
|
@@ -26,13 +26,14 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
|
|||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
mux.Handle("/assets/", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir))))
|
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/import/x_dusa2_itom_inc_imp", h.NewSnow)
|
||||||
mux.HandleFunc("/api/now/table/incident/", h.GetIncident)
|
mux.HandleFunc("/api/now/table/incident/", h.GetIncident)
|
||||||
mux.HandleFunc("/api/print", h.RenderIncomingTable)
|
mux.HandleFunc("/api/print", h.RenderIncomingTable)
|
||||||
|
|
||||||
// TODO - fallback route that will just echo incoming payload
|
// 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/create", h.VmCreateEvent)
|
||||||
// mux.HandleFunc("/api/event/vm/modify", h.VmModifyEvent)
|
// mux.HandleFunc("/api/event/vm/modify", h.VmModifyEvent)
|
||||||
|
@@ -55,7 +55,7 @@ func New(logger *slog.Logger, cron gocron.Scheduler, cancel context.CancelFunc,
|
|||||||
srv: srv,
|
srv: srv,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
cron: cron,
|
cron: cron,
|
||||||
//cancel: cancel,
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply any options
|
// Apply any options
|
||||||
|
Reference in New Issue
Block a user