MQTTListener/main.go

52 lines
777 B
Go

package main
import (
"mqttListener/config"
"mqttListener/env"
"mqttListener/models"
"mqttListener/mqtt"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"net/http"
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
config, err := config.NewConfig("config.toml")
if err != nil {
log.Panic(err)
}
// DEBUG Config
fmt.Println("BrokerAddress = ", config.BrokerAddress)
db, err := models.NewDB("simple.sqlite")
if err != nil {
log.Panic(err)
}
env := &env.Env{db, config}
mqtt.Setup(env)
mqtt.Connect()
go mqtt.Listen()
http.HandleFunc("/devices", env.DevicesIndex)
go http.ListenAndServe(":3000", nil)
fmt.Println("awaiting signal")
<-c // wait for SIGTERM
fmt.Println("exiting")
mqtt.Disconnect()
}