From 0a46061c07cc36e930074b100c02e1749cb38b2f Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 27 Sep 2018 16:00:16 +0200 Subject: [PATCH] Add apitest program --- apitest.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ portbase.go | 2 ++ 2 files changed, 97 insertions(+) create mode 100644 apitest.go diff --git a/apitest.go b/apitest.go new file mode 100644 index 0000000..ade0d70 --- /dev/null +++ b/apitest.go @@ -0,0 +1,95 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + "syscall" + "time" + + "github.com/Safing/portbase/info" + "github.com/Safing/portbase/log" + "github.com/Safing/portbase/modules" + // include packages here + + _ "github.com/Safing/portbase/api" + _ "github.com/Safing/portbase/api/testclient" + "github.com/Safing/portbase/config" + _ "github.com/Safing/portbase/crypto/random" + _ "github.com/Safing/portbase/database/dbmodule" +) + +// var ( +// err = debugStartProblems() +// ) +// +// func debugStartProblems() error { +// go func() { +// time.Sleep(1 * time.Second) +// fmt.Println("===== TAKING TOO LONG - PRINTING STACK TRACES =====") +// pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) +// os.Exit(1) +// }() +// return nil +// } + +func main() { + + // Set Info + info.Set("Portbase API Development Helper", "0.0.1") + + // Register some dummy config vars + + // Start + err := modules.Start() + if err != nil { + if err == modules.ErrCleanExit { + os.Exit(0) + } else { + modules.Shutdown() + os.Exit(1) + } + } + + // Test config option + config.Register(&config.Option{ + Name: "Explode on error", + Key: "test/explode_on_error", + Description: "Defines how hard we should crash, in case of an error, in Joule.", + ExpertiseLevel: config.ExpertiseLevelDeveloper, + OptType: config.OptTypeInt, + DefaultValue: 1, + }) + go func() { + for { + for i := 0; i < 1000; i++ { + time.Sleep(1 * time.Second) + if i%2 == 0 { + config.SetConfigOption("test/explode_on_error", i) + } else { + config.SetDefaultConfigOption("test/explode_on_error", i) + } + } + } + }() + + // Shutdown + // catch interrupt for clean shutdown + signalCh := make(chan os.Signal) + signal.Notify( + signalCh, + os.Interrupt, + syscall.SIGHUP, + syscall.SIGINT, + syscall.SIGTERM, + syscall.SIGQUIT, + ) + select { + case <-signalCh: + fmt.Println(" ") + log.Warning("main: program was interrupted, shutting down.") + modules.Shutdown() + case <-modules.ShuttingDown(): + } + +} diff --git a/portbase.go b/portbase.go index 842772e..353b171 100644 --- a/portbase.go +++ b/portbase.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "os/signal" "syscall" @@ -39,6 +40,7 @@ func main() { ) select { case <-signalCh: + fmt.Println(" ") log.Warning("main: program was interrupted, shutting down.") modules.Shutdown() case <-modules.ShuttingDown():