From 511cd8a570dcbf1485ddfb653b6658cf4f8e2872 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 3 May 2024 16:00:45 +0100 Subject: [PATCH] Organized the current server code --- cmd/server.go | 48 ++++------------------------------------ server/authentication.go | 34 ++++++++++++++++++++++++++++ server/tpServer.go | 28 +++++++++++++++++++++++ 3 files changed, 66 insertions(+), 44 deletions(-) create mode 100644 server/authentication.go create mode 100644 server/tpServer.go diff --git a/cmd/server.go b/cmd/server.go index 4b6a928..c27d749 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -5,57 +5,17 @@ package cmd import ( "fmt" - "io" "log" "net/http" - thermalprinter "thermalPrinter/thermalPrinter" + "thermalPrinter/server" "github.com/spf13/cobra" - "golang.org/x/crypto/bcrypt" ) -// pass -> $2y$08$ZBb/RxlrNHcA.YNasvGlhOeUmJVZe/clNelP4jwBpkpq/r4mE.lze -func isAuthorised(user string, pass string) bool { - p := []byte("$2y$08$ZBb/RxlrNHcA.YNasvGlhOeUmJVZe/clNelP4jwBpkpq/r4mE.lze") - err := bcrypt.CompareHashAndPassword(p, []byte(pass)) - - return err == nil -} - -func printMessage(message string) { - printer := thermalprinter.GetInstance(nil) - printer.Print(message) -} - -func print(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - - username, password, ok := r.BasicAuth() - if !ok { - w.Header().Add("WWW-Authenticate", `Basic realm="Give username and password"`) - w.WriteHeader(http.StatusUnauthorized) - w.Write([]byte(`{"message": "No basic auth present"}`)) - return - } - - if !isAuthorised(username, password) { - w.Header().Add("WWW-Authenticate", `Basic realm="Give username and password"`) - w.WriteHeader(http.StatusUnauthorized) - w.Write([]byte(`{"message": "Invalid username or password"}`)) - return - } - - data, _ := io.ReadAll(r.Body) - printMessage(string(data) + "\n\n\n\n\n") - - w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"message": "welcome to thermics"}`)) -} - func serverCmdAction(cmd *cobra.Command, args []string) { - http.HandleFunc("/print", print) - fmt.Println("Starting server on port 8080") - log.Fatal(http.ListenAndServe(":8080", nil)) + http.HandleFunc("/print", server.LaunchServer) + fmt.Println("Starting server on port 8083") + log.Fatal(http.ListenAndServe(":8083", nil)) } // serverCmd represents the server command diff --git a/server/authentication.go b/server/authentication.go new file mode 100644 index 0000000..1402969 --- /dev/null +++ b/server/authentication.go @@ -0,0 +1,34 @@ +package server + +import ( + "net/http" + + "golang.org/x/crypto/bcrypt" +) + +// pass -> $2y$08$ZBb/RxlrNHcA.YNasvGlhOeUmJVZe/clNelP4jwBpkpq/r4mE.lze +func isAuthorised(user string, pass string) bool { + p := []byte("$2y$08$ZBb/RxlrNHcA.YNasvGlhOeUmJVZe/clNelP4jwBpkpq/r4mE.lze") + err := bcrypt.CompareHashAndPassword(p, []byte(pass)) + + return err == nil +} + +func authenticate(w http.ResponseWriter, r *http.Request) bool { + username, password, ok := r.BasicAuth() + if !ok { + w.Header().Add("WWW-Authenticate", `Basic realm="Give username and password"`) + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(`{"message": "No basic auth present"}`)) + return false + } + + if !isAuthorised(username, password) { + w.Header().Add("WWW-Authenticate", `Basic realm="Give username and password"`) + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(`{"message": "Invalid username or password"}`)) + return false + } + + return true +} diff --git a/server/tpServer.go b/server/tpServer.go new file mode 100644 index 0000000..4339e49 --- /dev/null +++ b/server/tpServer.go @@ -0,0 +1,28 @@ +package server + +import ( + "fmt" + "io" + "net/http" + thermalprinter "thermalPrinter/thermalPrinter" +) + +func printMessage(message string) { + printer := thermalprinter.GetInstance(nil) + printer.Print(message) +} + +func LaunchServer(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + + if !authenticate(w, r) { + return + } + + data, _ := io.ReadAll(r.Body) + fmt.Println(string(data)) + // printMessage(string(data) + "\n\n\n\n\n") + + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"message": "welcome to thermics"}`)) +}