Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
Falko Victor Habel | 0078b9aec4 | |
Falko Victor Habel | 6a73f0ed9e | |
Falko Victor Habel | 34075e6da5 | |
Falko Victor Habel | 2da06410e8 | |
Falko Victor Habel | f2ee3fcb7f |
44
main.go
44
main.go
|
@ -20,49 +20,10 @@ 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 {
|
|
||||||
var extractedMessage string
|
|
||||||
|
|
||||||
// Try to unmarshal the message to see if it's a JSON object
|
|
||||||
var temp map[string]interface{}
|
|
||||||
if err := json.Unmarshal([]byte(message), &temp); err == nil {
|
|
||||||
// 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 {
|
|
||||||
return `{"response": "Error in preparing the message."}`
|
|
||||||
}
|
|
||||||
return string(jsonResp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func streamResponse(w http.ResponseWriter, messages <-chan string) {
|
func streamResponse(w http.ResponseWriter, messages <-chan string) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
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")
|
||||||
|
|
||||||
|
@ -73,8 +34,7 @@ func streamResponse(w http.ResponseWriter, messages <-chan string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for msg := range messages {
|
for msg := range messages {
|
||||||
formattedMessage := createStreamResponse("fromLanguage", "toLanguage", msg) // Adjust as needed
|
fmt.Fprintf(w, "%s\n", msg)
|
||||||
fmt.Fprintf(w, " %s\n\n", formattedMessage)
|
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,4 +15,4 @@ data = {
|
||||||
response = requests.post(url, headers=headers, data=json.dumps(data))
|
response = requests.post(url, headers=headers, data=json.dumps(data))
|
||||||
|
|
||||||
print(response.status_code)
|
print(response.status_code)
|
||||||
print(response.json())
|
print(response.text)
|
||||||
|
|
Loading…
Reference in New Issue