|
|
@@ -7,6 +7,7 @@ import (
|
|
|
"github.com/gorilla/mux"
|
|
|
"github.com/huichen/wukong/types"
|
|
|
"net/http"
|
|
|
+ "os"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
)
|
|
|
@@ -43,7 +44,9 @@ func startHttp(addr string) error {
|
|
|
v1.HandleFunc("/delete_index", delete_index_v1).Methods("POST")
|
|
|
v1.HandleFunc("/search", search_v1).Methods("GET").Queries("keywords", "{keywords}",
|
|
|
"offset", "{offset:[0-9]+}", "limit", "{offset:[0-9]+}")
|
|
|
- v1.HandleFunc("/shutdown", shutdown).Methods("POST")
|
|
|
+ v1.HandleFunc("/shutdown", shutdown_v1).Methods("POST")
|
|
|
+ v1.HandleFunc("/stat", stat_v1).Methods("GET")
|
|
|
+ v1.HandleFunc("/index/{docId}", index_get_v1).Methods("GET")
|
|
|
router.Use(mux.CORSMethodMiddleware(router))
|
|
|
|
|
|
_httpServer = &http.Server{
|
|
|
@@ -55,7 +58,7 @@ func startHttp(addr string) error {
|
|
|
|
|
|
go func() {
|
|
|
if err := _httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
- fmt.Println("http server error: ", err)
|
|
|
+ logger.Error().Err(err).Msg("http server error")
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
@@ -82,7 +85,8 @@ func index_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
return
|
|
|
}
|
|
|
- fmt.Println("indexing:", param)
|
|
|
+ logger.Trace().Interface("param", param).Msg("new index")
|
|
|
+
|
|
|
searcher.IndexDocumentS(param.DocId, types.DocumentIndexData{
|
|
|
Content: param.Content,
|
|
|
}, param.ForceUpdate)
|
|
|
@@ -91,6 +95,10 @@ func index_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
_, err = w.Write([]byte(`{"ec":0,"result":{}}`))
|
|
|
}
|
|
|
|
|
|
+func index_get_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
// delete index
|
|
|
func delete_index_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
var param ParamDelete
|
|
|
@@ -99,7 +107,7 @@ func delete_index_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
return
|
|
|
}
|
|
|
- fmt.Println("removing index:", param)
|
|
|
+ logger.Trace().Interface("param", param).Msg("delete index")
|
|
|
|
|
|
searcher.RemoveDocumentS(param.DocId, param.ForceUpdate)
|
|
|
|
|
|
@@ -113,7 +121,9 @@ func search_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
keywords := params["keywords"]
|
|
|
offset, _ := strconv.ParseInt(params["offset"], 10, 64)
|
|
|
limit, _ := strconv.ParseInt(params["limit"], 10, 64)
|
|
|
- fmt.Println("searching:", params)
|
|
|
+
|
|
|
+ logger.Trace().Interface("param", params).Msg("search")
|
|
|
+
|
|
|
resp := searcher.Search(types.SearchRequest{
|
|
|
Text: keywords,
|
|
|
RankOptions: &types.RankOptions{
|
|
|
@@ -130,6 +140,11 @@ func search_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
fmt.Fprintf(w, `{"ec":0,"result":%s}`, string(sresp))
|
|
|
}
|
|
|
|
|
|
-func shutdown(w http.ResponseWriter, r *http.Request) {
|
|
|
+func shutdown_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
+ logger.Trace().Msg("shutdown")
|
|
|
+ chSig <- os.Interrupt
|
|
|
+}
|
|
|
|
|
|
+func stat_v1(w http.ResponseWriter, r *http.Request) {
|
|
|
+ logger.Trace().Msg("stat")
|
|
|
}
|