MQTTListener/main.go

98 lines
2.2 KiB
Go

package main
import (
"mqttListener/config"
"mqttListener/env"
"mqttListener/models"
"mqttListener/mqtt"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"net/http"
)
// ---------------------------------------------------------------------------
// 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
// PrintVersion prints version information
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("")
}
// ---------------------------------------------------------------------------
func main() {
versionFlagPtr := flag.Bool("v", false, "print version")
flag.Parse()
if *versionFlagPtr {
PrintVersion()
os.Exit(0)
}
PrintVersion()
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: db, Config: 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()
}