go-aptproxy/main.go

75 lines
1.4 KiB
Go
Raw Normal View History

package main
import (
2016-05-02 03:24:36 +02:00
"github.com/micro/mdns"
2016-04-27 00:15:31 +02:00
"flag"
2016-05-02 03:24:36 +02:00
"fmt"
2016-04-27 00:15:31 +02:00
"log"
"os"
"os/signal"
"syscall"
)
func main() {
2016-04-27 00:15:31 +02:00
var (
2016-05-02 03:24:36 +02:00
host = flag.String("host", "0.0.0.0", "`host` to listen on")
port = flag.Int("port", 8000, "`port` to listen on")
2016-05-02 01:07:02 +02:00
directory = flag.String("directory", "/var/cache/go-aptproxy", "`directory` used for storing packages")
2016-04-27 00:15:31 +02:00
)
flag.Parse()
2016-05-02 03:24:36 +02:00
// 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)
}
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 {
2016-04-27 00:15:31 +02:00
log.Println(err)
os.Exit(1)
}
2016-05-02 03:24:36 +02:00
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
2016-05-02 03:24:36 +02:00
log.Println("Caught SIGINT")
}