Add stream key and listen address to settings.json

This resolves #14.  If the stream key or address are passed on the
command line, those values take precedence over whatever is in the
settings file.
This commit is contained in:
Zorchenhimer 2019-03-11 23:05:01 -04:00
parent c0b6b3598c
commit 2879ab2c95
5 changed files with 39 additions and 11 deletions

3
.gitignore vendored
View File

@ -20,5 +20,8 @@ MovieNight
# Twitch channel info
static/subscriber.json
# This file now holds the stream key. Don't include it.
settings.json
# vscode
.vscode

26
main.go
View File

@ -66,21 +66,24 @@ func main() {
if len(urlParts) > 2 {
fmt.Println("Extra garbage after stream key")
conn.Close()
return
}
if len(urlParts) != 2 {
fmt.Println("Missing stream key")
conn.Close()
return
}
if urlParts[1] != *sKey {
streamKey := settings.StreamKey
if sKey != nil && len(*sKey) != 0 {
streamKey = *sKey
}
if urlParts[1] != streamKey {
fmt.Println("Due to key not match, denied stream")
conn.Close()
return //If key not match, deny stream
}
streamPath := urlParts[0]
ch := channels[streamPath]
if ch == nil {
@ -94,7 +97,6 @@ func main() {
l.Unlock()
if ch == nil {
fmt.Println("Unable to start stream, channel is nil.")
conn.Close()
return
}
@ -147,13 +149,23 @@ func main() {
}
})
go http.ListenAndServe(*addr, nil)
fmt.Println("Listen and serve ", *addr)
address := settings.ListenAddress
if addr != nil && len(*addr) != 0 {
address = *addr
}
// Load emotes before starting server.
if err := chat.Init(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Stream key: ", streamKey)
fmt.Println("Admin password: ", settings.AdminPassword)
go http.ListenAndServe(address, nil)
fmt.Println("Listen and serve ", *addr)
server.ListenAndServe()
// ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie

View File

@ -17,6 +17,8 @@ type Settings struct {
filename string
AdminPassword string
Bans []BanInfo
StreamKey string
ListenAddress string
}
type BanInfo struct {
@ -31,6 +33,14 @@ func init() {
if err != nil {
panic("Unable to load settings: " + err.Error())
}
if len(settings.StreamKey) == 0 {
panic("Missing stream key is settings.json")
}
// Save admin password to file
if err = settings.Save(); err != nil {
panic("Unable to save settings: " + err.Error())
}
}
func LoadSettings(filename string) (*Settings, error) {
@ -63,7 +73,7 @@ func generateAdminPass(seed int64) string {
}
func (s *Settings) Save() error {
marshaled, err := json.Marshal(s)
marshaled, err := json.MarshalIndent(s, "", "\t")
if err != nil {
return fmt.Errorf("Error marshaling: %s", err)
}

View File

@ -1,3 +0,0 @@
{
"Bans": []
}

6
settings_example.json Normal file
View File

@ -0,0 +1,6 @@
{
"AdminPassword": "",
"Bans": [],
"StreamKey": "ALongStreamKey",
"ListenAddress": ":8089"
}