multi response capabilitys added
This commit is contained in:
parent
b94b62c57c
commit
2ca9ac7a1b
65
main.go
65
main.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,7 +20,6 @@ type Message struct {
|
||||||
To string `json:"to"`
|
To string `json:"to"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: The StreamResponse structure simplified for demonstration. Adjust according to actual requirements.
|
|
||||||
type StreamResponse struct {
|
type StreamResponse struct {
|
||||||
From string `json:"from"`
|
From string `json:"from"`
|
||||||
To string `json:"to"`
|
To string `json:"to"`
|
||||||
|
@ -44,7 +44,6 @@ func createStreamResponse(fromLanguage, toLanguage, message string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func streamResponse(w http.ResponseWriter, fromLanguage, toLanguage string, messages <-chan string) {
|
func streamResponse(w http.ResponseWriter, fromLanguage, toLanguage string, messages <-chan string) {
|
||||||
// Set headers for SSE
|
|
||||||
w.Header().Set("Content-Type", "text/event-stream")
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
w.Header().Set("Connection", "keep-alive")
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
@ -58,20 +57,16 @@ func streamResponse(w http.ResponseWriter, fromLanguage, toLanguage string, mess
|
||||||
for msg := range messages {
|
for msg := range messages {
|
||||||
formattedMessage := createStreamResponse(fromLanguage, toLanguage, msg)
|
formattedMessage := createStreamResponse(fromLanguage, toLanguage, msg)
|
||||||
fmt.Fprintf(w, " %s\n\n", formattedMessage)
|
fmt.Fprintf(w, " %s\n\n", formattedMessage)
|
||||||
flusher.Flush() // Ensure client receives the update immediately
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
||||||
|
|
||||||
// Create a channel to send messages from the download process
|
|
||||||
messages := make(chan string)
|
messages := make(chan string)
|
||||||
defer close(messages)
|
defer close(messages)
|
||||||
|
|
||||||
// Use a goroutine for streaming responses so we can proceed with the download
|
|
||||||
go streamResponse(w, fromLanguage, toLanguage, messages)
|
go streamResponse(w, fromLanguage, toLanguage, messages)
|
||||||
|
|
||||||
// Start the python command in the background
|
|
||||||
cmd := exec.Command(linux, "translator/download.py", fromLanguage, toLanguage)
|
cmd := exec.Command(linux, "translator/download.py", fromLanguage, toLanguage)
|
||||||
output, err := cmd.StdoutPipe()
|
output, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -85,10 +80,8 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
go func() {
|
go func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
|
@ -96,19 +89,16 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Read the output of the python command and send it to the channel
|
|
||||||
scanner := bufio.NewScanner(output)
|
scanner := bufio.NewScanner(output)
|
||||||
firstOutputReceived := false
|
firstOutputReceived := false
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if !firstOutputReceived {
|
if !firstOutputReceived {
|
||||||
// Stop the ticker after receiving the first real output
|
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
firstOutputReceived = true
|
firstOutputReceived = true
|
||||||
}
|
}
|
||||||
messages <- scanner.Text()
|
messages <- scanner.Text()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the command to complete
|
|
||||||
err = cmd.Wait()
|
err = cmd.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
messages <- fmt.Sprintf("Error waiting for Download: %s", err.Error())
|
messages <- fmt.Sprintf("Error waiting for Download: %s", err.Error())
|
||||||
|
@ -117,14 +107,10 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) {
|
func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) {
|
||||||
// Create a message channel for streaming translation results
|
|
||||||
messages := make(chan string)
|
messages := make(chan string)
|
||||||
defer close(messages)
|
defer close(messages)
|
||||||
|
|
||||||
// A goroutine will manage sending streamed responses
|
|
||||||
go streamResponse(w, fromLanguage, toLanguage, messages)
|
go streamResponse(w, fromLanguage, toLanguage, messages)
|
||||||
|
|
||||||
// Start the python command in the background
|
|
||||||
cmd := exec.Command(linux, "translator/translate.py", message, fromLanguage, toLanguage)
|
cmd := exec.Command(linux, "translator/translate.py", message, fromLanguage, toLanguage)
|
||||||
output, err := cmd.StdoutPipe()
|
output, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -138,13 +124,11 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the output of the python command and send it through the channel
|
|
||||||
scanner := bufio.NewScanner(output)
|
scanner := bufio.NewScanner(output)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
messages <- scanner.Text() // Sends each line of the output to the stream
|
messages <- scanner.Text()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the command to complete
|
|
||||||
err = cmd.Wait()
|
err = cmd.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
messages <- fmt.Sprintf("Error waiting for Translation: %s", err.Error())
|
messages <- fmt.Sprintf("Error waiting for Translation: %s", err.Error())
|
||||||
|
@ -153,27 +137,24 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckLanguagesInstalled(fromCode, toCode string) (bool, error) {
|
func CheckLanguagesInstalled(fromCode, toCode string) (bool, error) {
|
||||||
// Construct the target directory path
|
|
||||||
homeDir, err := os.UserHomeDir()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("unable to determine the user home directory: %w", err)
|
return false, fmt.Errorf("unable to determine the user home directory: %w", err)
|
||||||
}
|
}
|
||||||
targetDir := filepath.Join(homeDir, ".local", "share", "argos-translate", "packages", fromCode+"_"+toCode)
|
targetDir := filepath.Join(homeDir, ".local", "share", "argos-translate", "packages", fromCode+"_"+toCode)
|
||||||
|
|
||||||
// Check if the directory exists
|
|
||||||
_, err = os.Stat(targetDir)
|
_, err = os.Stat(targetDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return false, nil // Directory does not exist, indicating the language pair is not installed
|
return false, nil
|
||||||
}
|
}
|
||||||
return false, fmt.Errorf("error checking for language directory: %w", err)
|
return false, fmt.Errorf("error checking for language directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The directory exists, indicating the language pair is installed
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
// Read the request body
|
|
||||||
if r.Header.Get("Content-Type") != "application/json" {
|
if r.Header.Get("Content-Type") != "application/json" {
|
||||||
http.Error(w, "Request content type must be application/json", http.StatusBadRequest)
|
http.Error(w, "Request content type must be application/json", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
@ -185,7 +166,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
// Unmarshal the JSON data
|
|
||||||
var msg Message
|
var msg Message
|
||||||
err = json.Unmarshal(body, &msg)
|
err = json.Unmarshal(body, &msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -193,37 +173,42 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if From and To fields are not longer than 2 letters
|
|
||||||
if len(msg.From) > 2 || len(msg.To) > 2 {
|
if len(msg.From) > 2 || len(msg.To) > 2 {
|
||||||
http.Error(w, "From and To fields should not be longer than 2 letters.", http.StatusBadRequest)
|
http.Error(w, "From and To fields should not be longer than 2 letters.", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
} else {
|
}
|
||||||
installed, err := CheckLanguagesInstalled(msg.From, msg.To)
|
|
||||||
if err != nil {
|
installed, err := CheckLanguagesInstalled(msg.From, msg.To)
|
||||||
if err.Error() == "file not found" {
|
if err != nil {
|
||||||
http.Error(w, "file not found "+err.Error(), http.StatusInternalServerError)
|
if err.Error() == "file not found" {
|
||||||
} else {
|
http.Error(w, "file not found "+err.Error(), http.StatusInternalServerError)
|
||||||
fmt.Println("An error occurred:", err)
|
} else {
|
||||||
}
|
fmt.Println("An error occurred:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
if installed {
|
if installed {
|
||||||
executeTranslator(w, msg.Message, msg.From, msg.To)
|
executeTranslator(w, msg.Message, msg.From, msg.To)
|
||||||
} else {
|
} else {
|
||||||
downloadPackages(w, msg.From, msg.To)
|
downloadPackages(w, msg.From, msg.To)
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Define the HTTP handler function
|
http.HandleFunc("/", handleRequest)
|
||||||
http.HandleFunc("/", handleRequest) // This ensures all requests are routed to handleRequest
|
|
||||||
|
|
||||||
// Define the port
|
|
||||||
port := "53184"
|
port := "53184"
|
||||||
address := fmt.Sprintf("0.0.0.0:%s", port)
|
address := fmt.Sprintf("0.0.0.0:%s", port)
|
||||||
|
|
||||||
// Start the HTTP server
|
|
||||||
fmt.Printf("Listening on %s...\n", address)
|
fmt.Printf("Listening on %s...\n", address)
|
||||||
err := http.ListenAndServe(address, nil)
|
err := http.ListenAndServe(address, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue