This commit is contained in:
		@@ -1,3 +1,3 @@
 | 
			
		||||
package api
 | 
			
		||||
 | 
			
		||||
var server_status string = "OK"
 | 
			
		||||
var Server_Status string = "OK"
 | 
			
		||||
 
 | 
			
		||||
@@ -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`
 | 
			
		||||
 
 | 
			
		||||
@@ -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"))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,3 +5,5 @@ var MAX_TTL int
 | 
			
		||||
var MIN_TTL int
 | 
			
		||||
var HARDFAIL bool
 | 
			
		||||
var HARDFAIL_DELAY int
 | 
			
		||||
 | 
			
		||||
var TTL int
 | 
			
		||||
 
 | 
			
		||||
@@ -12,5 +12,6 @@ MIN_TTL: %d
 | 
			
		||||
HARDFAIL: %t
 | 
			
		||||
HARDFAIL_DELAY: %d
 | 
			
		||||
################################################################################
 | 
			
		||||
 | 
			
		||||
`, MAX_TTL, MIN_TTL, HARDFAIL, HARDFAIL_DELAY)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										42
									
								
								src/main.go
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								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")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user