Skip to content

Commit

Permalink
ragserver: fix other variants to be similar to ragserver-genkit
Browse files Browse the repository at this point in the history
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 <[email protected]>
Auto-Submit: Eli Bendersky <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: Eli Bendersky <[email protected]>
  • Loading branch information
eliben authored and gopherbot committed Sep 3, 2024
1 parent 88ba6f5 commit 0349f37
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
9 changes: 3 additions & 6 deletions ragserver/ragserver-langchaingo/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// readRequestJSON expects req to have a JSON content type with a body that
// contains a JSON-encoded value complying with the underlying type of target.
// It populates target, or returns an error.
func readRequestJSON(target any, req *http.Request) error {
func readRequestJSON(req *http.Request, target any) error {
contentType := req.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
Expand All @@ -26,14 +26,11 @@ func readRequestJSON(target any, req *http.Request) error {

dec := json.NewDecoder(req.Body)
dec.DisallowUnknownFields()
if err := dec.Decode(target); err != nil {
return err
}
return nil
return dec.Decode(target)
}

// renderJSON renders 'v' as JSON and writes it as a response into w.
func renderJSON(w http.ResponseWriter, v interface{}) {
func renderJSON(w http.ResponseWriter, v any) {
js, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
6 changes: 3 additions & 3 deletions ragserver/ragserver-langchaingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {

mux := http.NewServeMux()
mux.HandleFunc("POST /add/", server.addDocumentsHandler)
mux.HandleFunc("GET /query/", server.queryHandler)
mux.HandleFunc("POST /query/", server.queryHandler)

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

err := readRequestJSON(ar, req)
err := readRequestJSON(req, ar)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand All @@ -108,7 +108,7 @@ func (rs *ragServer) queryHandler(w http.ResponseWriter, req *http.Request) {
Content string
}
qr := &queryRequest{}
err := readRequestJSON(qr, req)
err := readRequestJSON(req, qr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
9 changes: 3 additions & 6 deletions ragserver/ragserver/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// readRequestJSON expects req to have a JSON content type with a body that
// contains a JSON-encoded value complying with the underlying type of target.
// It populates target, or returns an error.
func readRequestJSON(target any, req *http.Request) error {
func readRequestJSON(req *http.Request, target any) error {
contentType := req.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
Expand All @@ -26,14 +26,11 @@ func readRequestJSON(target any, req *http.Request) error {

dec := json.NewDecoder(req.Body)
dec.DisallowUnknownFields()
if err := dec.Decode(target); err != nil {
return err
}
return nil
return dec.Decode(target)
}

// renderJSON renders 'v' as JSON and writes it as a response into w.
func renderJSON(w http.ResponseWriter, v interface{}) {
func renderJSON(w http.ResponseWriter, v any) {
js, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
6 changes: 3 additions & 3 deletions ragserver/ragserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {

mux := http.NewServeMux()
mux.HandleFunc("POST /add/", server.addDocumentsHandler)
mux.HandleFunc("GET /query/", server.queryHandler)
mux.HandleFunc("POST /query/", server.queryHandler)

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

err := readRequestJSON(ar, req)
err := readRequestJSON(req, ar)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down Expand Up @@ -127,7 +127,7 @@ func (rs *ragServer) queryHandler(w http.ResponseWriter, req *http.Request) {
Content string
}
qr := &queryRequest{}
err := readRequestJSON(qr, req)
err := readRequestJSON(req, qr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down

0 comments on commit 0349f37

Please sign in to comment.