chore(go): move stream keys to use generated type. For #3778

This commit is contained in:
Gabe Kangas
2025-01-18 16:38:59 -08:00
parent b3947ef7ea
commit e78d62ce63
8 changed files with 29 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/owncast/owncast/models" "github.com/owncast/owncast/models"
"github.com/owncast/owncast/webserver/handlers/generated"
) )
// Defaults will hold default configuration values. // Defaults will hold default configuration values.
@@ -25,7 +26,7 @@ type Defaults struct {
WebServerIP string WebServerIP string
Name string Name string
AdminPassword string AdminPassword string
StreamKeys []models.StreamKey StreamKeys []generated.StreamKey
StreamVariants []models.StreamOutputVariant StreamVariants []models.StreamOutputVariant
@@ -43,14 +44,16 @@ type Defaults struct {
// GetDefaults will return default configuration values. // GetDefaults will return default configuration values.
func GetDefaults() Defaults { func GetDefaults() Defaults {
defaultStreamKey := "abc123"
defaultStreamKeyComment := "Default stream key"
return Defaults{ return Defaults{
Name: "New Owncast Server", Name: "New Owncast Server",
Summary: "This is a new live video streaming server powered by Owncast.", Summary: "This is a new live video streaming server powered by Owncast.",
ServerWelcomeMessage: "", ServerWelcomeMessage: "",
Logo: "logo.svg", Logo: "logo.svg",
AdminPassword: "abc123", AdminPassword: "abc123",
StreamKeys: []models.StreamKey{ StreamKeys: []generated.StreamKey{
{Key: "abc123", Comment: "Default stream key"}, {Key: &defaultStreamKey, Comment: &defaultStreamKeyComment},
}, },
Tags: []string{ Tags: []string{
"owncast", "owncast",

View File

@@ -14,6 +14,7 @@ import (
"github.com/owncast/owncast/config" "github.com/owncast/owncast/config"
"github.com/owncast/owncast/models" "github.com/owncast/owncast/models"
"github.com/owncast/owncast/persistence/configrepository" "github.com/owncast/owncast/persistence/configrepository"
"github.com/owncast/owncast/webserver/handlers/generated"
) )
var _hasInboundRTMPConnection = false var _hasInboundRTMPConnection = false
@@ -87,11 +88,11 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) {
// If a stream key override was specified then use that instead. // If a stream key override was specified then use that instead.
if config.TemporaryStreamKey != "" { if config.TemporaryStreamKey != "" {
validStreamingKeys = []models.StreamKey{{Key: config.TemporaryStreamKey}} validStreamingKeys = []generated.StreamKey{{Key: &config.TemporaryStreamKey}}
} }
for _, key := range validStreamingKeys { for _, key := range validStreamingKeys {
if secretMatch(key.Key, c.URL.Path) { if key.Key != nil && secretMatch(*key.Key, c.URL.Path) {
accessGranted = true accessGranted = true
break break
} }

View File

@@ -1,7 +0,0 @@
package models
// StreamKey represents a single stream key.
type StreamKey struct {
Key string `json:"key"`
Comment string `json:"comment"`
}

View File

@@ -4,7 +4,7 @@ import (
"strings" "strings"
"github.com/owncast/owncast/core/data" "github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/models" "github.com/owncast/owncast/webserver/handlers/generated"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -63,8 +63,9 @@ func migrateToDatastoreValues2(datastore *data.Datastore, configRepository Confi
oldAdminPassword, _ := datastore.GetString("stream_key") oldAdminPassword, _ := datastore.GetString("stream_key")
// Avoids double hashing the password // Avoids double hashing the password
_ = datastore.SetString("admin_password_key", oldAdminPassword) _ = datastore.SetString("admin_password_key", oldAdminPassword)
_ = configRepository.SetStreamKeys([]models.StreamKey{ comment := "Default stream key"
{Key: oldAdminPassword, Comment: "Default stream key"}, _ = configRepository.SetStreamKeys([]generated.StreamKey{
{Key: &oldAdminPassword, Comment: &comment},
}) })
} }

View File

@@ -5,6 +5,7 @@ import (
"github.com/owncast/owncast/models" "github.com/owncast/owncast/models"
"github.com/owncast/owncast/utils" "github.com/owncast/owncast/utils"
"github.com/owncast/owncast/webserver/handlers/generated"
) )
type ConfigRepository interface { type ConfigRepository interface {
@@ -114,8 +115,8 @@ type ConfigRepository interface {
SetCustomOfflineMessage(message string) error SetCustomOfflineMessage(message string) error
SetCustomColorVariableValues(variables map[string]string) error SetCustomColorVariableValues(variables map[string]string) error
GetCustomColorVariableValues() map[string]string GetCustomColorVariableValues() map[string]string
GetStreamKeys() []models.StreamKey GetStreamKeys() []generated.StreamKey
SetStreamKeys(actions []models.StreamKey) error SetStreamKeys(actions []generated.StreamKey) error
SetDisableSearchIndexing(disableSearchIndexing bool) error SetDisableSearchIndexing(disableSearchIndexing bool) error
GetDisableSearchIndexing() bool GetDisableSearchIndexing() bool
GetVideoServingEndpoint() string GetVideoServingEndpoint() string

View File

@@ -12,6 +12,7 @@ import (
"github.com/owncast/owncast/models" "github.com/owncast/owncast/models"
"github.com/owncast/owncast/static" "github.com/owncast/owncast/static"
"github.com/owncast/owncast/utils" "github.com/owncast/owncast/utils"
"github.com/owncast/owncast/webserver/handlers/generated"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -957,22 +958,22 @@ func (r *SqlConfigRepository) GetCustomColorVariableValues() map[string]string {
} }
// GetStreamKeys will return valid stream keys. // GetStreamKeys will return valid stream keys.
func (r *SqlConfigRepository) GetStreamKeys() []models.StreamKey { func (r *SqlConfigRepository) GetStreamKeys() []generated.StreamKey {
configEntry, err := r.datastore.Get(streamKeysKey) configEntry, err := r.datastore.Get(streamKeysKey)
if err != nil { if err != nil {
return []models.StreamKey{} return []generated.StreamKey{}
} }
var streamKeys []models.StreamKey var streamKeys []generated.StreamKey
if err := configEntry.GetObject(&streamKeys); err != nil { if err := configEntry.GetObject(&streamKeys); err != nil {
return []models.StreamKey{} return []generated.StreamKey{}
} }
return streamKeys return streamKeys
} }
// SetStreamKeys will set valid stream keys. // SetStreamKeys will set valid stream keys.
func (r *SqlConfigRepository) SetStreamKeys(actions []models.StreamKey) error { func (r *SqlConfigRepository) SetStreamKeys(actions []generated.StreamKey) error {
configEntry := models.ConfigEntry{Key: streamKeysKey, Value: actions} configEntry := models.ConfigEntry{Key: streamKeysKey, Value: actions}
return r.datastore.Save(configEntry) return r.datastore.Save(configEntry)
} }

View File

@@ -916,31 +916,27 @@ func SetStreamKeys(w http.ResponseWriter, r *http.Request) {
return return
} }
type streamKeysRequest struct {
Value []models.StreamKey `json:"value"`
}
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
var streamKeys streamKeysRequest var streamKeys generated.SetStreamKeysJSONRequestBody
if err := decoder.Decode(&streamKeys); err != nil { if err := decoder.Decode(&streamKeys); err != nil {
webutils.WriteSimpleResponse(w, false, "unable to update stream keys with provided values") webutils.WriteSimpleResponse(w, false, "unable to update stream keys with provided values")
return return
} }
if len(streamKeys.Value) == 0 { if streamKeys.Value == nil || len(*streamKeys.Value) == 0 {
webutils.WriteSimpleResponse(w, false, "must provide at least one valid stream key") webutils.WriteSimpleResponse(w, false, "must provide at least one valid stream key")
return return
} }
for _, streamKey := range streamKeys.Value { for _, streamKey := range *streamKeys.Value {
if streamKey.Key == "" { if *streamKey.Key == "" {
webutils.WriteSimpleResponse(w, false, "stream key cannot be empty") webutils.WriteSimpleResponse(w, false, "stream key cannot be empty")
return return
} }
} }
configRepository := configrepository.Get() configRepository := configrepository.Get()
if err := configRepository.SetStreamKeys(streamKeys.Value); err != nil { if err := configRepository.SetStreamKeys(*streamKeys.Value); err != nil {
webutils.WriteSimpleResponse(w, false, err.Error()) webutils.WriteSimpleResponse(w, false, err.Error())
return return
} }

View File

@@ -9,6 +9,7 @@ import (
"github.com/owncast/owncast/models" "github.com/owncast/owncast/models"
"github.com/owncast/owncast/persistence/configrepository" "github.com/owncast/owncast/persistence/configrepository"
"github.com/owncast/owncast/utils" "github.com/owncast/owncast/utils"
"github.com/owncast/owncast/webserver/handlers/generated"
"github.com/owncast/owncast/webserver/router/middleware" "github.com/owncast/owncast/webserver/router/middleware"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -118,7 +119,7 @@ type serverConfigAdminResponse struct {
ExternalActions []models.ExternalAction `json:"externalActions"` ExternalActions []models.ExternalAction `json:"externalActions"`
ForbiddenUsernames []string `json:"forbiddenUsernames"` ForbiddenUsernames []string `json:"forbiddenUsernames"`
SuggestedUsernames []string `json:"suggestedUsernames"` SuggestedUsernames []string `json:"suggestedUsernames"`
StreamKeys []models.StreamKey `json:"streamKeys"` StreamKeys []generated.StreamKey `json:"streamKeys"`
VideoSettings videoSettings `json:"videoSettings"` VideoSettings videoSettings `json:"videoSettings"`
RTMPServerPort int `json:"rtmpServerPort"` RTMPServerPort int `json:"rtmpServerPort"`
WebServerPort int `json:"webServerPort"` WebServerPort int `json:"webServerPort"`