general.go 939 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package search
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "github.com/G-Node/gogs/internal/conf"
  8. "github.com/G-Node/gogs/internal/context"
  9. "github.com/G-Node/libgin/libgin"
  10. )
  11. func Search(c *context.APIContext) {
  12. // TODO: API calls shouldn't use cookie (see https://github.com/G-Node/gin-dex/issues/2)
  13. if !c.IsLogged {
  14. c.Status(http.StatusUnauthorized)
  15. return
  16. }
  17. if conf.Search.SearchURL == "" {
  18. c.Status(http.StatusNotImplemented)
  19. return
  20. }
  21. ireq := libgin.SearchRequest{}
  22. data, err := json.Marshal(ireq)
  23. if err != nil {
  24. c.Status(http.StatusInternalServerError)
  25. return
  26. }
  27. req, _ := http.NewRequest("Post", conf.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. }