owncast/main.go

122 lines
2.7 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"
)
// Build-time injected values
var GitCommit string = "unknown"
var BuildVersion string = "0.0.0"
var BuildType string = "localdev"
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() {
// logrus.SetReportCaller(true)
log.StandardLogger().Printf("Owncast v%s/%s (%s)", BuildVersion, BuildType, GitCommit)
2020-06-09 11:52:15 +03:00
checkConfig(configuration)
resetDirectories(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
go monitorVideoContent(configuration.PrivateHLSPath, configuration, storage)
2020-06-02 02:53:31 +03:00
}
createInitialOfflineState()
2020-06-15 09:06:43 +03:00
go startRTMPService()
2020-05-24 03:57:49 +03:00
2020-06-18 09:01:31 +03:00
startWebServer()
}
2020-06-18 09:01:31 +03:00
func startWebServer() {
// 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)
disableCache(&w)
}
})
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 disableCache(w *http.ResponseWriter) {
(*w).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
(*w).Header().Set("Expires", "0")
}
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()
if configuration.EnableOfflineImage {
showStreamOfflineState(configuration)
}
}
func viewerAdded(clientID string) {
stats.SetClientActive(clientID)
}
func viewerRemoved(clientID string) {
stats.ViewerDisconnected(clientID)
}