-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
42 lines (32 loc) · 844 Bytes
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/nelbermora/go-gemini/gemini"
)
func handler(c *gin.Context) {
apiKey := c.GetHeader("api-key")
if apiKey == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "invaid api key"})
return
}
// Decodificar el JSON del cuerpo de la solicitud
var requestBody RequestBody
if err := c.BindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if requestBody.Text == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid field text"})
return
}
output, err := gemini.AskForData(requestBody.Text, apiKey)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
responseBody := ResponseBody{
Output: output,
}
c.JSON(http.StatusOK, responseBody)
}