Merge pull request 'Bug #002 Fixed. Downloading gives visuell feedback' (#10) from added_downloading_response into main

Reviewed-on: http://192.168.178.135:3000/Fabelous/GO-Translator/pulls/10
This commit is contained in:
Falko Victor Habel 2024-03-15 15:10:43 +00:00
commit d71038369d
1 changed files with 18 additions and 2 deletions

20
main.go
View File

@ -62,6 +62,7 @@ func streamResponse(w http.ResponseWriter, fromLanguage, toLanguage string, mess
}
func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
// Create a channel to send messages from the download process
messages := make(chan string)
defer close(messages)
@ -84,12 +85,26 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
}
// Initial message sent to the channel for streaming
messages <- "Download started . . ."
messages <- "Download started..."
// Start a ticker to send "..." every few seconds until we get real output
ticker := time.NewTicker(2 * time.Second)
go func() {
for range ticker.C {
messages <- ". . ."
}
}()
// Read the output of the Python command and send it to the channel
scanner := bufio.NewScanner(output)
firstOutputReceived := false
for scanner.Scan() {
messages <- scanner.Text() // Sends output line by line to the stream
if !firstOutputReceived {
// Stop the ticker after receiving the first real output
ticker.Stop()
firstOutputReceived = true
}
messages <- scanner.Text()
}
// Wait for the command to complete
@ -98,6 +113,7 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
messages <- fmt.Sprintf("Error waiting for Download: %s", err.Error())
return
}
return
}
func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) {