Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/search_handler.go
akuma06 9307087024 Refine form linked up & edited buildversion to commit hash (#1025)
Added the use of the new search form made by @kipukun
Buildversion are now commit hash. Please read the readme for new build
command (or just build using package.sh).
2017-06-20 10:06:01 +10:00

64 lignes
1,7 Kio
Go

package router
import (
"html"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/NyaaPantsu/nyaa/model"
"github.com/NyaaPantsu/nyaa/util/log"
msg "github.com/NyaaPantsu/nyaa/util/messages"
"github.com/NyaaPantsu/nyaa/util/search"
)
// SearchHandler : Controller for displaying search result page, accepting common search arguments
func SearchHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var err error
// TODO Don't create a new client for each request
messages := msg.GetMessages(r)
// TODO Fallback to postgres search if es is down
vars := mux.Vars(r)
page := vars["page"]
// db params url
pagenum := 1
if page != "" {
pagenum, err = strconv.Atoi(html.EscapeString(page))
if !log.CheckError(err) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if pagenum <= 0 {
NotFoundHandler(w, r)
return
}
}
searchParam, torrents, nbTorrents, err := search.SearchByQuery(r, pagenum)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
commonVar := newCommonVariables(r)
commonVar.Navigation = navigation{int(nbTorrents), int(searchParam.Max), int(searchParam.Page), "search_page"}
// Convert back to strings for now.
// TODO Deprecate fully SearchParam and only use TorrentParam
category := ""
if len(searchParam.Category) > 0 {
category = searchParam.Category[0].String()
}
commonVar.Search.SearchParam, commonVar.Search.Category = searchParam, category
htv := modelListVbs{commonVar, model.TorrentsToJSON(torrents), messages.GetAllErrors(), messages.GetAllInfos()}
err = searchTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}