diff --git a/router/api_handler.go b/router/api_handler.go index 34eae5e0..3902140f 100644 --- a/router/api_handler.go +++ b/router/api_handler.go @@ -18,6 +18,7 @@ import ( "github.com/NyaaPantsu/nyaa/service/upload" "github.com/NyaaPantsu/nyaa/util" "github.com/NyaaPantsu/nyaa/util/log" + "github.com/NyaaPantsu/nyaa/util/search" "github.com/gorilla/mux" ) @@ -261,3 +262,38 @@ func ApiUpdateHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%+v\n", torrent) } } +func ApiSearchHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + page := vars["page"] + + // db params url + var err error + 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 + } + } + + _, torrents, _, err := search.SearchByQuery(r, pagenum) + if err != nil { + util.SendError(w, err, 400) + return + } + + b := model.TorrentsToJSON(torrents) + w.Header().Set("Content-Type", "application/json") + err = json.NewEncoder(w).Encode(b) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + +} diff --git a/router/router.go b/router/router.go index 9729bb08..c118ec11 100755 --- a/router/router.go +++ b/router/router.go @@ -48,6 +48,8 @@ func init() { Router.Handle("/api/view/{id}", wrapHandler(gzipAPIViewHandler)).Methods("GET") Router.HandleFunc("/api/view/{id}", ApiViewHeadHandler).Methods("HEAD") Router.HandleFunc("/api/upload", ApiUploadHandler).Methods("POST") + Router.HandleFunc("/api/search", ApiSearchHandler) + Router.HandleFunc("/api/search/{page}", ApiSearchHandler) Router.HandleFunc("/api/update", ApiUpdateHandler).Methods("PUT") Router.HandleFunc("/faq", FaqHandler).Name("faq") Router.HandleFunc("/feed", RSSHandler).Name("feed") @@ -95,7 +97,7 @@ func init() { Router.HandleFunc("/mod/comment/delete", WrapModHandler(CommentDeleteModPanel)).Name("mod_cdelete") Router.HandleFunc("/mod/reassign", WrapModHandler(TorrentReassignModPanel)).Name("mod_treassign").Methods("GET") Router.HandleFunc("/mod/reassign", WrapModHandler(TorrentPostReassignModPanel)).Name("mod_treassign").Methods("POST") - Router.HandleFunc("/mod/api/torrents", WrapModHandler(ApiMassMod)).Name("mod_tapi").Methods("POST") + Router.HandleFunc("/mod/api/torrents", WrapModHandler(ApiMassMod)).Name("mod_tapi").Methods("POST") //reporting a torrent Router.HandleFunc("/report/{id}", ReportTorrentHandler).Methods("POST").Name("torrent_report")