suggest.go 874 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 Suggest(c *context.APIContext) {
  12. // TODO: API calls shouldn't use cookie (see https://github.com/G-Node/gin-dex/issues/2)
  13. if conf.Search.SearchURL == "" {
  14. c.Status(http.StatusNotImplemented)
  15. return
  16. }
  17. ireq := libgin.SearchRequest{}
  18. data, err := json.Marshal(ireq)
  19. if err != nil {
  20. c.Status(http.StatusInternalServerError)
  21. return
  22. }
  23. req, _ := http.NewRequest("Post", conf.Search.SearchURL, bytes.NewReader(data))
  24. cl := http.Client{}
  25. resp, err := cl.Do(req)
  26. if err != nil {
  27. c.Status(http.StatusInternalServerError)
  28. return
  29. }
  30. data, err = ioutil.ReadAll(resp.Body)
  31. if err != nil {
  32. c.Status(http.StatusInternalServerError)
  33. return
  34. }
  35. c.Write(data)
  36. }