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

Method ServeHTTP

http/doc_get.go:32–108  ·  view source on GitHub ↗
(w http.ResponseWriter, req *http.Request)

Source from the content-addressed store, hash-verified

30}
31
32func (h *DocGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
33 // find the index to operate on
34 var indexName string
35 if h.IndexNameLookup != nil {
36 indexName = h.IndexNameLookup(req)
37 }
38 if indexName == "" {
39 indexName = h.defaultIndexName
40 }
41 idx := IndexByName(indexName)
42 if idx == nil {
43 showError(w, req, fmt.Sprintf("no such index '%s'", indexName), 404)
44 return
45 }
46
47 // find the doc id
48 var docID string
49 if h.DocIDLookup != nil {
50 docID = h.DocIDLookup(req)
51 }
52 if docID == "" {
53 showError(w, req, "document id cannot be empty", 400)
54 return
55 }
56
57 doc, err := idx.Document(docID)
58 if err != nil {
59 showError(w, req, fmt.Sprintf("error deleting document '%s': %v", docID, err), 500)
60 return
61 }
62 if doc == nil {
63 showError(w, req, fmt.Sprintf("no such document '%s'", docID), 404)
64 return
65 }
66
67 rv := struct {
68 ID string `json:"id"`
69 Fields map[string]interface{} `json:"fields"`
70 }{
71 ID: docID,
72 Fields: map[string]interface{}{},
73 }
74
75 doc.VisitFields(func(field index.Field) {
76 var newval interface{}
77 switch field := field.(type) {
78 case *document.TextField:
79 newval = string(field.Value())
80 case *document.NumericField:
81 n, err := field.Number()
82 if err == nil {
83 newval = n
84 }
85 case *document.DateTimeField:
86 d, err := field.DateTime()
87 if err == nil {
88 newval = d.Format(time.RFC3339Nano)
89 }

Callers

nothing calls this directly

Calls 5

IndexByNameFunction · 0.85
showErrorFunction · 0.85
mustEncodeFunction · 0.70
DocumentMethod · 0.45
NameMethod · 0.45

Tested by

no test coverage detected