Ver código fonte

[search] add suggest endpoint

cgars 7 anos atrás
pai
commit
e54a88102f
2 arquivos alterados com 43 adições e 0 exclusões
  1. 1 0
      routes/api/v1/api.go
  2. 42 0
      routes/api/v1/search/suggest.go

+ 1 - 0
routes/api/v1/api.go

@@ -241,6 +241,7 @@ func RegisterRoutes(m *macaron.Macaron) {
 
 
 		m.Group("/repos", func() {
 		m.Group("/repos", func() {
 			m.Get("/search", repo.Search)
 			m.Get("/search", repo.Search)
+			m.Get("/suggest/:querry", search.Suggest)
 		})
 		})
 
 
 		m.Group("/repos", func() {
 		m.Group("/repos", func() {

+ 42 - 0
routes/api/v1/search/suggest.go

@@ -0,0 +1,42 @@
+package search
+
+import (
+	"net/http"
+	"github.com/G-Node/gin-dex/gindex"
+	"encoding/json"
+	"bytes"
+	"io/ioutil"
+	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gogs/pkg/setting"
+)
+
+func Suggest(c *context.APIContext) {
+	if ! c.IsLogged {
+		c.Status(http.StatusUnauthorized)
+		return
+	}
+	if !setting.Search.Do {
+		c.Status(http.StatusNotImplemented)
+		return
+	}
+	ireq := gindex.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName), UserID: c.User.ID,
+		Querry: c.Params("querry"), CsrfT: c.GetCookie(setting.CSRFCookieName), SType:gindex.SEARCH_SUGGEST}
+	data, err := json.Marshal(ireq)
+	if err != nil {
+		c.Status(http.StatusInternalServerError)
+		return
+	}
+	req, _ := http.NewRequest("Post", setting.Search.SearchUrl, bytes.NewReader(data))
+	cl := http.Client{}
+	resp, err := cl.Do(req)
+	if err != nil {
+		c.Status(http.StatusInternalServerError)
+		return
+	}
+	data, err = ioutil.ReadAll(resp.Body)
+	if err != nil {
+		c.Status(http.StatusInternalServerError)
+		return
+	}
+	c.Write(data)
+}