diff --git a/faxSrc/assets/css/aesthetic.css b/faxSrc/assets/css/aesthetic.css index 2433269..cfbc1a5 100644 --- a/faxSrc/assets/css/aesthetic.css +++ b/faxSrc/assets/css/aesthetic.css @@ -27,19 +27,19 @@ @keyframes flicker { 0% { - opacity: 1; + opacity: 0.9; } 20% { opacity: 1; } 40% { - opacity: 1; + opacity: 1.1; } 60% { - opacity: 1; + opacity: 0.9; } 80% { - opacity: 0.9; + opacity: 1; } 100% { opacity: 1.1; @@ -146,7 +146,6 @@ html { height: 100%; width: 100vw; background-color: var(--background-color); - color: white; /* Font settings */ font-family: dejavu_monospace; font-size: var(--html_font_size); @@ -205,14 +204,36 @@ code { color: var(--url-color); } +#fax-form { + height: 100%; +} + +#content { + height: 90%; +} + +.center { + margin: auto; + width: 52ch; + height: 100%; +} + +#fax-message-boundaries { + height: 100%; +} + textarea { + scrollbar-width: none; + width: 100%; + height: 100%; font-family: dejavu_monospace; font-size: var(--html_font_size); - max-width: 100ch; border: none; overflow: auto; outline: none; + resize: none; background-color: var(--background-color); + color: var(--text-color); -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; diff --git a/faxSrc/assets/css/aesthetic.css.map b/faxSrc/assets/css/aesthetic.css.map index 366a0bb..a79c15e 100644 --- a/faxSrc/assets/css/aesthetic.css.map +++ b/faxSrc/assets/css/aesthetic.css.map @@ -1 +1 @@ -{"version":3,"sourceRoot":"","sources":["crt.scss","main.scss"],"names":[],"mappings":";AAIA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAGA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;EAIA;EACA;EACA;EACA;EAEA;;;AAGF;EAII;IACE;;EADF;IACE;;EADF;IACE;;EADF;IACE;;EADF;IACE;;EADF;IACE;;;AAKN;EACE;IACE;;;AAIJ;EACE;IACE;;EAGF;IACE;;EAGF;IACE;;;AAIJ;EACE;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;;AClJJ;EACE;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;;AAGF;EACE;EACA;EACA;EAEA;EAEA;EACA;AAEA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EAEA;EAEA;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;EACA;EACA;EACA;EACA;;;AAIF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EAEA;EACA;EACA;;;AAIF;AACA;EACE;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;;;AAEF;AAEA;EACE;;;AAGF;EACE;;;AAGF;EACE;;;AAGF;EACE;;;AAGF;AAAA;EAEE;EACA,aACE;;;AAGJ;EACE;EACA;EAEA;EACA;;;AAGF;AACE;EACA;EACA;EACA;EACA;EACA;AAEA;EACA;EACA;EACA;EACA;EAEA;EACA;EAEA;EAEA;;;AAGF;EACE;EAEA;AAEA;EACA;AACA;EACA;EAEA;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;AACA;AACA;AACA;EACE;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;;;AAGF;EACE;IACE","file":"aesthetic.css"} \ No newline at end of file +{"version":3,"sourceRoot":"","sources":["crt.scss","main.scss"],"names":[],"mappings":";AAIA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAGA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;EAIA;EACA;EACA;EACA;EAEA;;;AAGF;EAII;IACE;;EADF;IACE;;EADF;IACE;;EADF;IACE;;EADF;IACE;;EADF;IACE;;;AAKN;EACE;IACE;;;AAIJ;EACE;IACE;;EAGF;IACE;;EAGF;IACE;;;AAIJ;EACE;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;EAGF;IACE;;;AClJJ;EACE;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;;AAGF;EACE;EACA;EACA;EAEA;EAEA;AAEA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EAEA;EAEA;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;EACA;EACA;EACA;EACA;;;AAGF;EACE;;;AAGF;EACE;;;AAGF;EACE;EAIA;EACA;;;AAGF;EACE;;;AAGF;EACE;EACA;EACA;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAEA;EACA;EACA;;;AAIF;AACA;EACE;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;;;AAEF;AAEA;EACE;;;AAGF;EACE;;;AAGF;EACE;;;AAGF;EACE;;;AAGF;AAAA;EAEE;EACA,aACE;;;AAGJ;EACE;EACA;EAEA;EACA;;;AAGF;AACE;EACA;EACA;EACA;EACA;EACA;AAEA;EACA;EACA;EACA;EACA;EAEA;EACA;EAEA;EAEA;;;AAGF;EACE;EAEA;AAEA;EACA;AACA;EACA;EAEA;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;;;AAGF;AACA;AACA;AACA;EACE;;;AAGF;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;;;AAGF;EACE;IACE","file":"aesthetic.css"} \ No newline at end of file diff --git a/faxSrc/assets/css/main.scss b/faxSrc/assets/css/main.scss index b1c7f1a..cfbfaa4 100644 --- a/faxSrc/assets/css/main.scss +++ b/faxSrc/assets/css/main.scss @@ -23,7 +23,6 @@ html { width: 100vw; background-color: var(--background-color); - color: white; /* Font settings */ font-family: dejavu_monospace; @@ -85,15 +84,40 @@ code { color: var(--url-color); } +#fax-form { + height: 100%; +} + +#content { + height: 90%; +} + +.center { + margin: auto; + // The width can also be 48chr for large chars + // 64chr for small chars + // Add 4 chars to compensate for paddings and so on + width: calc(48ch + 4ch); + height: 100%; +} + +#fax-message-boundaries { + height: 100%; +} textarea { + scrollbar-width: none; + width: 100%; + height: 100%; + font-family: dejavu_monospace; font-size: var(--html_font_size); - max-width: 100ch; border: none; overflow: auto; outline: none; + resize: none; background-color: var(--background-color); + color: var(--text-color); -webkit-box-shadow: none; -moz-box-shadow: none; diff --git a/faxSrc/assets/htmx/login.html b/faxSrc/assets/htmx/login.html index 265e934..0126264 100644 --- a/faxSrc/assets/htmx/login.html +++ b/faxSrc/assets/htmx/login.html @@ -1,4 +1,61 @@ -
- Message - -
\ No newline at end of file +
+
+ +
+ FAX + +
+ +
+ +
+ +
->
+
+
+ + \ No newline at end of file diff --git a/faxSrc/controllers/sendFax.go b/faxSrc/controllers/sendFax.go new file mode 100644 index 0000000..c8059f6 --- /dev/null +++ b/faxSrc/controllers/sendFax.go @@ -0,0 +1,81 @@ +package controllers + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net/http" +) + +type PrintServerResponse struct { + Message string `json:"result"` +} + +type WebFrontendRequest struct { + Message string `json:"fax_message"` +} + +// 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 +// TODO: add support for sockets +func sendRequestToPrinter(msg string) (PrintServerResponse, error) { + // Send the request to the actual printer server + req, err := http.NewRequest( + http.MethodPost, + "http://127.0.0.1:9099/print", + bytes.NewBuffer([]byte(msg)), + ) + if err != nil { + errStr := fmt.Sprintf("Failed to create new request: %s", err) + return PrintServerResponse{}, errors.New(errStr) + } + + // Perform the request + client := &http.Client{} + res, err := client.Do(req) + if err != nil { + errStr := fmt.Sprintf("Failed to perform new request: %s", err) + return PrintServerResponse{}, errors.New(errStr) + } + + // Process the request data + out, _ := io.ReadAll(res.Body) + var resp PrintServerResponse + err = json.Unmarshal(out, &resp) + if err != nil { + errStr := fmt.Sprintf("Failed to unmarshall response: %s", err) + return PrintServerResponse{}, errors.New(errStr) + } + + return resp, nil +} + +// sendFax is a function that receives the data from the fronted and +// sends it to the print server +func SendFax(w http.ResponseWriter, r *http.Request) { + 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.")) + return + } + + resp, err := sendRequestToPrinter(formData.Message) + if err != nil { + w.Write([]byte(fmt.Sprintf("Failed to send message to printer: %s", err))) + return + } + + log.Println("Print Service Response: ", resp.Message) + + w.Write([]byte("Success")) +} diff --git a/faxSrc/main.go b/faxSrc/main.go index 56c9c9d..1ef5709 100644 --- a/faxSrc/main.go +++ b/faxSrc/main.go @@ -4,71 +4,32 @@ import ( "fmt" "log" "net/http" - - "golang.org/x/crypto/bcrypt" + "thermalFax/controllers" + "thermalFax/models" + "thermalFax/views" ) -// 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") - - if user != "r" { - return false - } - - 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 -} - +// launchServer is the main server function func launchServer(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "text/html") - if !authenticate(w, r) { + // Authenticate the user + // TODO: I should make this a bit better since we want to use another service + // and that one should handle the authentication and this one make use of it + var auth models.Authentication + if !auth.Authenticate(w, r) { return } - // data, _ := io.ReadAll(r.Body) - - // main page starts here I guess - w.WriteHeader(http.StatusOK) - - mainPage := HtmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{ - "content": HtmlTemplate("./assets/htmx/login.html", nil), - "renderNavBar": true, - "page": "fax", - }) - - _, err := w.Write([]byte(mainPage)) - if err != nil { - http.Error(w, "Internal error", http.StatusInternalServerError) - return - } + views.EditorPage(w, r) } func main() { // Set up a very basic http server http.HandleFunc("/", launchServer) + http.HandleFunc("/send-fax", controllers.SendFax) + // Serve the CSS and JS files, we might have to change this http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) diff --git a/faxSrc/models/authentication.go b/faxSrc/models/authentication.go new file mode 100644 index 0000000..e90bb97 --- /dev/null +++ b/faxSrc/models/authentication.go @@ -0,0 +1,52 @@ +package models + +import ( + "log" + "net/http" + + "golang.org/x/crypto/bcrypt" +) + +var DATABASE map[string]string = map[string]string{ + "user": "$2y$08$ZBb/RxlrNHcA.YNasvGlhOeUmJVZe/clNelP4jwBpkpq/r4mE.lze", +} + +type Authenticator interface { + isAuthorised(string, string) bool + Authenticate(w http.ResponseWriter, r *http.Request) bool +} +type Authentication struct{} + +// pass -> $2y$08$ZBb/RxlrNHcA.YNasvGlhOeUmJVZe/clNelP4jwBpkpq/r4mE.lze +func (a *Authentication) isAuthorised(user string, pass string) bool { + p, ok := DATABASE[user] + if !ok { + return false + } + + err := bcrypt.CompareHashAndPassword([]byte(p), []byte(pass)) + + return err == nil +} + +func (a *Authentication) Authenticate(w http.ResponseWriter, r *http.Request) bool { + username, password, ok := r.BasicAuth() + if !ok { + log.Println("Log in failed") + 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 !a.isAuthorised(username, password) { + log.Printf("Log in failed: %s:%s", 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 + } + + log.Printf("User %s logged in successfully", username) + return true +} diff --git a/faxSrc/views/editorPage.go b/faxSrc/views/editorPage.go new file mode 100644 index 0000000..14cdc1d --- /dev/null +++ b/faxSrc/views/editorPage.go @@ -0,0 +1,19 @@ +package views + +import "net/http" + +func EditorPage(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + + mainPage := htmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{ + "content": htmlTemplate("./assets/htmx/login.html", nil), + "renderNavBar": true, + "page": "fax", + }) + + _, err := w.Write([]byte(mainPage)) + if err != nil { + http.Error(w, "Internal error", http.StatusInternalServerError) + return + } +} diff --git a/faxSrc/templating.go b/faxSrc/views/templating.go similarity index 68% rename from faxSrc/templating.go rename to faxSrc/views/templating.go index 6643f0a..8488a20 100644 --- a/faxSrc/templating.go +++ b/faxSrc/views/templating.go @@ -1,22 +1,21 @@ -package main +package views import ( "bytes" "html/template" "log" - "os" "path" ) -func LoadFile(path string) []byte { - file, err := os.ReadFile(path) - if err != nil { - log.Printf("Unable to open file: %s\n", path) - } - return file -} +// func loadFile(path string) []byte { +// file, err := os.ReadFile(path) +// if err != nil { +// log.Printf("Unable to open file: %s\n", path) +// } +// return file +// } -func HtmlTemplate(file string, data map[string]interface{}) template.HTML { +func htmlTemplate(file string, data map[string]interface{}) template.HTML { templateName := path.Base(file) tmpl, err := template.New(templateName).ParseFiles(file) if err != nil { diff --git a/src/cmd/server.go b/src/cmd/server.go index c27d749..ad860ae 100644 --- a/src/cmd/server.go +++ b/src/cmd/server.go @@ -14,8 +14,8 @@ import ( func serverCmdAction(cmd *cobra.Command, args []string) { http.HandleFunc("/print", server.LaunchServer) - fmt.Println("Starting server on port 8083") - log.Fatal(http.ListenAndServe(":8083", nil)) + fmt.Println("Starting server on port 9099") + log.Fatal(http.ListenAndServe(":9099", nil)) } // serverCmd represents the server command diff --git a/src/server/tpServer.go b/src/server/tpServer.go index 4339e49..4fc1731 100644 --- a/src/server/tpServer.go +++ b/src/server/tpServer.go @@ -15,14 +15,15 @@ func printMessage(message string) { func LaunchServer(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") - if !authenticate(w, r) { - return - } + // TODO: solve this thing + // if !authenticate(w, r) { + // return + // } data, _ := io.ReadAll(r.Body) fmt.Println(string(data)) - // printMessage(string(data) + "\n\n\n\n\n") + // printMessage(string(data) + "\n\n\n") w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"message": "welcome to thermics"}`)) + w.Write([]byte(`{"result": "SUCCESS"}`)) }