Files
pmomusic/internal/upnp/server.go
Eric Coissac 2e80eb85d2 Changes to be committed:
modified:   cmd/pmomusic/main.go
	new file:   internal/netutils/ip_detect.go
	new file:   internal/netutils/list_all_ip.go
	modified:   internal/soap/soap.go
	modified:   internal/ssdp/responder.go
	modified:   internal/ssdp/ssdp.go
	modified:   internal/upnp/http.go
	new file:   internal/upnp/server.go
	new file:   internal/upnp/xml/AVTransport.xml
	new file:   internal/upnp/xml/ConnectionManager.xml
	new file:   internal/upnp/xml/RenderingControl.xml
2025-06-08 17:41:52 +02:00

46 lines
1.2 KiB
Go

package upnp
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"gargoton.petite-maison-orange.fr/eric/pmomusic/internal/soap"
)
//go:embed xml/*.xml
var embeddedXML embed.FS
// ServeStaticXML mounts handlers for SCPD XML files.
func ServeStaticXML(mux *http.ServeMux) {
subFS, err := fs.Sub(embeddedXML, "xml")
if err != nil {
panic("failed to create sub FS: " + err.Error())
}
mux.Handle("/scpd/", http.StripPrefix("/scpd/", http.FileServer(http.FS(subFS))))
}
func StartHTTPServer(usn, ip string, port uint) {
mux := http.NewServeMux()
// Device description
mux.HandleFunc("/description.xml", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/xml")
w.Write([]byte(generateDeviceDescription(usn, ip, port)))
})
// SOAP control endpoints
mux.HandleFunc("/upnp/control/AVTransport", soap.HandleSOAP)
mux.HandleFunc("/upnp/control/RenderingControl", soap.HandleSOAP)
mux.HandleFunc("/upnp/control/ConnectionManager", soap.HandleSOAP)
// Serve static SCPD XML files
ServeStaticXML(mux)
addr := fmt.Sprintf("%s:%d", ip, port)
log.Printf("Serving UPnP fake renderer at http://%s", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}