43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"gitea.miguelmuniz.com/junk2jive-server/internal/config"
|
|
"gitea.miguelmuniz.com/junk2jive-server/internal/services"
|
|
)
|
|
|
|
type TextPromptRequest struct {
|
|
Query string `json:"query"`
|
|
}
|
|
|
|
type DIYResponse struct {
|
|
Prompt string `json:"prompt"`
|
|
Result string `json:"result"`
|
|
}
|
|
|
|
func TextPromptHandler(cfg *config.Config) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req TextPromptRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "Invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
openAI := services.NewOpenAIService(cfg.OpenAIKey)
|
|
result, err := openAI.GenerateDIY(req.Query)
|
|
if err != nil {
|
|
http.Error(w, "Failed to generate DIY ideas", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := DIYResponse{
|
|
Prompt: "AI Suggestions for " + req.Query + " are:",
|
|
Result: result,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
} |