Ver código fonte

[GinS] Gin Search API functionality

cgars 7 anos atrás
pai
commit
6d5581e41f
2 arquivos alterados com 45 adições e 0 exclusões
  1. 3 0
      routes/api/v1/api.go
  2. 42 0
      routes/api/v1/search/general.go

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

@@ -21,6 +21,7 @@ import (
 	"github.com/G-Node/gogs/routes/api/v1/org"
 	"github.com/G-Node/gogs/routes/api/v1/repo"
 	"github.com/G-Node/gogs/routes/api/v1/user"
+	"github.com/G-Node/gogs/routes/api/v1/search"
 )
 
 func repoAssignment() macaron.Handler {
@@ -181,6 +182,8 @@ func RegisterRoutes(m *macaron.Macaron) {
 		m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
 		m.Post("/markdown/raw", misc.MarkdownRaw)
 
+		m.Get("/search/:querry", search.Search)
+
 		// Users
 		m.Group("/users", func() {
 			m.Get("/search", user.Search)

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

@@ -0,0 +1,42 @@
+package search
+
+import (
+	"github.com/G-Node/gogs/pkg/context"
+	"github.com/G-Node/gin-dex/gindex"
+	"net/http"
+	"github.com/G-Node/gogs/pkg/setting"
+	"encoding/json"
+	"bytes"
+	"io/ioutil"
+)
+
+func Search(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)}
+	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)
+}