owncast/main.go

110 lines
2.3 KiB
Go
Raw Normal View History

2020-05-24 02:57:49 +02:00
package main
import (
"encoding/json"
2020-05-24 02:57:49 +02:00
"net/http"
"path"
2020-06-02 01:53:31 +02:00
"strconv"
2020-05-24 02:57:49 +02:00
log "github.com/sirupsen/logrus"
)
var storage ChunkStorage
2020-06-02 01:53:31 +02:00
var configuration = getConfig()
var server *Server
2020-06-11 08:52:55 +02:00
var stats *Stats
var usingExternalStorage = false
2020-05-30 03:08:33 +02:00
func main() {
2020-06-02 01:53:31 +02:00
log.Println("Starting up. Please wait...")
2020-06-09 10:52:15 +02:00
resetDirectories(configuration)
checkConfig(configuration)
2020-06-11 08:52:55 +02:00
stats = getSavedStats()
stats.Setup()
2020-06-02 01:53:31 +02:00
if configuration.IPFS.Enabled {
storage = &IPFSStorage{}
2020-06-03 10:34:05 +02:00
usingExternalStorage = true
} else if configuration.S3.Enabled {
storage = &S3Storage{}
usingExternalStorage = true
}
2020-06-03 10:34:05 +02:00
if usingExternalStorage {
storage.Setup(configuration)
2020-06-09 10:52:15 +02:00
// hlsDirectoryPath = configuration.PrivateHLSPath
go monitorVideoContent(configuration.PrivateHLSPath, configuration, storage)
2020-06-02 01:53:31 +02:00
}
2020-06-15 08:06:43 +02:00
go startRTMPService()
2020-05-24 02:57:49 +02:00
2020-06-15 08:06:43 +02:00
startChatServer()
}
func startChatServer() {
// log.SetFlags(log.Lshortfile)
// websocket server
server = NewServer("/entry")
go server.Listen()
// static files
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2020-06-14 02:08:34 +02:00
enableCors(&w)
http.ServeFile(w, r, path.Join("webroot", r.URL.Path))
if path.Ext(r.URL.Path) == ".m3u8" {
clientID := getClientIDFromRequest(r)
stats.SetClientActive(clientID)
}
})
http.HandleFunc("/status", getStatus)
log.Printf("Starting public web server on port %d", configuration.WebServerPort)
2020-06-02 01:53:31 +02:00
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(configuration.WebServerPort), nil))
}
2020-06-14 02:08:34 +02:00
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func getStatus(w http.ResponseWriter, r *http.Request) {
2020-06-14 07:39:02 +02:00
enableCors(&w)
status := Status{
2020-06-11 08:52:55 +02:00
Online: stats.IsStreamConnected(),
ViewerCount: stats.GetViewerCount(),
OverallMaxViewerCount: stats.GetOverallMaxViewerCount(),
SessionMaxViewerCount: stats.GetSessionMaxViewerCount(),
}
json.NewEncoder(w).Encode(status)
}
func streamConnected() {
2020-06-11 08:52:55 +02:00
stats.StreamConnected()
chunkPath := configuration.PublicHLSPath
if usingExternalStorage {
chunkPath = configuration.PrivateHLSPath
}
startThumbnailGenerator(chunkPath)
}
func streamDisconnected() {
2020-06-11 08:52:55 +02:00
stats.StreamDisconnected()
if configuration.EnableOfflineImage {
showStreamOfflineState(configuration)
}
}
func viewerAdded(clientID string) {
stats.SetClientActive(clientID)
}
func viewerRemoved(clientID string) {
stats.ViewerDisconnected(clientID)
}