From dd477e8d6646c5c925b924fe9c31169c87bd9273 Mon Sep 17 00:00:00 2001 From: janic Date: Sun, 12 Mar 2023 00:18:13 +0100 Subject: [PATCH] Should work like this --- src/api/api_10_vars.go | 2 +- src/api/api_20_struct.go | 2 +- src/api/api_30_get_health.go | 8 +++++-- src/api/api_30_get_status.go | 12 ++++++++-- src/helper/loadenv_10_vars.go | 2 ++ src/helper/printenv.go | 1 + src/main.go | 42 ++++++++++++++++++++++++++++++++--- 7 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/api/api_10_vars.go b/src/api/api_10_vars.go index f0bb770..9f36d06 100644 --- a/src/api/api_10_vars.go +++ b/src/api/api_10_vars.go @@ -1,3 +1,3 @@ package api -var server_status string = "OK" +var Server_Status string = "OK" diff --git a/src/api/api_20_struct.go b/src/api/api_20_struct.go index 32ff6e4..e046afc 100644 --- a/src/api/api_20_struct.go +++ b/src/api/api_20_struct.go @@ -1,6 +1,6 @@ package api -type Status struct { +type AppStatus struct { Hostname string `json: hostname` Server_status string `json: server_status` TTL int `json: ttl` diff --git a/src/api/api_30_get_health.go b/src/api/api_30_get_health.go index 88f4aac..45ea215 100644 --- a/src/api/api_30_get_health.go +++ b/src/api/api_30_get_health.go @@ -3,10 +3,14 @@ package api import ( "net/http" + "gitea.voser.cloud/Container/iwilldie/src/helper" "github.com/gin-gonic/gin" ) func GET_health(c *gin.Context) { - c.Data(http.StatusOK, "text/plain", []byte(server_status)) - return + if helper.TTL > 0 { + c.Data(http.StatusOK, "text/plain", []byte("OK")) + } else { + c.Data(http.StatusOK, "text/plain", []byte("Unhealthy")) + } } diff --git a/src/api/api_30_get_status.go b/src/api/api_30_get_status.go index 3750a54..0085dc0 100644 --- a/src/api/api_30_get_status.go +++ b/src/api/api_30_get_status.go @@ -3,10 +3,18 @@ package api import ( "net/http" + "gitea.voser.cloud/Container/iwilldie/src/helper" "github.com/gin-gonic/gin" ) func GET_status(c *gin.Context) { - response := "OK" - c.Data(http.StatusOK, "text/plain", []byte(response)) + as := AppStatus{ + Server_status: Server_Status, + TTL: helper.TTL, + Max_TTL: helper.MAX_TTL, + Min_TTL: helper.MIN_TTL, + Hardfail: helper.HARDFAIL, + Hardfail_delay: helper.HARDFAIL_DELAY, + } + c.JSON(http.StatusOK, as) } diff --git a/src/helper/loadenv_10_vars.go b/src/helper/loadenv_10_vars.go index 0424621..90286de 100644 --- a/src/helper/loadenv_10_vars.go +++ b/src/helper/loadenv_10_vars.go @@ -5,3 +5,5 @@ var MAX_TTL int var MIN_TTL int var HARDFAIL bool var HARDFAIL_DELAY int + +var TTL int diff --git a/src/helper/printenv.go b/src/helper/printenv.go index 8d95fa7..849438c 100644 --- a/src/helper/printenv.go +++ b/src/helper/printenv.go @@ -12,5 +12,6 @@ MIN_TTL: %d HARDFAIL: %t HARDFAIL_DELAY: %d ################################################################################ + `, MAX_TTL, MIN_TTL, HARDFAIL, HARDFAIL_DELAY) } diff --git a/src/main.go b/src/main.go index c7026e4..5a8b773 100644 --- a/src/main.go +++ b/src/main.go @@ -2,6 +2,9 @@ package main import ( "fmt" + "log" + "math/rand" + "time" "gitea.voser.cloud/Container/iwilldie/src/api" "gitea.voser.cloud/Container/iwilldie/src/helper" @@ -11,15 +14,16 @@ import ( func main() { helper.Loadenv() helper.Printenv() + start_death_timer() start_server() - result := helper.Func2test(2, 16) - fmt.Printf("The result is %d \n", result) + stop_server() } // Start Server func start_server() { - fmt.Println("Start Server now") + fmt.Printf("\nStart Server now\n") + gin.SetMode(gin.ReleaseMode) router := gin.Default() // Favicon router.StaticFile("/favicon.ico", "./assets/favicon.ico") @@ -31,6 +35,38 @@ func start_server() { router.Run("127.0.0.1:8000") } +// Start Death Timer +func start_death_timer() { + + // initialize the random number generator with the current time + rand.Seed(time.Now().UnixNano()) + + // generate a random integer between 1 and 10 + delta_ttl := helper.MAX_TTL - helper.MIN_TTL + helper.TTL = rand.Intn(helper.MIN_TTL) + delta_ttl + fmt.Printf("The TTL for this instance was set to %d seconds", helper.TTL) + go func() { + for { + time.Sleep(time.Second) + helper.TTL-- + if helper.TTL == 0 { + api.Server_Status = "Unhealthy" + if helper.HARDFAIL != true { + for { + time.Sleep(time.Second) + helper.HARDFAIL_DELAY-- + if helper.HARDFAIL_DELAY == 0 { + log.Fatal("Hardfail was set to false") + } + } + } else { + log.Panic("Hardfail was set to true") + } + } + } + }() +} + // Stop Server func stop_server() { fmt.Println("Stop Server now")