Compare commits
No commits in common. "main" and "revoked" have entirely different histories.
44
main.go
44
main.go
|
@ -20,10 +20,49 @@ 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", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; 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")
|
||||||
|
|
||||||
|
@ -34,7 +73,8 @@ func streamResponse(w http.ResponseWriter, messages <-chan string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for msg := range messages {
|
for msg := range messages {
|
||||||
fmt.Fprintf(w, "%s\n", msg)
|
formattedMessage := createStreamResponse("fromLanguage", "toLanguage", msg) // Adjust as needed
|
||||||
|
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.text)
|
print(response.json())
|
||||||
|
|
Loading…
Reference in New Issue