From 40ef1e3dc5dd5a40a5c04940118943dc216afbdf Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Tue, 21 May 2024 19:06:30 +0100 Subject: [PATCH] Simple change for the "error handling" --- faxSrc/assets/htmx/editor.html | 12 ++++++++++-- faxSrc/controllers/sendFax.go | 18 ++++++++++-------- faxSrc/models/authentication.go | 3 +++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/faxSrc/assets/htmx/editor.html b/faxSrc/assets/htmx/editor.html index 0126264..f07a62e 100644 --- a/faxSrc/assets/htmx/editor.html +++ b/faxSrc/assets/htmx/editor.html @@ -15,7 +15,7 @@
-
->
+
@@ -52,7 +52,15 @@ } }) .then(data => { - console.log("Success:", data); + const response = JSON.parse(data) + + console.log(data) + var elem = document.getElementById("req-result") + if (response["state"] === "success") { + elem.innerHTML = "-> Fax sent successfully <-" + } else { + elem.innerHTML = "-> Fax failed to send <-" + } }) .catch(error => { console.error(error); diff --git a/faxSrc/controllers/sendFax.go b/faxSrc/controllers/sendFax.go index 26f7b52..cfa2398 100644 --- a/faxSrc/controllers/sendFax.go +++ b/faxSrc/controllers/sendFax.go @@ -19,6 +19,10 @@ type WebFrontendRequest struct { Message string `json:"fax_message"` } +type WebFrontendResponse struct { + State string `json:"state"` +} + // SendRequestToPrinter function will request the thermal printer // to print the message. // It should be able to send the requests both via http/https or sockets @@ -63,27 +67,25 @@ func SendFax(w http.ResponseWriter, r *http.Request) { } session, _ := models.GetSession(token) - log.Printf(" | User %s request a fax.", session.User) + log.Printf("| User %s requested a fax", session.User) data, _ := io.ReadAll(r.Body) - log.Printf("| Received Request: %s", data) // Parse the JSON data that comes from the form var formData WebFrontendRequest err := json.Unmarshal(data, &formData) if err != nil { log.Println("Failed to unmarshal form data: ", err) - w.Write([]byte("Failed to unmarshal form data.")) + w.Write([]byte("{\"state\":\"failed\"}")) return } - resp, err := sendRequestToPrinter(formData.Message) + _, err = sendRequestToPrinter(formData.Message) if err != nil { - w.Write([]byte(fmt.Sprintf("Failed to send message to printer: %s", err))) + w.Write([]byte("{\"state\":\"failed\"}")) return } - log.Println("Print Service Response: ", resp.Message) - - w.Write([]byte("Success")) + w.Header().Set("Content-Type", "text/json") + w.Write([]byte("{\"state\":\"success\"}")) } diff --git a/faxSrc/models/authentication.go b/faxSrc/models/authentication.go index 31c86cf..8306b4f 100644 --- a/faxSrc/models/authentication.go +++ b/faxSrc/models/authentication.go @@ -42,6 +42,7 @@ func openDB() *Database { return &d } +// getUserPass will return the password (encrypted) of the user that is trying to log in func getUserPass(user string) (string, error) { d := openDB() @@ -71,6 +72,7 @@ func getUserPass(user string) (string, error) { return p, nil } +// insertToken will insert the token generated by a successful authentication func insertToken(token string, user string, expirationDate int64) error { d := openDB() @@ -93,6 +95,7 @@ func insertToken(token string, user string, expirationDate int64) error { return nil } +// getSessionData returns the data of a session for a given token func getSessionData(token string) (*SessionData, error) { d := openDB()