コード例 #1
0
ファイル: serve_search.go プロジェクト: oblank/knowledgedb
func (s *httpServer) serveSearchEntries(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	res, err := index.Search(r.Form["query"][0])
	if err != nil {
		http.Error(w, "Bad Request", http.StatusBadRequest)
		log.Warning(err)
		return
	}
	searchRes := make([]*SearchResult, 0, len(res.Hits))
	for _, hit := range res.Hits {
		entry := storage.LoadEntry(hit.ID)
		if entry != nil {
			title := template.HTML(tt.HTMLEscapeString(entry.Title))
			if thit, ok := hit.Fragments["Title"]; ok {
				title = template.HTML(thit[0])
			}

			searchHits, ok := hit.Fragments["Body"]
			result := &SearchResult{
				Id:    hit.ID,
				Title: title,
				Tags:  entry.Tags,
			}
			if ok {
				hitTexts := make([]template.HTML, 0, len(searchHits))
				for _, hitText := range searchHits {
					hitTexts = append(hitTexts, template.HTML(hitText))
				}
				result.Texts = hitTexts
			} else {
				result.Texts = []template.HTML{
					template.HTML(tt.HTMLEscapeString(entry.GetBodySnippet())),
				}
			}

			searchRes = append(searchRes, result)
		}
	}
	renderTemplate(w, "search_result", searchRes)
}
コード例 #2
0
ファイル: serve_entries.go プロジェクト: oblank/knowledgedb
func (s *httpServer) withDoc(fn func(id string, entry *storage.Entry, w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		id, ok := mux.Vars(r)["id"]
		if !ok {
			http.Error(w, "Bad Request", http.StatusBadRequest)
			return
		}

		if _, err := uuid.ParseHex(id); err != nil {
			http.Error(w, "Bad Request", http.StatusBadRequest)
			log.Warningf("Bad Document ID Request: %v", id)
			return
		}

		entry := storage.LoadEntry(id)
		if entry == nil {
			http.Error(w, "Bad Request", http.StatusBadRequest)
			log.Warningf("Document %v not found", id)
			return
		}

		fn(id, entry, w, r)
	}
}