owncast/main.go

107 lines
2.3 KiB
Go
Raw Normal View History

2020-05-24 03:57:49 +03:00
package main
import (
"encoding/json"
2020-05-24 03:57:49 +03:00
"net/http"
"path"
2020-06-02 02:53:31 +03:00
"strconv"
2020-05-24 03:57:49 +03:00
log "github.com/sirupsen/logrus"
)
var storage ChunkStorage
2020-06-02 02:53:31 +03:00
var configuration = getConfig()
var server *Server
2020-06-11 09:52:55 +03:00
var stats *Stats
var usingExternalStorage = false
2020-05-30 04:08:33 +03:00
func main() {
2020-06-02 02:53:31 +03:00
log.Println("Starting up. Please wait...")
2020-06-09 11:52:15 +03:00
resetDirectories(configuration)
checkConfig(configuration)
2020-06-11 09:52:55 +03:00
stats = getSavedStats()
stats.Setup()
2020-06-02 02:53:31 +03:00
if configuration.IPFS.Enabled {
storage = &IPFSStorage{}
2020-06-03 11:34:05 +03:00
usingExternalStorage = true
} else if configuration.S3.Enabled {
storage = &S3Storage{}
usingExternalStorage = true
}
2020-06-03 11:34:05 +03:00
if usingExternalStorage {
storage.Setup(configuration)
2020-06-09 11:52:15 +03:00
// hlsDirectoryPath = configuration.PrivateHLSPath
go monitorVideoContent(configuration.PrivateHLSPath, configuration, storage)
2020-06-02 02:53:31 +03:00
}
2020-06-15 09:06:43 +03:00
go startRTMPService()
2020-05-24 03:57:49 +03:00
2020-06-15 09:06:43 +03: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 03:08:34 +03: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 02:53:31 +03:00
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(configuration.WebServerPort), nil))
}
2020-06-14 03:08:34 +03:00
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func getStatus(w http.ResponseWriter, r *http.Request) {
2020-06-14 08:39:02 +03:00
enableCors(&w)
status := Status{
2020-06-11 09:52:55 +03:00
Online: stats.IsStreamConnected(),
ViewerCount: stats.GetViewerCount(),
OverallMaxViewerCount: stats.GetOverallMaxViewerCount(),
SessionMaxViewerCount: stats.GetSessionMaxViewerCount(),
}
json.NewEncoder(w).Encode(status)
}
func streamConnected() {
2020-06-11 09:52:55 +03:00
stats.StreamConnected()
chunkPath := configuration.PublicHLSPath
if usingExternalStorage {
chunkPath = configuration.PrivateHLSPath
}
startThumbnailGenerator(chunkPath)
}
func streamDisconnected() {
2020-06-11 09:52:55 +03:00
stats.StreamDisconnected()
}
func viewerAdded(clientID string) {
stats.SetClientActive(clientID)
}
func viewerRemoved(clientID string) {
stats.ViewerDisconnected(clientID)
}