2020-05-24 03:57:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-02 10:27:54 +03:00
|
|
|
"encoding/json"
|
2020-05-24 03:57:49 +03:00
|
|
|
"net/http"
|
2020-06-02 02:53:31 +03:00
|
|
|
"strconv"
|
2020-05-24 03:57:49 +03:00
|
|
|
|
2020-06-01 22:15:07 +03:00
|
|
|
icore "github.com/ipfs/interface-go-ipfs-core"
|
2020-05-24 03:57:49 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-06-01 22:15:07 +03:00
|
|
|
var ipfs icore.CoreAPI
|
2020-06-02 02:53:31 +03:00
|
|
|
var configuration = getConfig()
|
2020-06-03 03:35:49 +03:00
|
|
|
var server *Server
|
|
|
|
|
2020-06-02 10:27:54 +03:00
|
|
|
var online = false
|
2020-05-30 04:08:33 +03:00
|
|
|
|
2020-06-01 22:15:07 +03:00
|
|
|
func main() {
|
2020-06-02 02:53:31 +03:00
|
|
|
checkConfig(configuration)
|
|
|
|
// resetDirectories()
|
2020-05-30 04:08:33 +03:00
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
var hlsDirectoryPath = configuration.PublicHLSPath
|
2020-06-01 22:15:07 +03:00
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
log.Println("Starting up. Please wait...")
|
|
|
|
|
|
|
|
if configuration.IPFS.Enabled {
|
|
|
|
hlsDirectoryPath = configuration.PrivateHLSPath
|
|
|
|
enableIPFS()
|
|
|
|
go monitorVideoContent(hlsDirectoryPath, configuration, &ipfs)
|
|
|
|
}
|
2020-06-01 22:15:07 +03:00
|
|
|
|
2020-05-24 03:57:49 +03:00
|
|
|
go startChatServer()
|
|
|
|
|
2020-06-01 22:15:07 +03:00
|
|
|
startRTMPService()
|
|
|
|
}
|
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
func enableIPFS() {
|
|
|
|
log.Println("Enabling IPFS support...")
|
|
|
|
|
|
|
|
ipfsInstance, node, _ := createIPFSInstance()
|
|
|
|
ipfs = *ipfsInstance
|
|
|
|
|
|
|
|
createIPFSDirectory(ipfsInstance, "./hls")
|
|
|
|
go startIPFSNode(ipfs, node)
|
|
|
|
}
|
|
|
|
|
2020-06-01 22:15:07 +03:00
|
|
|
func startChatServer() {
|
|
|
|
// log.SetFlags(log.Lshortfile)
|
|
|
|
|
|
|
|
// websocket server
|
2020-06-03 03:35:49 +03:00
|
|
|
server = NewServer("/entry")
|
2020-06-01 22:15:07 +03:00
|
|
|
go server.Listen()
|
|
|
|
|
|
|
|
// static files
|
|
|
|
http.Handle("/", http.FileServer(http.Dir("webroot")))
|
2020-06-02 10:27:54 +03:00
|
|
|
http.HandleFunc("/status", getStatus)
|
2020-06-01 22:15:07 +03:00
|
|
|
|
2020-06-02 03:42:36 +03:00
|
|
|
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-01 22:15:07 +03:00
|
|
|
}
|
2020-06-02 10:27:54 +03:00
|
|
|
|
|
|
|
func getStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
status := Status{
|
2020-06-03 03:35:49 +03:00
|
|
|
Online: online,
|
|
|
|
ViewerCount: server.ClientCount(),
|
2020-06-02 10:27:54 +03:00
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamConnected() {
|
|
|
|
online = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamDisconnected() {
|
|
|
|
online = false
|
|
|
|
}
|
2020-06-03 03:35:49 +03:00
|
|
|
|
|
|
|
func viewerAdded() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewerRemoved() {
|
|
|
|
}
|