search.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package routes
  2. import (
  3. "github.com/G-Node/gogs/pkg/context"
  4. "net/http"
  5. "github.com/G-Node/gin-dex/gindex"
  6. "encoding/json"
  7. "bytes"
  8. "io/ioutil"
  9. "github.com/G-Node/gogs/pkg/setting"
  10. "fmt"
  11. "strconv"
  12. "github.com/Sirupsen/logrus"
  13. )
  14. const (
  15. EXPLORE_DATA = "explore/data"
  16. EXPLORE_COMMITS = "explore/commits"
  17. )
  18. func Search(c *context.Context, keywords string, sType int64) ([]byte, error) {
  19. if !setting.Search.Do {
  20. c.Status(http.StatusNotImplemented)
  21. return nil, fmt.Errorf("Extended search not implemented")
  22. }
  23. ireq := gindex.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName),
  24. Querry: keywords, CsrfT: c.GetCookie(setting.CSRFCookieName), SType: sType, UserID: -10}
  25. if c.IsLogged {
  26. ireq.UserID = c.User.ID
  27. }
  28. data, err := json.Marshal(ireq)
  29. if err != nil {
  30. c.Status(http.StatusInternalServerError)
  31. return nil, err
  32. }
  33. req, _ := http.NewRequest("Post", setting.Search.SearchUrl, bytes.NewReader(data))
  34. cl := http.Client{}
  35. resp, err := cl.Do(req)
  36. if err != nil {
  37. c.Status(http.StatusInternalServerError)
  38. return nil, err
  39. }
  40. data, err = ioutil.ReadAll(resp.Body)
  41. if err != nil {
  42. c.Status(http.StatusInternalServerError)
  43. return nil, err
  44. }
  45. return data, nil
  46. }
  47. func ExploreData(c *context.Context) {
  48. c.Data["Title"] = c.Tr("explore")
  49. c.Data["PageIsExplore"] = true
  50. c.Data["PageIsExploreData"] = true
  51. keywords := c.Query("q")
  52. c.Data["Keywords"] = keywords
  53. sType, err := strconv.ParseInt(c.Query("stype"), 10, 0)
  54. if err != nil {
  55. logrus.Errorf("Serach type not understood:%+v", err)
  56. sType = 0
  57. }
  58. data, err := Search(c, keywords, sType)
  59. if err != nil {
  60. c.Handle(http.StatusInternalServerError, "Could not querry", err)
  61. return
  62. }
  63. res := gindex.SearchResults{}
  64. err = json.Unmarshal(data, &res)
  65. if err != nil {
  66. c.Handle(http.StatusInternalServerError, "Could not display result", err)
  67. return
  68. }
  69. c.Data["Blobs"] = res.Blobs
  70. c.Data["opsel"] = sType
  71. c.HTML(200, EXPLORE_DATA)
  72. }
  73. func ExploreCommits(c *context.Context) {
  74. c.Data["Title"] = c.Tr("explore")
  75. c.Data["PageIsExplore"] = true
  76. c.Data["PageIsExploreCommits"] = true
  77. keywords := c.Query("q")
  78. sType, err := strconv.ParseInt(c.Query("stype"), 10, 0)
  79. if err != nil {
  80. logrus.Errorf("Serach type not understood:%+v", err)
  81. sType = 0
  82. }
  83. data, err := Search(c, keywords, sType)
  84. if err != nil {
  85. c.Handle(http.StatusInternalServerError, "Could nor querry", err)
  86. return
  87. }
  88. res := gindex.SearchResults{}
  89. err = json.Unmarshal(data, &res)
  90. if err != nil {
  91. c.Handle(http.StatusInternalServerError, "Could not display result", err)
  92. return
  93. }
  94. c.Data["Commits"] = res.Commits
  95. c.HTML(200, EXPLORE_COMMITS)
  96. }