-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_request.go
45 lines (40 loc) · 954 Bytes
/
handler_request.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
43
44
45
package main
import (
"fmt"
"io"
"net/http"
"github.com/go-chi/chi"
)
func (apiCfg *apiConfig) handlerRequest(w http.ResponseWriter, r *http.Request) {
input, err := getInput(r)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
// input = strings.ReplaceAll(input, "+", " ")
response, err := callGroqAPI(input)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
respondWithText(w, 200, response+"\n")
}
func getInput(r *http.Request) (string, error) {
input := chi.URLParam(r, "input")
return input, nil
}
func (apiCfg *apiConfig) handlerChatRequest(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, 400, "Error reading body")
return
}
input := string(body)
fmt.Printf("%+v\n", input)
response, err := callGroqAPI(input)
if err != nil {
respondWithError(w, 400, err.Error())
return
}
respondWithText(w, 200, response+"\n")
}