mqtt/cmd/main.go

97 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-08-15 23:06:20 +03:00
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() {
2022-08-15 23:51:55 +03:00
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")
2022-08-15 23:06:20 +03:00
infoAddr := flag.String("info", ":8080", "network address for web info dashboard listener")
2022-08-15 23:22:19 +03:00
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.")
2022-08-15 23:06:20 +03:00
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}}
2022-08-15 23:51:55 +03:00
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{
2022-08-15 23:06:20 +03:00
Auth: new(auth.Allow),
TLSConfig: config,
}); err != nil {
log.Fatal(err)
}
2022-08-15 23:51:55 +03:00
ws := listeners.NewWebsocket("ws", *wsAddr)
2022-08-15 23:06:20 +03:00
if err = server.AddListener(ws, &listeners.Config{
Auth: new(auth.Allow),
2022-08-15 23:51:55 +03:00
}); err != nil {
log.Fatal(err)
}
wss := listeners.NewWebsocket("wss", *wssAddr)
if err = server.AddListener(wss, &listeners.Config{
Auth: new(auth.Allow),
2022-08-15 23:06:20 +03:00
TLSConfig: config,
}); err != nil {
log.Fatal(err)
}
2022-08-15 23:51:55 +03:00
2022-08-15 23:06:20 +03:00
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")
}