Should work like this
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Janic Voser 2023-03-12 00:18:13 +01:00
parent f70b58c70d
commit dd477e8d66
7 changed files with 60 additions and 9 deletions

View File

@ -1,3 +1,3 @@
package api package api
var server_status string = "OK" var Server_Status string = "OK"

View File

@ -1,6 +1,6 @@
package api package api
type Status struct { type AppStatus struct {
Hostname string `json: hostname` Hostname string `json: hostname`
Server_status string `json: server_status` Server_status string `json: server_status`
TTL int `json: ttl` TTL int `json: ttl`

View File

@ -3,10 +3,14 @@ package api
import ( import (
"net/http" "net/http"
"gitea.voser.cloud/Container/iwilldie/src/helper"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func GET_health(c *gin.Context) { func GET_health(c *gin.Context) {
c.Data(http.StatusOK, "text/plain", []byte(server_status)) if helper.TTL > 0 {
return c.Data(http.StatusOK, "text/plain", []byte("OK"))
} else {
c.Data(http.StatusOK, "text/plain", []byte("Unhealthy"))
}
} }

View File

@ -3,10 +3,18 @@ package api
import ( import (
"net/http" "net/http"
"gitea.voser.cloud/Container/iwilldie/src/helper"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func GET_status(c *gin.Context) { func GET_status(c *gin.Context) {
response := "OK" as := AppStatus{
c.Data(http.StatusOK, "text/plain", []byte(response)) 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)
} }

View File

@ -5,3 +5,5 @@ var MAX_TTL int
var MIN_TTL int var MIN_TTL int
var HARDFAIL bool var HARDFAIL bool
var HARDFAIL_DELAY int var HARDFAIL_DELAY int
var TTL int

View File

@ -12,5 +12,6 @@ MIN_TTL: %d
HARDFAIL: %t HARDFAIL: %t
HARDFAIL_DELAY: %d HARDFAIL_DELAY: %d
################################################################################ ################################################################################
`, MAX_TTL, MIN_TTL, HARDFAIL, HARDFAIL_DELAY) `, MAX_TTL, MIN_TTL, HARDFAIL, HARDFAIL_DELAY)
} }

View File

@ -2,6 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"log"
"math/rand"
"time"
"gitea.voser.cloud/Container/iwilldie/src/api" "gitea.voser.cloud/Container/iwilldie/src/api"
"gitea.voser.cloud/Container/iwilldie/src/helper" "gitea.voser.cloud/Container/iwilldie/src/helper"
@ -11,15 +14,16 @@ import (
func main() { func main() {
helper.Loadenv() helper.Loadenv()
helper.Printenv() helper.Printenv()
start_death_timer()
start_server() start_server()
result := helper.Func2test(2, 16)
fmt.Printf("The result is %d \n", result)
stop_server() stop_server()
} }
// Start Server // Start Server
func start_server() { func start_server() {
fmt.Println("Start Server now") fmt.Printf("\nStart Server now\n")
gin.SetMode(gin.ReleaseMode)
router := gin.Default() router := gin.Default()
// Favicon // Favicon
router.StaticFile("/favicon.ico", "./assets/favicon.ico") router.StaticFile("/favicon.ico", "./assets/favicon.ico")
@ -31,6 +35,38 @@ func start_server() {
router.Run("127.0.0.1:8000") 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 // Stop Server
func stop_server() { func stop_server() {
fmt.Println("Stop Server now") fmt.Println("Stop Server now")