Skip to content

Commit

Permalink
Move handler into own package
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Jul 23, 2024
1 parent 9b1cc2c commit 4a1ccb6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
42 changes: 42 additions & 0 deletions internal/handlers/check.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
24 changes: 2 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"log/slog"
Expand All @@ -11,6 +10,7 @@ import (
"reflect"
"strings"

"github.com/lehigh-university-libraries/fabricator/internal/handlers"
"github.com/lehigh-university-libraries/go-islandora/workbench"
)

Expand Down Expand Up @@ -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

Expand All @@ -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 {
Expand Down

0 comments on commit 4a1ccb6

Please sign in to comment.