0
0
Fork 0

removes dependency on mux from bleve.http

handlers now use varLookupFunc's to get variable values from
the request object.  this allows applications to use whatever
mux/router they want, and extracting variables like
indexName and docID is up to the caller-provided function

closes #113
This commit is contained in:
Marty Schoch 2014-10-31 14:44:15 -04:00
parent 3f83149ed3
commit 5f40396ce8
12 changed files with 79 additions and 42 deletions

View File

@ -14,14 +14,14 @@ import (
"net/http"
"github.com/blevesearch/bleve/index/upside_down"
"github.com/gorilla/mux"
)
// DebugDocumentHandler allows you to debug the index content
// for a given document id. the document ID should be mapped
// to the mux router URL with name "docId"
// for a given document id.
type DebugDocumentHandler struct {
defaultIndexName string
IndexNameLookup varLookupFunc
DocIDLookup varLookupFunc
}
func NewDebugDocumentHandler(defaultIndexName string) *DebugDocumentHandler {
@ -33,7 +33,10 @@ func NewDebugDocumentHandler(defaultIndexName string) *DebugDocumentHandler {
func (h *DebugDocumentHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the index to operate on
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
indexName = h.defaultIndexName
}
@ -44,7 +47,10 @@ func (h *DebugDocumentHandler) ServeHTTP(w http.ResponseWriter, req *http.Reques
}
// find the docID
docID := mux.Vars(req)["docID"]
var docID string
if h.DocIDLookup != nil {
docID = h.DocIDLookup(req)
}
rv := make([]interface{}, 0)
rowChan := index.DumpDoc(docID)

View File

@ -12,12 +12,11 @@ package http
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type DocCountHandler struct {
defaultIndexName string
IndexNameLookup varLookupFunc
}
func NewDocCountHandler(defaultIndexName string) *DocCountHandler {
@ -28,7 +27,10 @@ func NewDocCountHandler(defaultIndexName string) *DocCountHandler {
func (h *DocCountHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the index to operate on
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
indexName = h.defaultIndexName
}

View File

@ -12,12 +12,12 @@ package http
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type DocDeleteHandler struct {
defaultIndexName string
IndexNameLookup varLookupFunc
DocIDLookup varLookupFunc
}
func NewDocDeleteHandler(defaultIndexName string) *DocDeleteHandler {
@ -29,7 +29,10 @@ func NewDocDeleteHandler(defaultIndexName string) *DocDeleteHandler {
func (h *DocDeleteHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the index to operate on
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
indexName = h.defaultIndexName
}
@ -40,7 +43,10 @@ func (h *DocDeleteHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
// find the doc id
docID := mux.Vars(req)["docID"]
var docID string
if h.DocIDLookup != nil {
docID = h.DocIDLookup(req)
}
if docID == "" {
showError(w, req, "document id cannot be empty", 400)
return

View File

@ -15,12 +15,12 @@ import (
"time"
"github.com/blevesearch/bleve/document"
"github.com/gorilla/mux"
)
type DocGetHandler struct {
defaultIndexName string
IndexNameLookup varLookupFunc
DocIDLookup varLookupFunc
}
func NewDocGetHandler(defaultIndexName string) *DocGetHandler {
@ -31,7 +31,10 @@ func NewDocGetHandler(defaultIndexName string) *DocGetHandler {
func (h *DocGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the index to operate on
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
indexName = h.defaultIndexName
}
@ -42,7 +45,10 @@ func (h *DocGetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
// find the doc id
docID := mux.Vars(req)["docID"]
var docID string
if h.DocIDLookup != nil {
docID = h.DocIDLookup(req)
}
if docID == "" {
showError(w, req, "document id cannot be empty", 400)
return

View File

@ -13,12 +13,12 @@ import (
"fmt"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
)
type DocIndexHandler struct {
defaultIndexName string
IndexNameLookup varLookupFunc
DocIDLookup varLookupFunc
}
func NewDocIndexHandler(defaultIndexName string) *DocIndexHandler {
@ -30,7 +30,10 @@ func NewDocIndexHandler(defaultIndexName string) *DocIndexHandler {
func (h *DocIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the index to operate on
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
indexName = h.defaultIndexName
}
@ -41,7 +44,10 @@ func (h *DocIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
// find the doc id
docID := mux.Vars(req)["docID"]
var docID string
if h.DocIDLookup != nil {
docID = h.DocIDLookup(req)
}
if docID == "" {
showError(w, req, "document id cannot be empty", 400)
return

View File

@ -12,12 +12,11 @@ package http
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type ListFieldsHandler struct {
defaultIndexName string
IndexNameLookup varLookupFunc
}
func NewListFieldsHandler(defaultIndexName string) *ListFieldsHandler {
@ -29,7 +28,10 @@ func NewListFieldsHandler(defaultIndexName string) *ListFieldsHandler {
func (h *ListFieldsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the index to operate on
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
indexName = h.defaultIndexName
}

View File

@ -17,12 +17,11 @@ import (
"os"
"github.com/blevesearch/bleve"
"github.com/gorilla/mux"
)
type CreateIndexHandler struct {
basePath string
basePath string
IndexNameLookup varLookupFunc
}
func NewCreateIndexHandler(basePath string) *CreateIndexHandler {
@ -33,7 +32,10 @@ func NewCreateIndexHandler(basePath string) *CreateIndexHandler {
func (h *CreateIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the name of the index to create
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
showError(w, req, "index name is required", 400)
return

View File

@ -13,12 +13,11 @@ import (
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
)
type DeleteIndexHandler struct {
basePath string
basePath string
IndexNameLookup varLookupFunc
}
func NewDeleteIndexHandler(basePath string) *DeleteIndexHandler {
@ -29,7 +28,10 @@ func NewDeleteIndexHandler(basePath string) *DeleteIndexHandler {
func (h *DeleteIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the name of the index to delete
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
showError(w, req, "index name is required", 400)
return

View File

@ -14,11 +14,11 @@ import (
"net/http"
"github.com/blevesearch/bleve"
"github.com/gorilla/mux"
)
type GetIndexHandler struct{}
type GetIndexHandler struct {
IndexNameLookup varLookupFunc
}
func NewGetIndexHandler() *GetIndexHandler {
return &GetIndexHandler{}
@ -26,7 +26,10 @@ func NewGetIndexHandler() *GetIndexHandler {
func (h *GetIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the name of the index to create
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
showError(w, req, "index name is required", 400)
return

View File

@ -13,7 +13,8 @@ import (
"net/http"
)
type ListIndexesHandler struct{}
type ListIndexesHandler struct {
}
func NewListIndexesHandler() *ListIndexesHandler {
return &ListIndexesHandler{}

View File

@ -16,17 +16,13 @@ import (
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/blevesearch/bleve"
)
// SearchHandler can handle search requests sent over HTTP
// the index name can be selected in the URL by mapping a
// gorilla mux var, or it can be set manually with by
// setting the defaultIndex value
type SearchHandler struct {
defaultIndexName string
IndexNameLookup varLookupFunc
}
func NewSearchHandler(defaultIndexName string) *SearchHandler {
@ -38,7 +34,10 @@ func NewSearchHandler(defaultIndexName string) *SearchHandler {
func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// find the index to operate on
indexName := mux.Vars(req)["indexName"]
var indexName string
if h.IndexNameLookup != nil {
indexName = h.IndexNameLookup(req)
}
if indexName == "" {
indexName = h.defaultIndexName
}

View File

@ -33,3 +33,5 @@ func mustEncode(w io.Writer, i interface{}) {
panic(err)
}
}
type varLookupFunc func(req *http.Request) string