package main import ( "crypto/tls" "flag" "fmt" "log" "os" "os/signal" "syscall" mqtt "github.com/mochi-co/mqtt/server" "github.com/mochi-co/mqtt/server/listeners" "github.com/mochi-co/mqtt/server/listeners/auth" ) func main() { tcpAddr := flag.String("tcp", ":1883", "network address for TCP listener") wsAddr := flag.String("ws", ":1882", "network address for Websocket listener") infoAddr := flag.String("info", ":8080", "network address for web info dashboard listener") certFile := flag.String("cert", "cert/fullchain.pem", "path the body of a public certificate") keyFile := flag.String("key", "cert/privkey.pem", "path to body of a private key.") flag.Parse() sigs := make(chan os.Signal, 1) done := make(chan bool, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) go func() { <-sigs done <- true }() fmt.Println("Mochi MQTT Broker initializing...") fmt.Println("TCP", *tcpAddr) fmt.Println("Websocket", *wsAddr) fmt.Println("Dashboard", *infoAddr) server := mqtt.NewServer(nil) tcp := listeners.NewTCP("t1", *tcpAddr) cer, err := tls.LoadX509KeyPair(*certFile, *keyFile) if err != nil { log.Println(err) return } config := &tls.Config{Certificates: []tls.Certificate{cer}} if err := server.AddListener(tcp, &listeners.Config{ Auth: new(auth.Allow), TLSConfig: config, }); err != nil { log.Fatal(err) } ws := listeners.NewWebsocket("ws1", *wsAddr) if err = server.AddListener(ws, &listeners.Config{ Auth: new(auth.Allow), TLSConfig: config, }); err != nil { log.Fatal(err) } stats := listeners.NewHTTPStats("stats", *infoAddr) if err = server.AddListener(stats, nil); err != nil { log.Fatal(err) } go server.Serve() fmt.Println("Started!") <-done fmt.Println("Caught Signal") server.Close() fmt.Println("Finished") }