general.go 982 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package search
  2. import (
  3. "github.com/G-Node/gogs/pkg/context"
  4. "github.com/G-Node/gin-dex/gindex"
  5. "net/http"
  6. "github.com/G-Node/gogs/pkg/setting"
  7. "encoding/json"
  8. "bytes"
  9. "io/ioutil"
  10. )
  11. func Search(c *context.APIContext) {
  12. if ! c.IsLogged {
  13. c.Status(http.StatusUnauthorized)
  14. return
  15. }
  16. if !setting.Search.Do {
  17. c.Status(http.StatusNotImplemented)
  18. return
  19. }
  20. ireq := gindex.SearchRequest{Token: c.GetCookie(setting.SessionConfig.CookieName), UserID: c.User.ID,
  21. Querry: c.Params("querry"), CsrfT: c.GetCookie(setting.CSRFCookieName)}
  22. data, err := json.Marshal(ireq)
  23. if err != nil {
  24. c.Status(http.StatusInternalServerError)
  25. return
  26. }
  27. req, _ := http.NewRequest("Post", setting.Search.SearchUrl, bytes.NewReader(data))
  28. cl := http.Client{}
  29. resp, err := cl.Do(req)
  30. if err != nil {
  31. c.Status(http.StatusInternalServerError)
  32. return
  33. }
  34. data, err = ioutil.ReadAll(resp.Body)
  35. if err != nil {
  36. c.Status(http.StatusInternalServerError)
  37. return
  38. }
  39. c.Write(data)
  40. }