示例#1
0
func things(pageResolver *ranklib.PageResolver, w http.ResponseWriter, r *http.Request) {
	things, ok := r.URL.Query()["things[]"]
	if !ok || len(things) != 2 {
		http.Error(w, "Bad search", http.StatusBadRequest)
		return
	}

	ret := make([]interface{}, 0, len(things))
	for _, name := range things {
		page, ok := pageResolver.PageByTitle(name)
		if !ok {
			http.Error(w, fmt.Sprintf("%s doesn't exist", name), http.StatusBadRequest)
			return
		}
		ret = append(ret, page)
	}

	responseObject, err := json.Marshal(ret)

	if err != nil {
		http.Error(w, "Unable to jsonify all things", http.StatusInternalServerError)
		return
	}

	response := string(responseObject)
	w.Header().Set("Content-Length", strconv.Itoa(len(response)))
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	fmt.Fprintf(w, response)
}
示例#2
0
func namedEntitySuggestions(pageResolver *ranklib.PageResolver, w http.ResponseWriter, r *http.Request) {
	suggestions := map[string]interface{}{}
	search, ok := r.URL.Query()["q"]
	log.Printf("Search is %s from URL %s", search, r.URL)
	if !ok || len(search) != 1 {
		http.Error(w, "Bad search prefix", http.StatusBadRequest)
		return
	}

	suggestions["suggestions"] = pageResolver.PrefixSuggestions(search[0], 10)

	responseObject, err := json.Marshal(suggestions)
	if err != nil {
		http.Error(w, "Unable to jsonify suggestions", http.StatusInternalServerError)
		return
	}
	response := string(responseObject)
	w.Header().Set("Content-Length", strconv.Itoa(len(response)))
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	fmt.Fprintf(w, response)
}
示例#3
0
func index(pageResolver *ranklib.PageResolver, w http.ResponseWriter, r *http.Request) {
	templateName := fmt.Sprintf("%s/index.gotemplate.html", templateDir)
	t := template.New("index")
	t.Funcs(helpers)
	_, err := t.ParseFiles(templateName)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	log.Printf("%s", t)

	categories := make([]IndexCategory, 0, 1000)
	for _, categoryResolver := range pageResolver.GetCategories() {
		pages := make([]IndexCategoryPage, 0, 1000)
		for i, page := range categoryResolver.PageResolver.OrderedPageRange(0, 1000) {
			percentDecrease := 0.0
			if i > 0 {
				percentDecrease = 100 * float64(page.Rank/pages[0].Page.Rank)
			}

			pages = append(pages, IndexCategoryPage{
				Page:            page,
				PercentDecrease: percentDecrease,
			})
		}

		categories = append(categories, IndexCategory{
			CategoryName: categoryResolver.CategoryName,
			TopTen:       pages,
		})
	}

	templateData := IndexData{categories}
	err = t.ExecuteTemplate(w, "index.gotemplate.html", templateData)
	if err != nil {
		log.Printf(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

}