simplified
This commit is contained in:
parent
166c95b7bb
commit
f2ee3fcb7f
56
main.go
56
main.go
|
@ -20,63 +20,21 @@ type Message struct {
|
||||||
To string `json:"to"`
|
To string `json:"to"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type StreamResponse struct {
|
|
||||||
From string `json:"from"`
|
|
||||||
To string `json:"to"`
|
|
||||||
CreatedAt string `json:"createdAt"`
|
|
||||||
Response string `json:"response"`
|
|
||||||
}
|
|
||||||
|
|
||||||
const linux = "python3"
|
const linux = "python3"
|
||||||
|
|
||||||
func createStreamResponse(fromLanguage, toLanguage, message string) string {
|
func extractMessage(message string) (string, error) {
|
||||||
var extractedMessage string
|
|
||||||
|
|
||||||
// Try to unmarshal the message to see if it's a JSON object
|
|
||||||
var temp map[string]interface{}
|
var temp map[string]interface{}
|
||||||
if err := json.Unmarshal([]byte(message), &temp); err == nil {
|
err := json.Unmarshal([]byte(message), &temp)
|
||||||
// Check if the response field exists in the JSON object
|
|
||||||
if response, exists := temp["response"]; exists {
|
|
||||||
extractedMessage = fmt.Sprintf("%v", response)
|
|
||||||
} else {
|
|
||||||
// If no response field, use the entire message as a fallback
|
|
||||||
extractedMessage = message
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If unmarshalling fails, use the message as it is
|
|
||||||
extractedMessage = message
|
|
||||||
}
|
|
||||||
|
|
||||||
response := StreamResponse{
|
|
||||||
From: fromLanguage,
|
|
||||||
To: toLanguage,
|
|
||||||
CreatedAt: time.Now().Format(time.RFC3339),
|
|
||||||
Response: extractedMessage,
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonResp, err := json.Marshal(response)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return `{"response": "Error in preparing the message."}`
|
return "", fmt.Errorf("failed to unmarshal message: %v", err)
|
||||||
}
|
|
||||||
return string(jsonResp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func streamResponse(w http.ResponseWriter, messages <-chan string) {
|
response, exists := temp["response"]
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
if !exists {
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
return "", fmt.Errorf("no 'response' field found in the message")
|
||||||
w.Header().Set("Connection", "keep-alive")
|
|
||||||
|
|
||||||
flusher, ok := w.(http.Flusher)
|
|
||||||
if !ok {
|
|
||||||
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for msg := range messages {
|
return fmt.Sprintf("%v", response), nil
|
||||||
formattedMessage := createStreamResponse("fromLanguage", "toLanguage", msg) // Adjust as needed
|
|
||||||
fmt.Fprintf(w, " %s\n\n", formattedMessage)
|
|
||||||
flusher.Flush()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
||||||
|
|
Loading…
Reference in New Issue