Organized the current server code
This commit is contained in:
@@ -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