~ubuntu-branches/debian/sid/golang-github-blevesearch-bleve/sid

« back to all changes in this revision

Viewing changes to http/fields.go

  • Committer: Package Import Robot
  • Author(s): Michael Lustfield
  • Date: 2017-03-30 16:06:03 UTC
  • Revision ID: package-import@ubuntu.com-20170330160603-0oogmb960l7918jx
Tags: upstream-0.5.0+git20170324.202.4702785f
ImportĀ upstreamĀ versionĀ 0.5.0+git20170324.202.4702785f

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright (c) 2014 Couchbase, Inc.
 
2
//
 
3
// Licensed under the Apache License, Version 2.0 (the "License");
 
4
// you may not use this file except in compliance with the License.
 
5
// You may obtain a copy of the License at
 
6
//
 
7
//              http://www.apache.org/licenses/LICENSE-2.0
 
8
//
 
9
// Unless required by applicable law or agreed to in writing, software
 
10
// distributed under the License is distributed on an "AS IS" BASIS,
 
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
// See the License for the specific language governing permissions and
 
13
// limitations under the License.
 
14
 
 
15
package http
 
16
 
 
17
import (
 
18
        "fmt"
 
19
        "net/http"
 
20
)
 
21
 
 
22
type ListFieldsHandler struct {
 
23
        defaultIndexName string
 
24
        IndexNameLookup  varLookupFunc
 
25
}
 
26
 
 
27
func NewListFieldsHandler(defaultIndexName string) *ListFieldsHandler {
 
28
        return &ListFieldsHandler{
 
29
                defaultIndexName: defaultIndexName,
 
30
        }
 
31
}
 
32
 
 
33
func (h *ListFieldsHandler) 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
        fields, err := index.Fields()
 
50
        if err != nil {
 
51
                showError(w, req, fmt.Sprintf("error: %v", err), 500)
 
52
                return
 
53
        }
 
54
 
 
55
        fieldsResponse := struct {
 
56
                Fields []string `json:"fields"`
 
57
        }{
 
58
                Fields: fields,
 
59
        }
 
60
 
 
61
        // encode the response
 
62
        mustEncode(w, fieldsResponse)
 
63
}