From d8c43d2c56a1f1f3b29a6ca2998db43d7715a7de Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Fri, 2 Oct 2020 00:02:42 -0700 Subject: [PATCH] Add server config admin endpoint (#207) * Add support for ending the inbound stream. Closes #191 * Add a simple success response to API requests * Add server config admin endpoint --- config/config.go | 38 +++++++++++++++++------------------ config/configUtils.go | 17 +++++++++++++++- controllers/serverConfig.go | 40 +++++++++++++++++++++++++++++++++++++ router/router.go | 3 +++ 4 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 controllers/serverConfig.go diff --git a/config/config.go b/config/config.go index 46d564572..1f80003b2 100644 --- a/config/config.go +++ b/config/config.go @@ -22,7 +22,7 @@ type config struct { InstanceDetails InstanceDetails `yaml:"instanceDetails"` PrivateHLSPath string `yaml:"privateHLSPath"` PublicHLSPath string `yaml:"publicHLSPath"` - S3 s3 `yaml:"s3"` + S3 S3 `yaml:"s3"` VersionInfo string `yaml:"-"` VideoSettings videoSettings `yaml:"videoSettings"` WebServerPort int `yaml:"webServerPort"` @@ -72,35 +72,35 @@ type StreamQuality struct { // Enable passthrough to copy the video and/or audio directly from the // incoming stream and disable any transcoding. It will ignore any of // the below settings. - IsVideoPassthrough bool `yaml:"videoPassthrough"` - IsAudioPassthrough bool `yaml:"audioPassthrough"` + IsVideoPassthrough bool `yaml:"videoPassthrough" json:"videoPassthrough"` + IsAudioPassthrough bool `yaml:"audioPassthrough" json:"audioPassthrough"` - VideoBitrate int `yaml:"videoBitrate"` - AudioBitrate int `yaml:"audioBitrate"` + VideoBitrate int `yaml:"videoBitrate" json:"videoBitrate"` + AudioBitrate int `yaml:"audioBitrate" json:"audioBitrate"` // Set only one of these in order to keep your current aspect ratio. // Or set neither to not scale the video. - ScaledWidth int `yaml:"scaledWidth"` - ScaledHeight int `yaml:"scaledHeight"` + ScaledWidth int `yaml:"scaledWidth" json:"scaledWidth,omitempty"` + ScaledHeight int `yaml:"scaledHeight" json:"scaledHeight,omitempty"` - Framerate int `yaml:"framerate"` - EncoderPreset string `yaml:"encoderPreset"` + Framerate int `yaml:"framerate" json:"framerate"` + EncoderPreset string `yaml:"encoderPreset" json:"encoderPreset"` } type files struct { MaxNumberInPlaylist int `yaml:"maxNumberInPlaylist"` } -//s3 is for configuring the s3 integration -type s3 struct { - Enabled bool `yaml:"enabled"` - Endpoint string `yaml:"endpoint"` - ServingEndpoint string `yaml:"servingEndpoint"` - AccessKey string `yaml:"accessKey"` - Secret string `yaml:"secret"` - Bucket string `yaml:"bucket"` - Region string `yaml:"region"` - ACL string `yaml:"acl"` +//S3 is for configuring the S3 integration +type S3 struct { + Enabled bool `yaml:"enabled" json:"enabled"` + Endpoint string `yaml:"endpoint" json:"endpoint,omitempty"` + ServingEndpoint string `yaml:"servingEndpoint" json:"servingEndpoint,omitempty"` + AccessKey string `yaml:"accessKey" json:"accessKey,omitempty"` + Secret string `yaml:"secret" json:"secret,omitempty"` + Bucket string `yaml:"bucket" json:"bucket,omitempty"` + Region string `yaml:"region" json:"region,omitempty"` + ACL string `yaml:"acl" json:"acl,omitempty"` } func (c *config) load(filePath string) error { diff --git a/config/configUtils.go b/config/configUtils.go index f15a443a0..5b9fa60ae 100644 --- a/config/configUtils.go +++ b/config/configUtils.go @@ -1,6 +1,9 @@ package config -import "sort" +import ( + "encoding/json" + "sort" +) func findHighestQuality(qualities []StreamQuality) int { type IndexedQuality struct { @@ -32,3 +35,15 @@ func findHighestQuality(qualities []StreamQuality) int { return indexedQualities[0].index } + +// MarshalJSON is a custom JSON marshal function for video stream qualities +func (q *StreamQuality) MarshalJSON() ([]byte, error) { + type Alias StreamQuality + return json.Marshal(&struct { + Framerate int `json:"framerate"` + *Alias + }{ + Framerate: q.GetFramerate(), + Alias: (*Alias)(q), + }) +} diff --git a/controllers/serverConfig.go b/controllers/serverConfig.go new file mode 100644 index 000000000..bfc7bb36c --- /dev/null +++ b/controllers/serverConfig.go @@ -0,0 +1,40 @@ +package controllers + +import ( + "encoding/json" + "net/http" + + "github.com/gabek/owncast/config" +) + +// GetServerConfig gets the config details of the server +func GetServerConfig(w http.ResponseWriter, r *http.Request) { + response := serverConfigAdminResponse{ + InstanceDetails: config.Config.InstanceDetails, + FFmpegPath: config.Config.GetFFMpegPath(), + WebServerPort: config.Config.GetPublicWebServerPort(), + VideoSettings: videoSettings{ + VideoQualityVariants: config.Config.GetVideoStreamQualities(), + SegmentLengthSeconds: config.Config.GetVideoSegmentSecondsLength(), + NumberOfPlaylistItems: config.Config.GetMaxNumberOfReferencedSegmentsInPlaylist(), + }, + S3: config.Config.S3, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(response) +} + +type serverConfigAdminResponse struct { + InstanceDetails config.InstanceDetails `json:"instanceDetails"` + FFmpegPath string `json:"ffmpegPath"` + WebServerPort int `json:"webServerPort"` + S3 config.S3 `json:"s3"` + VideoSettings videoSettings `json:"videoSettings"` +} + +type videoSettings struct { + VideoQualityVariants []config.StreamQuality `json:"videoQualityVariants"` + SegmentLengthSeconds int `json:"segmentLengthSeconds"` + NumberOfPlaylistItems int `json:"numberOfPlaylistItems"` +} diff --git a/router/router.go b/router/router.go index 8504fba1f..f80c81599 100644 --- a/router/router.go +++ b/router/router.go @@ -57,6 +57,9 @@ func Start() error { // Change the current streaming key in memory http.HandleFunc("/api/admin/changekey", middleware.RequireAdminAuth(admin.ChangeStreamKey)) + // Server config + http.HandleFunc("/api/admin/serverconfig", middleware.RequireAdminAuth(controllers.GetServerConfig)) + port := config.Config.GetPublicWebServerPort() log.Infof("Web server running on port: %d", port)