MQTTListener/main.go

89 lines
2.0 KiB
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"
"path/filepath"
2020-08-26 09:54:26 +02:00
"syscall"
"net/http"
2020-08-26 09:54:26 +02:00
)
// ---------------------------------------------------------------------------
// Following variables will be statically linked at the time of compiling
// GitCommit holds short commit hash of source tree
var GitCommit string
// GitBranch holds current branch name the code is built off
var GitBranch string
// GitState shows whether there are uncommitted changes
var GitState string
// GitSummary holds output of git describe --tags --dirty --always
var GitSummary string
// BuildDate holds RFC3339 formatted UTC date (build time)
var BuildDate string
// Version holds contents of ./VERSION file, if exists, or the value passed via the -version option
var Version string
func PrintVersion() {
var versionString string = GitSummary
fmt.Println("\n--------------------------------------------------------")
fmt.Println(filepath.Base(os.Args[0]), " Version Information: ")
fmt.Println("\tVersion:\t", versionString)
fmt.Println("\tBranch:\t\t", GitBranch)
fmt.Println("\tCommit-ID:\t", GitCommit)
fmt.Println("\tBuilt:\t\t", BuildDate)
fmt.Println("\n--------------------------------------------------------")
fmt.Println("")
}
// ---------------------------------------------------------------------------
2020-08-26 09:54:26 +02:00
func main() {
PrintVersion()
2020-08-26 09:54:26 +02:00
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()
}