69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"html/template"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
|
type WebServer struct {
|
||
|
Logger *zap.SugaredLogger
|
||
|
ctx context.Context
|
||
|
rtmpServer *RTMPServer
|
||
|
httpServer *http.Server
|
||
|
}
|
||
|
|
||
|
func NewWebServer(ctx context.Context, rs *RTMPServer) *WebServer {
|
||
|
return &WebServer{
|
||
|
ctx: ctx,
|
||
|
rtmpServer: rs,
|
||
|
Logger: zap.NewNop().Sugar(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (ws *WebServer) Serve() error {
|
||
|
ws.httpServer = &http.Server{
|
||
|
Addr: ":8077",
|
||
|
Handler: http.HandlerFunc(ws.IndexHandler),
|
||
|
}
|
||
|
|
||
|
go func() {
|
||
|
<-ws.ctx.Done()
|
||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
|
defer cancel()
|
||
|
_ = ws.httpServer.Shutdown(shutdownCtx)
|
||
|
}()
|
||
|
|
||
|
return ws.httpServer.ListenAndServe()
|
||
|
}
|
||
|
|
||
|
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
data := struct {
|
||
|
Streams []struct {
|
||
|
Name string
|
||
|
URL template.URL
|
||
|
}
|
||
|
}{}
|
||
|
|
||
|
for _, s := range ws.rtmpServer.streams {
|
||
|
stream := struct {
|
||
|
Name string
|
||
|
URL template.URL
|
||
|
}{
|
||
|
Name: s.Name,
|
||
|
URL: template.URL(fmt.Sprintf("rtmp://localhost:5566/view/%s", s.Name)),
|
||
|
}
|
||
|
data.Streams = append(data.Streams, stream)
|
||
|
}
|
||
|
|
||
|
tmpl := template.Must(template.ParseFiles("server/templates/index.html"))
|
||
|
|
||
|
w.Header().Add("Content-Type", "text/html")
|
||
|
|
||
|
tmpl.Execute(w, data)
|
||
|
}
|