MCPcopy Create free account
hub / github.com/couchbase/cbft / ServeHTTP

Method ServeHTTP

http/search.go:33–82  ·  view source on GitHub ↗
(w http.ResponseWriter, req *http.Request)

Source from the content-addressed store, hash-verified

31}
32
33func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
34
35 // find the index to operate on
36 var indexName string
37 if h.IndexNameLookup != nil {
38 indexName = h.IndexNameLookup(req)
39 }
40 if indexName == "" {
41 indexName = h.defaultIndexName
42 }
43 index := IndexByName(indexName)
44 if index == nil {
45 showError(w, req, fmt.Sprintf("no such index '%s'", indexName), 404)
46 return
47 }
48
49 // read the request body
50 requestBody, err := ioutil.ReadAll(req.Body)
51 if err != nil {
52 showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400)
53 return
54 }
55
56 // parse the request
57 var searchRequest bleve.SearchRequest
58 err = json.Unmarshal(requestBody, &searchRequest)
59 if err != nil {
60 showError(w, req, fmt.Sprintf("error parsing query: %v", err), 400)
61 return
62 }
63
64 // validate the query
65 if srqv, ok := searchRequest.Query.(query.ValidatableQuery); ok {
66 err = srqv.Validate()
67 if err != nil {
68 showError(w, req, fmt.Sprintf("error validating query: %v", err), 400)
69 return
70 }
71 }
72
73 // execute the query
74 searchResponse, err := index.Search(&searchRequest)
75 if err != nil {
76 showError(w, req, fmt.Sprintf("error executing query: %v", err), 500)
77 return
78 }
79
80 // encode the response
81 mustEncode(w, searchResponse)
82}

Callers

nothing calls this directly

Calls 5

IndexByNameFunction · 0.85
showErrorFunction · 0.85
UnmarshalMethod · 0.80
mustEncodeFunction · 0.70
SearchMethod · 0.65

Tested by

no test coverage detected