arcast/pkg/server/mdns.go

45 lines
904 B
Go

package server
import (
"context"
"forge.cadoles.com/arcad/arcast/pkg/network"
"github.com/grandcat/zeroconf"
"github.com/pkg/errors"
"github.com/wlynxg/anet"
"gitlab.com/wpetit/goweb/logger"
)
const (
MDNSService = "_arcast._http._tcp"
MDNSDomain = "local."
)
func (s *Server) startMDNServer(ctx context.Context) error {
logger.Debug(ctx, "starting mdns server")
ifaces, err := anet.Interfaces()
if err != nil {
return errors.WithStack(err)
}
ips, err := network.GetLANIPv4Addrs()
if err != nil {
return errors.WithStack(err)
}
logger.Debug(ctx, "advertising ips", logger.F("ips", ips))
server, err := zeroconf.RegisterProxy(s.instanceID, MDNSService, MDNSDomain, s.port, s.instanceID, ips, []string{}, ifaces)
if err != nil {
return errors.WithStack(err)
}
go func() {
<-ctx.Done()
logger.Debug(ctx, "closing mdns server")
server.Shutdown()
}()
return nil
}