added env support

This commit is contained in:
Falko Victor Habel 2024-03-16 10:41:43 +01:00
parent 6e4504297d
commit 2f8c114f9f
1 changed files with 9 additions and 7 deletions

16
main.go
View File

@ -28,6 +28,8 @@ type StreamResponse struct {
Response string `json:"response"` Response string `json:"response"`
} }
const env = "/home/translator/env/bin/getPythonCommand()"
func createStreamResponse(fromLanguage, toLanguage, message string) string { func createStreamResponse(fromLanguage, toLanguage, message string) string {
response := StreamResponse{ response := StreamResponse{
From: fromLanguage, From: fromLanguage,
@ -70,8 +72,8 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
// Use a goroutine for streaming responses so we can proceed with the download // 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 python3 command in the background // Start the getPythonCommand() command in the background
cmd := exec.Command("python3", "translator/download.py", fromLanguage, toLanguage) cmd := exec.Command(env, "translator/download.py", fromLanguage, toLanguage)
output, err := cmd.StdoutPipe() output, err := cmd.StdoutPipe()
if err != nil { if err != nil {
messages <- fmt.Sprintf("Error starting Download: %s", err.Error()) messages <- fmt.Sprintf("Error starting Download: %s", err.Error())
@ -95,7 +97,7 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
} }
}() }()
// Read the output of the python3 command and send it to the channel // Read the output of the getPythonCommand() 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() {
@ -124,8 +126,8 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage
// A goroutine will manage sending streamed responses // A goroutine will manage sending streamed responses
go streamResponse(w, fromLanguage, toLanguage, messages) go streamResponse(w, fromLanguage, toLanguage, messages)
// Start the python3 command in the background // Start the getPythonCommand() command in the background
cmd := exec.Command("python3", "translator/translate.py", message, fromLanguage, toLanguage) cmd := exec.Command(env, "translator/translate.py", message, fromLanguage, toLanguage)
output, err := cmd.StdoutPipe() output, err := cmd.StdoutPipe()
if err != nil { if err != nil {
messages <- fmt.Sprintf("Error starting Translation: %s", err.Error()) messages <- fmt.Sprintf("Error starting Translation: %s", err.Error())
@ -138,7 +140,7 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage
return return
} }
// Read the output of the python3 command and send it through the channel // Read the output of the getPythonCommand() 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() // Sends each line of the output to the stream
@ -183,7 +185,7 @@ func CheckLanguagesInstalled(fromCode, toCode string) (bool, error) {
return false, nil return false, nil
} }
func getAllPackages() error { func getAllPackages() error {
cmd := exec.Command("python3", "translator/note_all_packages.py") cmd := exec.Command(env, "translator/note_all_packages.py")
// Create a buffer to capture the standard output. // Create a buffer to capture the standard output.
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out