Add minimal web interface
This commit is contained in:
8
server/templates/index.html
Normal file
8
server/templates/index.html
Normal file
@@ -0,0 +1,8 @@
|
||||
<DOCTYPE html>
|
||||
<html>
|
||||
<ol>
|
||||
{{ range .Streams }}
|
||||
<li>{{ .URL }}</li>
|
||||
{{ end }}
|
||||
</ol>
|
||||
</html>
|
68
server/web.go
Normal file
68
server/web.go
Normal file
@@ -0,0 +1,68 @@
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user