status.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package handlers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "time"
  6. "github.com/owncast/owncast/core"
  7. "github.com/owncast/owncast/core/data"
  8. "github.com/owncast/owncast/utils"
  9. "github.com/owncast/owncast/webserver/router/middleware"
  10. webutils "github.com/owncast/owncast/webserver/utils"
  11. )
  12. // GetStatus gets the status of the server.
  13. func GetStatus(w http.ResponseWriter, r *http.Request) {
  14. response := getStatusResponse()
  15. w.Header().Set("Content-Type", "application/json")
  16. middleware.DisableCache(w)
  17. if err := json.NewEncoder(w).Encode(response); err != nil {
  18. webutils.InternalErrorHandler(w, err)
  19. }
  20. }
  21. func getStatusResponse() webStatusResponse {
  22. status := core.GetStatus()
  23. response := webStatusResponse{
  24. Online: status.Online,
  25. ServerTime: time.Now(),
  26. LastConnectTime: status.LastConnectTime,
  27. LastDisconnectTime: status.LastDisconnectTime,
  28. VersionNumber: status.VersionNumber,
  29. StreamTitle: status.StreamTitle,
  30. }
  31. if !data.GetHideViewerCount() {
  32. response.ViewerCount = status.ViewerCount
  33. }
  34. return response
  35. }
  36. type webStatusResponse struct {
  37. ServerTime time.Time `json:"serverTime"`
  38. LastConnectTime *utils.NullTime `json:"lastConnectTime"`
  39. LastDisconnectTime *utils.NullTime `json:"lastDisconnectTime"`
  40. VersionNumber string `json:"versionNumber"`
  41. StreamTitle string `json:"streamTitle"`
  42. ViewerCount int `json:"viewerCount,omitempty"`
  43. Online bool `json:"online"`
  44. }