MQTTListener/main.go

52 lines
777 B
Go
Raw Normal View History

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