This commit is contained in:
Your New SJW Waifu 2021-08-21 13:20:08 -05:00
parent d82d8f4b39
commit 2bb00f2d0e
1 changed files with 33 additions and 54 deletions

87
main.go
View File

@ -1,13 +1,11 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"time"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"github.com/nareix/joy4/format" "github.com/nareix/joy4/format"
@ -22,12 +20,11 @@ var (
sKey string sKey string
stats = newStreamStats() stats = newStreamStats()
sAdminPass string sAdminPass string
confFile string
) )
func setupSettings() error { func setupSettings() error {
var err error var err error
settings, err = LoadSettings(confFile) settings, err = LoadSettings("settings.json")
if err != nil { if err != nil {
return fmt.Errorf("Unable to load settings: %s", err) return fmt.Errorf("Unable to load settings: %s", err)
} }
@ -56,7 +53,6 @@ func main() {
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream") flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
flag.StringVar(&sAdminPass, "a", "", "Set admin password. Overrides configuration in settings.json. This will not write the password to settings.json.") flag.StringVar(&sAdminPass, "a", "", "Set admin password. Overrides configuration in settings.json. This will not write the password to settings.json.")
flag.BoolVar(&pullEmotes, "e", false, "Pull emotes") flag.BoolVar(&pullEmotes, "e", false, "Pull emotes")
flag.StringVar(&confFile, "f", "./settings.json", "URI of the conf file")
flag.Parse() flag.Parse()
format.RegisterAll() format.RegisterAll()
@ -113,64 +109,47 @@ func main() {
common.LogInfoln("RoomAccess: ", settings.RoomAccess) common.LogInfoln("RoomAccess: ", settings.RoomAccess)
common.LogInfoln("RoomAccessPin: ", settings.RoomAccessPin) common.LogInfoln("RoomAccessPin: ", settings.RoomAccessPin)
rtmpServer := &rtmp.Server{ go startServer()
go startRmtpServer()
<-exit
}
func startRmtpServer() {
server := &rtmp.Server{
HandlePlay: handlePlay, HandlePlay: handlePlay,
HandlePublish: handlePublish, HandlePublish: handlePublish,
Addr: rtmpAddr, Addr: rtmpAddr,
} }
err := server.ListenAndServe()
router := http.NewServeMux() if err != nil {
// If the server cannot start, don't pretend we can continue.
router.HandleFunc("/ws", wsHandler) // Chat websocket panic("Error trying to start rtmp server: " + err.Error())
router.HandleFunc("/static/js/", wsStaticFiles)
router.HandleFunc("/static/css/", wsStaticFiles)
router.HandleFunc("/static/img/", wsImages)
router.HandleFunc("/static/main.wasm", wsWasmFile)
router.HandleFunc("/emotes/", wsEmotes)
router.HandleFunc("/favicon.ico", wsStaticFiles)
router.HandleFunc("/chat", handleIndexTemplate)
router.HandleFunc("/video", handleIndexTemplate)
router.HandleFunc("/help", handleHelpTemplate)
router.HandleFunc("/emotes", handleEmoteTemplate)
router.HandleFunc("/live", handleLive)
router.HandleFunc("/", handleDefault)
httpServer := &http.Server{
Addr: addr,
Handler: router,
} }
}
// RTMP Server func startServer() {
go func() { // Chat websocket
err := rtmpServer.ListenAndServe() http.HandleFunc("/ws", wsHandler)
if err != nil { http.HandleFunc("/static/js/", wsStaticFiles)
// If the server cannot start, don't pretend we can continue. http.HandleFunc("/static/css/", wsStaticFiles)
panic("Error trying to start rtmp server: " + err.Error()) http.HandleFunc("/static/img/", wsImages)
} http.HandleFunc("/static/main.wasm", wsWasmFile)
}() http.HandleFunc("/emotes/", wsEmotes)
http.HandleFunc("/favicon.ico", wsStaticFiles)
http.HandleFunc("/chat", handleIndexTemplate)
http.HandleFunc("/video", handleIndexTemplate)
http.HandleFunc("/help", handleHelpTemplate)
http.HandleFunc("/pin", handlePin)
http.HandleFunc("/emotes", handleEmoteTemplate)
// HTTP Server http.HandleFunc("/", handleDefault)
go func() {
err := httpServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
// If the server cannot start, don't pretend we can continue.
panic("Error trying to start chat/http server: " + err.Error())
}
}()
<-exit err := http.ListenAndServe(addr, nil)
if err != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // If the server cannot start, don't pretend we can continue.
defer cancel() panic("Error trying to start chat/http server: " + err.Error())
if err := httpServer.Shutdown(ctx); err != nil && err != http.ErrServerClosed {
panic("Gracefull HTTP server shutdown failed: " + err.Error())
} }
// I don't think the RTMP server can be shutdown cleanly. Apparently the author
// of joy4 want's everyone to use joy5, but that one doesn't seem to allow clean
// shutdowns either? Idk, the documentation on joy4 and joy5 are non-existent.
} }
func handleInterrupt(exit chan bool) { func handleInterrupt(exit chan bool) {