Organized the current server code
This commit is contained in:
+4
-44
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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"}`))
|
||||
}
|
||||
Reference in New Issue
Block a user