status.go 1.4 KB

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