-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (85 loc) · 2.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 2024 Johan Stenstam, [email protected]
*/
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
flag "github.com/spf13/pflag"
"github.com/dnstapir/tapir"
"github.com/spf13/viper"
)
var TEMExiter = func(args ...interface{}) {
log.Printf("TEMExiter: [placeholderfunction w/o real cleanup]")
log.Printf("TEMExiter: Exit message: %s", fmt.Sprintf(args[0].(string), args[1:]...))
os.Exit(1)
}
func main() {
flag.BoolVarP(&tapir.GlobalCF.Debug, "debug", "d", false, "Debug mode")
flag.BoolVarP(&tapir.GlobalCF.Verbose, "verbose", "v", false, "Verbose mode")
flag.Parse()
var cfgFile string
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.SetConfigFile(tapir.DefaultSloggerCfgFile)
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
} else {
TEMExiter("Could not load config %s: Error: %v", tapir.DefaultSloggerCfgFile, err)
}
// Load configuration
config, err := LoadConfig(tapir.DefaultSloggerCfgFile)
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
log.Printf("Config loaded: %+v", config)
// Initialize logger
logger := NewLogger(config.LogConfig.File)
meng, err := tapir.NewMqttEngine("tapir-slogger", config.TapirConfig.MqttConfig.ClientID, tapir.TapirSub, nil, log.Default())
if err != nil {
log.Fatalf("Error initializing MQTT engine: %v", err)
}
config.MqttEngine = meng
log.Printf("MQTT Engine: Starting")
_, _, _, err = meng.StartEngine()
if err != nil {
log.Fatalf("Error starting MQTT engine: %v", err)
}
// Initialize Status Receiver
srecv, err := NewStatusReceiver(config, logger)
if err != nil {
log.Fatalf("Error initializing Status Receiver: %v", err)
}
config.StatusReceiver = srecv
// Start MQTT handler
go srecv.Start()
// Initialize PubKeyReceiver
pkeyrecv, err := NewPubKeyReceiver(config, logger)
if err != nil {
log.Fatalf("Error initializing PubKey Receiver: %v", err)
}
config.PubKeyReceiver = pkeyrecv
// Start MQTT handler
go pkeyrecv.Start()
// Initialize API handler
//apiHandler := NewAPIHandler(config, mqttHandler)
var done_ch = make(chan struct{})
// Start API handler
// go apiHandler.Start(config)
go APIhandler(config, done_ch)
// Handle termination signals
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
// Cleanup
config.MqttEngine.StopEngine()
logger.Close()
log.Println("tapir-slogger stopped")
}