Added built-in mDNS server.

This commit is contained in:
Nathan Osman 2016-05-01 18:24:36 -07:00
parent b674a7daa2
commit 35358a6f84

57
main.go
View File

@ -1,7 +1,10 @@
package main
import (
"github.com/micro/mdns"
"flag"
"fmt"
"log"
"os"
"os/signal"
@ -10,20 +13,62 @@ import (
func main() {
var (
addr = flag.String("addr", ":8000", "`host:port` to listen on")
host = flag.String("host", "0.0.0.0", "`host` to listen on")
port = flag.Int("port", 8000, "`port` to listen on")
directory = flag.String("directory", "/var/cache/go-aptproxy", "`directory` used for storing packages")
)
flag.Parse()
s, err := NewServer(*addr, *directory)
// Create the HTTP server and initialize the cache
addr := fmt.Sprintf("%s:%d", *host, *port)
httpServer, err := NewServer(addr, *directory)
if err != nil {
log.Println(err)
os.Exit(1)
}
s.Start()
defer s.Stop()
log.Println("APT proxy started")
log.Println("Starting HTTP server...")
if err = httpServer.Start(); err != nil {
log.Println(err)
os.Exit(1)
}
defer func() {
httpServer.Stop()
log.Println("Stopping HTTP server...")
}()
// Respond to mDNS queries
h, err := os.Hostname()
if err != nil {
log.Println(err)
os.Exit(1)
}
service, err := mdns.NewMDNSService(
h,
"_apt_proxy._tcp",
"",
"",
*port,
nil,
[]string{"go-aptproxy - Smarter APT Proxy"},
)
if err != nil {
log.Println(err)
os.Exit(1)
}
log.Println("Starting mDNS server...")
mDNSServer, err := mdns.NewServer(&mdns.Config{Zone: service})
if err != nil {
log.Println(err)
os.Exit(1)
}
defer func() {
mDNSServer.Shutdown()
log.Println("Stopping mDNS server...")
}()
// Wait for a signal
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
<-c
log.Println("APT proxy shut down by signal")
log.Println("Caught SIGINT")
}