api.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package yp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/owncast/owncast/core/data"
  6. "github.com/owncast/owncast/models"
  7. "github.com/owncast/owncast/utils"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. type ypDetailsResponse struct {
  11. LastConnectTime *utils.NullTime `json:"lastConnectTime"`
  12. Name string `json:"name"`
  13. Description string `json:"description"`
  14. StreamTitle string `json:"streamTitle,omitempty"`
  15. Logo string `json:"logo"`
  16. Tags []string `json:"tags"`
  17. Social []models.SocialHandle `json:"social"`
  18. ViewerCount int `json:"viewerCount"`
  19. OverallMaxViewerCount int `json:"overallMaxViewerCount"`
  20. SessionMaxViewerCount int `json:"sessionMaxViewerCount"`
  21. NSFW bool `json:"nsfw"`
  22. Online bool `json:"online"`
  23. }
  24. // GetYPResponse gets the status of the server for YP purposes.
  25. func GetYPResponse(w http.ResponseWriter, r *http.Request) {
  26. if !data.GetDirectoryEnabled() {
  27. w.WriteHeader(http.StatusNotFound)
  28. return
  29. }
  30. status := getStatus()
  31. streamTitle := data.GetStreamTitle()
  32. response := ypDetailsResponse{
  33. Name: data.GetServerName(),
  34. Description: data.GetServerSummary(),
  35. StreamTitle: streamTitle,
  36. Logo: "/logo",
  37. NSFW: data.GetNSFW(),
  38. Tags: data.GetServerMetadataTags(),
  39. Online: status.Online,
  40. ViewerCount: status.ViewerCount,
  41. OverallMaxViewerCount: status.OverallMaxViewerCount,
  42. SessionMaxViewerCount: status.SessionMaxViewerCount,
  43. LastConnectTime: status.LastConnectTime,
  44. Social: data.GetSocialHandles(),
  45. }
  46. w.Header().Set("Content-Type", "application/json")
  47. err := json.NewEncoder(w).Encode(response)
  48. if err != nil {
  49. log.Errorln(err)
  50. }
  51. }