main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package preview
  2. import (
  3. "embed"
  4. "fmt"
  5. "mime"
  6. "net/http"
  7. "path/filepath"
  8. "strings"
  9. . "m7s.live/engine/v4"
  10. "m7s.live/engine/v4/config"
  11. )
  12. //go:embed ui
  13. var f embed.FS
  14. type PreviewConfig struct {
  15. }
  16. func (p *PreviewConfig) OnEvent(event any) {
  17. }
  18. var _ = InstallPlugin(&PreviewConfig{})
  19. func (p *PreviewConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  20. if r.URL.Path == "/" {
  21. s := "<h1><h1><h2>Live Streams 引擎中正在发布的流</h2>"
  22. Streams.Range(func(streamPath string, stream *Stream) {
  23. s += fmt.Sprintf("<a href='%s'>%s</a> [ %s ]<br>", streamPath, streamPath, stream.GetType())
  24. })
  25. s += "<h2>pull stream on subscribe 订阅时才会触发拉流的流</h2>"
  26. for name, p := range Plugins {
  27. if pullcfg, ok := p.Config.(config.PullConfig); ok {
  28. pullconf := pullcfg.GetPullConfig()
  29. pullconf.PullOnSubLocker.RLock()
  30. if pullconf.PullOnSub != nil {
  31. s += fmt.Sprintf("<h3>%s</h3>", name)
  32. for streamPath, url := range pullconf.PullOnSub {
  33. s += fmt.Sprintf("<a href='%s'>%s</a> <-- %s<br>", streamPath, streamPath, url)
  34. }
  35. }
  36. pullconf.PullOnSubLocker.RUnlock()
  37. }
  38. }
  39. w.Write([]byte(s))
  40. return
  41. }
  42. ss := strings.Split(r.URL.Path, "/")
  43. if b, err := f.ReadFile("ui/" + ss[len(ss)-1]); err == nil {
  44. w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(ss[len(ss)-1])))
  45. w.Write(b)
  46. } else {
  47. //w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
  48. //w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
  49. b, err = f.ReadFile("ui/demo.html")
  50. w.Write(b)
  51. }
  52. }