1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-23 02:42:20 +00:00
ghostream/main.go

91 lines
2.5 KiB
Go
Raw Normal View History

2020-09-28 15:36:40 +00:00
//go:generate pkger
2020-09-21 15:29:50 +00:00
package main
import (
2020-09-21 17:59:41 +00:00
"log"
2020-09-22 09:42:57 +00:00
"strings"
2020-09-21 17:59:41 +00:00
2020-09-24 09:24:13 +00:00
"github.com/pion/webrtc/v3"
2020-09-22 09:42:57 +00:00
"github.com/spf13/viper"
"gitlab.crans.org/nounous/ghostream/auth"
2020-09-21 19:38:11 +00:00
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
2020-09-24 09:24:13 +00:00
"gitlab.crans.org/nounous/ghostream/stream"
2020-09-27 09:14:22 +00:00
"gitlab.crans.org/nounous/ghostream/stream/srt"
2020-09-21 15:47:31 +00:00
"gitlab.crans.org/nounous/ghostream/web"
2020-09-21 15:29:50 +00:00
)
2020-09-22 09:42:57 +00:00
func loadConfiguration() {
// Load configuration from environnement variables
// Replace "." to "_" for nested structs
// e.g. GHOSTREAM_LDAP_URI will apply to Config.LDAP.URI
viper.SetEnvPrefix("ghostream")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
// Load configuration file if exists
2020-09-27 19:46:33 +00:00
viper.SetConfigName("ghostream.yml")
2020-09-22 09:42:57 +00:00
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/.ghostream")
viper.AddConfigPath("/etc/ghostream")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found, ignore and use defaults
log.Print(err)
} else {
// Config file was found but another error was produced
log.Fatal(err)
}
} else {
// Config loaded
log.Printf("Using config file: %s", viper.ConfigFileUsed())
}
// Define configuration default values
2020-09-22 14:39:06 +00:00
viper.SetDefault("Auth.Backend", "Basic")
2020-09-22 09:42:57 +00:00
viper.SetDefault("Auth.LDAP.URI", "ldap://127.0.0.1:389")
viper.SetDefault("Auth.LDAP.UserDn", "cn=users,dc=example,dc=com")
viper.SetDefault("Monitoring.ListenAddress", ":2112")
2020-09-27 09:14:22 +00:00
viper.SetDefault("Srt.ListenAddress", ":9710")
2020-09-27 19:55:22 +00:00
viper.SetDefault("Web.ListenAddress", ":8080")
2020-09-22 09:42:57 +00:00
viper.SetDefault("Web.Name", "Ghostream")
viper.SetDefault("Web.Hostname", "localhost")
viper.SetDefault("Web.Favicon", "/favicon.ico")
}
2020-09-21 15:29:50 +00:00
func main() {
2020-09-21 17:59:41 +00:00
// Load configuration
2020-09-22 09:42:57 +00:00
loadConfiguration()
cfg := struct {
Auth auth.Options
Monitoring monitoring.Options
2020-09-27 09:14:22 +00:00
Srt srt.Options
2020-09-22 09:42:57 +00:00
Web web.Options
}{}
if err := viper.Unmarshal(&cfg); err != nil {
log.Fatalln("Failed to load settings", err)
2020-09-21 17:59:41 +00:00
}
2020-09-22 10:54:12 +00:00
// Init authentification
authBackend, err := auth.New(&cfg.Auth)
if err != nil {
log.Fatalln("Failed to load authentification backend:", err)
}
defer authBackend.Close()
2020-09-22 10:54:12 +00:00
2020-09-24 09:24:13 +00:00
// WebRTC session description channels
remoteSdpChan := make(chan webrtc.SessionDescription)
localSdpChan := make(chan webrtc.SessionDescription)
2020-09-21 19:05:45 +00:00
2020-09-24 09:24:13 +00:00
// Start stream, web and monitoring server
2020-09-27 09:14:22 +00:00
go srt.Serve(&cfg.Srt)
2020-09-24 09:24:13 +00:00
go stream.Serve(remoteSdpChan, localSdpChan)
go web.Serve(remoteSdpChan, localSdpChan, &cfg.Web)
go monitoring.Serve(&cfg.Monitoring)
2020-09-21 19:33:32 +00:00
2020-09-21 19:05:45 +00:00
// Wait for routines
select {}
2020-09-21 15:29:50 +00:00
}