mqtt/cmd/main.go
Кобелев Андрей Андреевич ee70ab5bbb add more
2022-08-15 23:51:55 +03:00

97 lines
2.2 KiB
Go

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", ":1880", "network address for TCP listener")
tlsTcpAddr := flag.String("tls_tcp", ":1883", "network address for TLS TCP listener")
wsAddr := flag.String("ws", ":1884", "network address for Websocket listener")
wssAddr := flag.String("wss", ":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)
cer, err := tls.LoadX509KeyPair(*certFile, *keyFile)
if err != nil {
log.Println(err)
return
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
tcp := listeners.NewTCP("tcp", *tcpAddr)
if err := server.AddListener(tcp, nil); err != nil {
log.Fatal(err)
}
tlsTcp := listeners.NewTCP("tls", *tlsTcpAddr)
if err := server.AddListener(tlsTcp, &listeners.Config{
Auth: new(auth.Allow),
TLSConfig: config,
}); err != nil {
log.Fatal(err)
}
ws := listeners.NewWebsocket("ws", *wsAddr)
if err = server.AddListener(ws, &listeners.Config{
Auth: new(auth.Allow),
}); err != nil {
log.Fatal(err)
}
wss := listeners.NewWebsocket("wss", *wssAddr)
if err = server.AddListener(wss, &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")
}