Use message events to encode SSE and display the status accordingly
This commit is contained in:
parent
203dbb8592
commit
114d020284
5 changed files with 24 additions and 16 deletions
10
main.go
10
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -74,7 +75,7 @@ func main() {
|
|||
Name: c.FormValue("name"),
|
||||
Date: time.Now(),
|
||||
Duplex: c.FormValue("duplex") == "on",
|
||||
Events: make(chan string),
|
||||
Events: make(chan scan.Event),
|
||||
}
|
||||
|
||||
documents[document.Id] = document
|
||||
|
@ -115,7 +116,12 @@ func main() {
|
|||
w.Flush()
|
||||
|
||||
for event := range document.Events {
|
||||
fmt.Fprintf(w, "data: Message: %s\n\n", event)
|
||||
msg, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
log.Fatal("Could not encode event")
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "data: %s \n\n", msg)
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ type Document struct {
|
|||
Name string
|
||||
Duplex bool
|
||||
Date time.Time
|
||||
Events chan string
|
||||
Events chan Event
|
||||
}
|
||||
|
||||
func (document Document) Args() []string {
|
||||
|
|
|
@ -12,7 +12,7 @@ func TestDocumentFullName(t *testing.T) {
|
|||
Name: "Here-Be-A-Test-Name",
|
||||
Date: time.Date(2020, 10, 12, 20, 15, 00, 0, time.UTC),
|
||||
Duplex: true,
|
||||
Events: make(chan string),
|
||||
Events: make(chan Event),
|
||||
}
|
||||
|
||||
if document.FullName() != "2020-10-12-201500-Here-Be-A-Test-Name" {
|
||||
|
|
|
@ -3,7 +3,6 @@ package scan
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
@ -34,24 +33,23 @@ func Process(basedir string, document *Document) string {
|
|||
|
||||
command.Start()
|
||||
go func() {
|
||||
document.Events <- command.Name + " Startet"
|
||||
document.Events <- Event{Message: command.Name + " Startet", Type: "info"}
|
||||
|
||||
for scanner.Scan() {
|
||||
document.Events <- scanner.Text()
|
||||
document.Events <- Event{Message: scanner.Text(), Type: command.Name }
|
||||
}
|
||||
|
||||
document.Events <- command.Name + " Ist fast fertig"
|
||||
done <- true
|
||||
document.Events <- command.Name + " Ist gestartet"
|
||||
}()
|
||||
command.Wait()
|
||||
<-done
|
||||
}
|
||||
|
||||
document.Events <- Event{Message: "Starting to copy", Type: "info"}
|
||||
// Open original file
|
||||
original, err := os.Open(filepath.Join(basedir, document.FullNameWithExtension()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
document.Events <- Event{Message: "Alles kaputt", Type: "error"}
|
||||
// log.Fatal(err)
|
||||
}
|
||||
defer original.Close()
|
||||
|
||||
|
@ -59,14 +57,16 @@ func Process(basedir string, document *Document) string {
|
|||
newPath, err := filepath.Abs("./downloads/" + document.FullNameWithExtension())
|
||||
new, err := os.Create(newPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
document.Events <- Event{Message: "Alles kaputt", Type: "error"}
|
||||
// log.Fatal(err)
|
||||
}
|
||||
defer new.Close()
|
||||
|
||||
//This will copy
|
||||
bytesWritten, err := io.Copy(new, original)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
document.Events <- Event{Message: "Alles kaputt", Type: "error"}
|
||||
// log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Bytes Written: %d\n", bytesWritten)
|
||||
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
}
|
||||
</style>
|
||||
<script type="text/javascript">
|
||||
console.log("Jajajaajaj")
|
||||
|
||||
function showStatus(status) {
|
||||
|
||||
for (const el of document.getElementsByClassName("status")) {
|
||||
|
@ -31,11 +29,13 @@
|
|||
if (event.data == "started") {
|
||||
showStatus("waiting")
|
||||
evtSource.onmessage = function(event) {
|
||||
messageEvent = JSON.parse(event.data)
|
||||
|
||||
const newElement = document.createElement("li");
|
||||
const eventList = document.getElementById("list");
|
||||
|
||||
newElement.textContent = "message: " + event.data;
|
||||
showStatus(messageEvent.Type)
|
||||
newElement.textContent = "message: " + messageEvent.Message;
|
||||
eventList.appendChild(newElement);
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +51,8 @@
|
|||
<h3>Status {{ .full_name }}</h3>
|
||||
<div class="hide status" data-name="waiting">⌛</div>
|
||||
<div class="hide status" data-name="done">✅</div>
|
||||
<div class="hide status" data-name="scanimage">📃</div>
|
||||
<div class="hide status" data-name="ocrmypdf">👀</div>
|
||||
<pre id="list"></pre>
|
||||
<a href="/show/{{ .full_name }}" class="hide status" data-name="done">{{.FullNameWithExtension}}</a>
|
||||
<a href="/scan">
|
||||
|
|
Loading…
Reference in a new issue