Changes for the fax branch

This commit is contained in:
2024-05-09 15:14:01 +01:00
parent 9c86b28fc0
commit ec957b1255
11 changed files with 296 additions and 81 deletions
+27 -6
View File
@@ -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;
+1 -1
View File
@@ -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"}
{"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"}
+26 -2
View File
@@ -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;
+61 -4
View File
@@ -1,4 +1,61 @@
<fieldset>
<legend>Message</legend>
<textarea id="fax-message"></textarea>
</fieldset>
<div class="center">
<form
id="fax-form"
action="submit"
hx-post="/send-fax"
hx-swap="innerHTML"
hx-target="#req-result">
<fieldset id="fax-message-boundaries">
<legend>FAX</legend>
<textarea name="fax-message" id="fax-message"></textarea>
</fieldset>
<br>
<button id="fax-submit-button" type="button">Submit</button>
<br>
<div id="req-result">-&gt;</div>
</form>
</div>
<script>
// Get a reference to the submit button
var submitBtn = document.getElementById("fax-submit-button");
// Add a click event listener to the submit button
submitBtn.addEventListener("click", function() {
// Get the fax message from the textarea
var faxMessage = document.getElementById("fax-message").value;
// Create the JSON object to send
var jsonData = {
fax_message: faxMessage
};
// Configure the fetch request
fetch("/send-fax", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(jsonData)
})
.then(response => {
// Check if the response is OK
if (response.ok) {
// Request was successful, handle response if needed
return response.text();
} else {
// Request failed, handle error if needed
throw new Error("Error: " + response.statusText);
}
})
.then(data => {
console.log("Success:", data);
})
.catch(error => {
console.error(error);
});
});
</script>
+81
View File
@@ -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"))
}
+12 -51
View File
@@ -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"))))
+52
View File
@@ -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
}
+19
View File
@@ -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
}
}
@@ -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 {
+2 -2
View File
@@ -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
+6 -5
View File
@@ -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"}`))
}