Skip to content

Commit 0349f37

Browse files
elibengopherbot
authored andcommitted
ragserver: fix other variants to be similar to ragserver-genkit
Apply suggestions from go.dev/cl/608737 to the other ragserver variants Change-Id: I627cec1ff21c8f7c1b1d257b542435793ade680d Reviewed-on: https://go-review.googlesource.com/c/example/+/609305 TryBot-Bypass: Eli Bendersky <eliben@google.com> Auto-Submit: Eli Bendersky <eliben@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Eli Bendersky <eliben@google.com>
1 parent 88ba6f5 commit 0349f37

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

ragserver/ragserver-langchaingo/json.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// readRequestJSON expects req to have a JSON content type with a body that
1515
// contains a JSON-encoded value complying with the underlying type of target.
1616
// It populates target, or returns an error.
17-
func readRequestJSON(target any, req *http.Request) error {
17+
func readRequestJSON(req *http.Request, target any) error {
1818
contentType := req.Header.Get("Content-Type")
1919
mediaType, _, err := mime.ParseMediaType(contentType)
2020
if err != nil {
@@ -26,14 +26,11 @@ func readRequestJSON(target any, req *http.Request) error {
2626

2727
dec := json.NewDecoder(req.Body)
2828
dec.DisallowUnknownFields()
29-
if err := dec.Decode(target); err != nil {
30-
return err
31-
}
32-
return nil
29+
return dec.Decode(target)
3330
}
3431

3532
// renderJSON renders 'v' as JSON and writes it as a response into w.
36-
func renderJSON(w http.ResponseWriter, v interface{}) {
33+
func renderJSON(w http.ResponseWriter, v any) {
3734
js, err := json.Marshal(v)
3835
if err != nil {
3936
http.Error(w, err.Error(), http.StatusInternalServerError)

ragserver/ragserver-langchaingo/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func main() {
6060

6161
mux := http.NewServeMux()
6262
mux.HandleFunc("POST /add/", server.addDocumentsHandler)
63-
mux.HandleFunc("GET /query/", server.queryHandler)
63+
mux.HandleFunc("POST /query/", server.queryHandler)
6464

6565
port := cmp.Or(os.Getenv("SERVERPORT"), "9020")
6666
address := "localhost:" + port
@@ -84,7 +84,7 @@ func (rs *ragServer) addDocumentsHandler(w http.ResponseWriter, req *http.Reques
8484
}
8585
ar := &addRequest{}
8686

87-
err := readRequestJSON(ar, req)
87+
err := readRequestJSON(req, ar)
8888
if err != nil {
8989
http.Error(w, err.Error(), http.StatusBadRequest)
9090
return
@@ -108,7 +108,7 @@ func (rs *ragServer) queryHandler(w http.ResponseWriter, req *http.Request) {
108108
Content string
109109
}
110110
qr := &queryRequest{}
111-
err := readRequestJSON(qr, req)
111+
err := readRequestJSON(req, qr)
112112
if err != nil {
113113
http.Error(w, err.Error(), http.StatusBadRequest)
114114
return

ragserver/ragserver/json.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// readRequestJSON expects req to have a JSON content type with a body that
1515
// contains a JSON-encoded value complying with the underlying type of target.
1616
// It populates target, or returns an error.
17-
func readRequestJSON(target any, req *http.Request) error {
17+
func readRequestJSON(req *http.Request, target any) error {
1818
contentType := req.Header.Get("Content-Type")
1919
mediaType, _, err := mime.ParseMediaType(contentType)
2020
if err != nil {
@@ -26,14 +26,11 @@ func readRequestJSON(target any, req *http.Request) error {
2626

2727
dec := json.NewDecoder(req.Body)
2828
dec.DisallowUnknownFields()
29-
if err := dec.Decode(target); err != nil {
30-
return err
31-
}
32-
return nil
29+
return dec.Decode(target)
3330
}
3431

3532
// renderJSON renders 'v' as JSON and writes it as a response into w.
36-
func renderJSON(w http.ResponseWriter, v interface{}) {
33+
func renderJSON(w http.ResponseWriter, v any) {
3734
js, err := json.Marshal(v)
3835
if err != nil {
3936
http.Error(w, err.Error(), http.StatusInternalServerError)

ragserver/ragserver/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252

5353
mux := http.NewServeMux()
5454
mux.HandleFunc("POST /add/", server.addDocumentsHandler)
55-
mux.HandleFunc("GET /query/", server.queryHandler)
55+
mux.HandleFunc("POST /query/", server.queryHandler)
5656

5757
port := cmp.Or(os.Getenv("SERVERPORT"), "9020")
5858
address := "localhost:" + port
@@ -77,7 +77,7 @@ func (rs *ragServer) addDocumentsHandler(w http.ResponseWriter, req *http.Reques
7777
}
7878
ar := &addRequest{}
7979

80-
err := readRequestJSON(ar, req)
80+
err := readRequestJSON(req, ar)
8181
if err != nil {
8282
http.Error(w, err.Error(), http.StatusBadRequest)
8383
return
@@ -127,7 +127,7 @@ func (rs *ragServer) queryHandler(w http.ResponseWriter, req *http.Request) {
127127
Content string
128128
}
129129
qr := &queryRequest{}
130-
err := readRequestJSON(qr, req)
130+
err := readRequestJSON(req, qr)
131131
if err != nil {
132132
http.Error(w, err.Error(), http.StatusBadRequest)
133133
return

0 commit comments

Comments
 (0)