Compare commits

..

No commits in common. "main" and "revoked" have entirely different histories.

2 changed files with 43 additions and 3 deletions

44
main.go
View File

@ -20,10 +20,49 @@ type Message struct {
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"
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) {
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("Connection", "keep-alive")
@ -34,7 +73,8 @@ func streamResponse(w http.ResponseWriter, messages <-chan string) {
}
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()
}
}

View File

@ -15,4 +15,4 @@ data = {
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.status_code)
print(response.text)
print(response.json())