From 4a1ccb6e13acdf3ace9463d71c4466fb76835c65 Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Tue, 23 Jul 2024 08:15:31 -0400 Subject: [PATCH] Move handler into own package --- internal/handlers/check.go | 42 ++++++++++++++++++++++++++++++++++++++ main.go | 24 ++-------------------- 2 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 internal/handlers/check.go diff --git a/internal/handlers/check.go b/internal/handlers/check.go new file mode 100644 index 0000000..d922c64 --- /dev/null +++ b/internal/handlers/check.go @@ -0,0 +1,42 @@ +package handlers + +import ( + "encoding/json" + "io" + "log/slog" + "net/http" +) + +func CheckMyWork(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + body, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, "Error reading request body", http.StatusInternalServerError) + return + } + defer r.Body.Close() + + slog.Info("Payload", "payload", string(body)) + + response := map[string]string{ + "A2": "Failed date format", + "C12": "File does not exist", + } + w.Header().Set("Content-Type", "application/json") + jsonResponse, err := json.Marshal(response) + if err != nil { + slog.Error("Error creating JSON response", "err", err) + http.Error(w, "Error creating JSON response", http.StatusInternalServerError) + return + } + + _, err = w.Write(jsonResponse) + if err != nil { + slog.Error("Error writing JSON response", "err", err) + http.Error(w, "Error writing JSON response", http.StatusInternalServerError) + } +} diff --git a/main.go b/main.go index 68ce7b4..107238f 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "encoding/csv" - "encoding/json" "flag" "fmt" "log/slog" @@ -11,6 +10,7 @@ import ( "reflect" "strings" + "github.com/lehigh-university-libraries/fabricator/internal/handlers" "github.com/lehigh-university-libraries/go-islandora/workbench" ) @@ -116,26 +116,6 @@ func readCSVWithJSONTags(filePath string) ([]map[string][]string, error) { return rows, nil } -func checkMyWork(w http.ResponseWriter, r *http.Request) { - response := map[string]string{ - "A2": "Failed date format", - "C12": "File does not exist", - } - w.Header().Set("Content-Type", "application/json") - jsonResponse, err := json.Marshal(response) - if err != nil { - slog.Error("Error creating JSON response", "err", err) - http.Error(w, "Error creating JSON response", http.StatusInternalServerError) - return - } - - _, err = w.Write(jsonResponse) - if err != nil { - slog.Error("Error writing JSON response", "err", err) - http.Error(w, "Error writing JSON response", http.StatusInternalServerError) - } -} - func main() { var serverMode bool @@ -144,7 +124,7 @@ func main() { if serverMode { // Start HTTP server - http.HandleFunc("/workbench/check", checkMyWork) + http.HandleFunc("/workbench/check", handlers.CheckMyWork) slog.Info("Starting server on :8080") if err := http.ListenAndServe(":8080", nil); err != nil {