Poo recorder almost ported to go, needs to have perodically backup
This commit is contained in:
@@ -13,14 +13,17 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
pooRecorder "github.com/t-liu93/home-automation-backend/components"
|
||||
"github.com/t-liu93/home-automation-backend/components/homeassistant"
|
||||
"github.com/t-liu93/home-automation-backend/components/pooRecorder"
|
||||
"github.com/t-liu93/home-automation-backend/util/notion"
|
||||
)
|
||||
|
||||
var port string
|
||||
var scheduler gocron.Scheduler
|
||||
|
||||
// serveCmd represents the serve command
|
||||
var serveCmd = &cobra.Command{
|
||||
@@ -41,11 +44,12 @@ func initUtil() {
|
||||
|
||||
func initComponent() {
|
||||
// init pooRecorder
|
||||
pooRecorder.Init()
|
||||
pooRecorder.Init(&scheduler)
|
||||
}
|
||||
|
||||
func serve(cmd *cobra.Command, args []string) {
|
||||
slog.Info("Starting server...")
|
||||
slog.Info("Starting server..")
|
||||
|
||||
viper.SetConfigName("config") // name of config file (without extension)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath("$HOME/.config/home-automation")
|
||||
@@ -56,23 +60,46 @@ func serve(cmd *cobra.Command, args []string) {
|
||||
os.Exit(1)
|
||||
}
|
||||
viper.WatchConfig()
|
||||
viper.SetDefault("logLevel", "info")
|
||||
logLevelCfg := viper.GetString("logLevel")
|
||||
switch logLevelCfg {
|
||||
case "debug":
|
||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
case "info":
|
||||
slog.SetLogLoggerLevel(slog.LevelInfo)
|
||||
case "warn":
|
||||
slog.SetLogLoggerLevel(slog.LevelWarn)
|
||||
case "error":
|
||||
slog.SetLogLoggerLevel(slog.LevelError)
|
||||
}
|
||||
|
||||
if viper.InConfig("port") {
|
||||
port = viper.GetString("port")
|
||||
} else {
|
||||
slog.Error("Port not found in config file, exiting..")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
scheduler, err = gocron.NewScheduler()
|
||||
defer scheduler.Shutdown()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("Cannot create scheduler, %s, exiting..", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
initUtil()
|
||||
initComponent()
|
||||
scheduler.Start()
|
||||
|
||||
// routing
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
}).Methods("GET")
|
||||
|
||||
router.HandleFunc("/poo/latest", pooRecorder.HandleNotifyLatestPoo).Methods("GET")
|
||||
router.HandleFunc("/poo/record", pooRecorder.HandleRecordPoo).Methods("POST")
|
||||
|
||||
router.HandleFunc("/homeassistant/publish", homeassistant.HandleHaMessage).Methods("POST")
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + port,
|
||||
Handler: router,
|
||||
|
||||
Reference in New Issue
Block a user