소스 검색

[GinS] Extended search routes

cgars 7 년 전
부모
커밋
19bdfb40d0
2개의 변경된 파일73개의 추가작업 그리고 0개의 파일을 삭제
  1. 1 0
      cmd/web.go
  2. 72 0
      routes/search.go

+ 1 - 0
cmd/web.go

@@ -182,6 +182,7 @@ func runWeb(c *cli.Context) error {
 		m.Get("", func(c *context.Context) {
 		m.Get("", func(c *context.Context) {
 			c.Redirect(setting.AppSubURL + "/explore/repos")
 			c.Redirect(setting.AppSubURL + "/explore/repos")
 		})
 		})
+		m.Get("/data", routes.ExploreData)
 		m.Get("/repos", routes.ExploreRepos)
 		m.Get("/repos", routes.ExploreRepos)
 		m.Get("/users", routes.ExploreUsers)
 		m.Get("/users", routes.ExploreUsers)
 		m.Get("/organizations", routes.ExploreOrganizations)
 		m.Get("/organizations", routes.ExploreOrganizations)

+ 72 - 0
routes/search.go

@@ -0,0 +1,72 @@
+package routes
+
+import (
+	"github.com/G-Node/gogs/pkg/context"
+	"net/http"
+	"github.com/G-Node/gin-dex/gindex"
+	"encoding/json"
+	"bytes"
+	"io/ioutil"
+	"github.com/G-Node/gogs/pkg/setting"
+	"fmt"
+)
+
+const (
+	EXPLORE_DATA = "explore/data"
+)
+
+func Search(c *context.Context, keywords string) ([]byte, error) {
+	if ! c.IsLogged {
+		c.Status(http.StatusUnauthorized)
+		return nil, fmt.Errorf("User nor logged in")
+	}
+	if !setting.Search.Do {
+		c.Status(http.StatusNotImplemented)
+		return nil, fmt.Errorf("Extended search not implemented")
+	}
+	ireq := gindex.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName), UserID: c.User.ID,
+		Querry: keywords, CsrfT: c.GetCookie(setting.CSRFCookieName)}
+	data, err := json.Marshal(ireq)
+	if err != nil {
+		c.Status(http.StatusInternalServerError)
+		return nil, err
+	}
+	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 nil, err
+	}
+	data, err = ioutil.ReadAll(resp.Body)
+	if err != nil {
+		c.Status(http.StatusInternalServerError)
+		return nil, err
+	}
+	return data, nil
+}
+
+func ExploreData(c *context.Context) {
+	c.Data["Title"] = c.Tr("explore")
+	c.Data["PageIsExplore"] = true
+	c.Data["PageIsExploreData"] = true
+
+	keywords := c.Query("q")
+	data, err := Search(c, keywords)
+
+	if err != nil {
+		c.Handle(http.StatusInternalServerError, "Could nor querry", err)
+		return
+	}
+
+	res := gindex.SearchResults{}
+	err = json.Unmarshal(data, &res)
+	if err != nil {
+		c.Handle(http.StatusInternalServerError, "Could nor display result", err)
+		return
+	}
+	c.Data["Blobs"] = res.Blobs
+	c.Data["Commits"] = res.Commits
+	c.HTML(200, EXPLORE_DATA)
+
+}