2020-06-30 20:56:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-07-04 13:07:32 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-06-30 20:56:12 +00:00
|
|
|
"net/http"
|
2020-07-12 16:01:32 +00:00
|
|
|
"os"
|
2020-07-01 14:40:10 +00:00
|
|
|
"strings"
|
2020-06-30 20:56:12 +00:00
|
|
|
|
|
|
|
"github.com/blevesearch/bleve"
|
|
|
|
"github.com/blevesearch/bleve/mapping"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: http handler
|
|
|
|
// TODO: index all pages on start
|
|
|
|
// TODO: reindex all command
|
|
|
|
// TODO: search(query) command
|
|
|
|
|
|
|
|
type searchHandler struct {
|
|
|
|
indexMapping mapping.IndexMapping
|
|
|
|
searchIndex bleve.Index
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:40:10 +00:00
|
|
|
type nameLine struct {
|
2020-07-12 16:01:32 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Line string `json:"line"`
|
2020-07-01 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type searchObject struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Blocks []string `json:"blocks"`
|
|
|
|
Refs []nameLine `json:"refs"`
|
|
|
|
Meta map[string]string `json:"meta"`
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:56:12 +00:00
|
|
|
func NewSearchHandler(searchIndex bleve.Index) (http.Handler, error) {
|
|
|
|
return &searchHandler{
|
2020-07-01 14:40:10 +00:00
|
|
|
searchIndex: searchIndex,
|
2020-06-30 20:56:12 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2020-07-04 13:07:32 +00:00
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if !sess.LoggedIn {
|
|
|
|
fmt.Fprint(w, "{}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-12 16:01:32 +00:00
|
|
|
if r.URL.Query().Get("fields") == "1" {
|
|
|
|
fields, err := s.searchIndex.Fields()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
err = enc.Encode(&fields)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if r.Method == "GET" && r.URL.Query().Get("reset") == "1" {
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
fmt.Fprint(w, `<form action="/search/" method="post"><input type=hidden name=reset value=1><input type=submit value="Reset Index"></form>`)
|
|
|
|
return
|
|
|
|
} else if r.Method == "POST" {
|
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.PostForm.Get("reset") == "1" {
|
|
|
|
|
|
|
|
refs := make(Refs)
|
|
|
|
mp := NewFilePages("data", nil)
|
|
|
|
|
|
|
|
pages, err := mp.AllPages()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, page := range pages {
|
|
|
|
err = processBackrefsForPage(page, refs)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error while processing backrefs: ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = saveBackrefs("data/backrefs.json", refs)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while saving backrefs %w", err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll("data/_tmp_index")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while remove old index %w", err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
indexMapping := bleve.NewIndexMapping()
|
|
|
|
index, err := bleve.New("data/_tmp_index", indexMapping)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, page := range pages {
|
|
|
|
so, err := createSearchObject(page)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while createing search object %s: %w", page.Title, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = index.Index(page.Name, so)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while indexing %s: %w", page.Title, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Rename("data/_page-index", "data/_page-index-old")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while resetting index: %w", err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = os.Rename("data/_tmp_index", "data/_page-index")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while putthing new index in place: %w", err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = os.RemoveAll("data/_page-index-old")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while remove old index %w", err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
err = enc.Encode(struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
}{Ok: true})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:56:12 +00:00
|
|
|
q := bleve.NewQueryStringQuery(r.URL.Query().Get("q"))
|
|
|
|
sr := bleve.NewSearchRequest(q)
|
|
|
|
results, err := s.searchIndex.Search(sr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
err = enc.Encode(&results)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 14:40:10 +00:00
|
|
|
|
|
|
|
func createSearchObject(page Page) (searchObject, error) {
|
|
|
|
so := searchObject{}
|
|
|
|
so.Title = page.Title
|
|
|
|
so.Meta = make(map[string]string)
|
|
|
|
|
|
|
|
type simpleListItem struct {
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
var listItems []simpleListItem
|
|
|
|
if err := json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems); err != nil {
|
|
|
|
so.Blocks = append(so.Blocks, page.Content)
|
|
|
|
} else {
|
|
|
|
for _, li := range listItems {
|
|
|
|
meta := strings.SplitN(li.Text, "::", 2)
|
|
|
|
if len(meta) == 2 {
|
2020-07-19 15:20:38 +00:00
|
|
|
key := strings.ToLower(strings.TrimSpace(meta[0]))
|
|
|
|
value := strings.ToLower(strings.TrimSpace(meta[1]))
|
|
|
|
if (key == "title") {
|
|
|
|
so.Title = value
|
|
|
|
}
|
|
|
|
so.Meta[key] = value
|
2020-07-01 14:40:10 +00:00
|
|
|
}
|
|
|
|
so.Blocks = append(so.Blocks, li.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 16:01:32 +00:00
|
|
|
for _, refs := range page.Refs {
|
2020-07-01 14:40:10 +00:00
|
|
|
for _, ref := range refs {
|
|
|
|
so.Refs = append(so.Refs, nameLine{
|
2020-07-12 16:01:32 +00:00
|
|
|
ref.Name,
|
|
|
|
ref.Title,
|
2020-07-01 14:40:10 +00:00
|
|
|
ref.Line,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return so, nil
|
|
|
|
}
|