MCPcopy
hub / github.com/elastic/go-elasticsearch / ServeHTTP

Method ServeHTTP

_examples/xkcdsearch/http.go:44–74  ·  view source on GitHub ↗

ServeHTTP is a handler returning search results in JSON.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

42// ServeHTTP is a handler returning search results in JSON.
43//
44func (s *Store) ServeHTTP(w http.ResponseWriter, r *http.Request) {
45 w.Header().Set("Content-Type", "application/json")
46
47 query := r.URL.Query().Get("q")
48 after := r.URL.Query().Get("a")
49 if len(r.URL.RawQuery) > 0 {
50 r.URL.RawQuery = fmt.Sprintf("?%s", r.URL.RawQuery)
51 }
52 httpLog.Info().Msg(fmt.Sprintf("%s %s%s", r.Method, r.URL.Path, r.URL.RawQuery))
53
54 results, err := s.Search(query, after)
55 if err != nil {
56 httpLog.Error().Err(err).Msg("Failed to search the index")
57 http.Error(w, fmt.Sprintf(`{ "error" : %q }`, err), http.StatusInternalServerError)
58 return
59 }
60
61 out, err := json.Marshal(results)
62 if err != nil {
63 httpLog.Error().Err(err).Msg("Failed to encode results to JSON")
64 http.Error(w, fmt.Sprintf(`{ "error" : %q }`, err), http.StatusInternalServerError)
65 return
66 }
67
68 _, err = io.Copy(w, bytes.NewReader(out))
69 if err != nil {
70 httpLog.Error().Err(err).Msg("Failed to write to response")
71 http.Error(w, fmt.Sprintf(`{ "error" : %q }`, err), http.StatusInternalServerError)
72 return
73 }
74}

Callers 2

SPNEGOAuthMiddlewareFunction · 0.80
startServerFunction · 0.80

Calls 7

SearchMethod · 0.95
SetMethod · 0.45
HeaderMethod · 0.45
GetMethod · 0.45
QueryMethod · 0.45
InfoMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected