dogtamer/server/web.go

79 lines
1.6 KiB
Go
Raw Normal View History

2021-08-23 00:45:44 +00:00
package server
import (
"context"
"fmt"
"html/template"
"net/http"
"time"
"go.uber.org/zap"
)
type WebServer struct {
Logger *zap.SugaredLogger
ListenAddr string
2021-08-23 00:45:44 +00:00
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(),
ListenAddr: ":8077",
2021-08-23 00:45:44 +00:00
}
}
func (ws *WebServer) Serve() error {
ws.httpServer = &http.Server{
Addr: ws.ListenAddr,
2021-08-23 00:45:44 +00:00
Handler: http.HandlerFunc(ws.IndexHandler),
}
go func() {
<-ws.ctx.Done()
ws.Logger.Debugw("HTTP shutdown signal received.")
2021-08-23 00:45:44 +00:00
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = ws.httpServer.Shutdown(shutdownCtx)
}()
err := ws.httpServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
ws.Logger.Warnw("HTTP Server stopped with error.", "error", err)
return err
}
ws.Logger.Info("HTTP server stopped.")
return nil
2021-08-23 00:45:44 +00:00
}
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)
}