github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/health/webserverhealth.go (about)

     1  package health
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"time"
     8  
     9  	"github.com/mdaxf/iac/com"
    10  	"github.com/mdaxf/iac/health/checks"
    11  	"github.com/mdaxf/iac/logger"
    12  )
    13  
    14  // CheckSystemHealth is a function that checks the health of the system.
    15  // It takes a gin.Context as input and returns a map[string]interface{} and an error.
    16  // The function registers various health checks for different components of the system,
    17  // such as HTTP, MongoDB, MySQL, MQTT, and SignalR.
    18  // It measures the health of the system and returns the result as a JSON-encoded map.
    19  
    20  func CheckWebServerHealth(Node map[string]interface{}) (map[string]interface{}, error) {
    21  	iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "System Status Check"}
    22  
    23  	startTime := time.Now()
    24  	defer func() {
    25  		elapsed := time.Since(startTime)
    26  		iLog.PerformanceWithDuration("framework.health.CheckSystemHealth", elapsed)
    27  	}()
    28  
    29  	defer func() {
    30  		if r := recover(); r != nil {
    31  			fmt.Println("Error:", r)
    32  		}
    33  	}()
    34  
    35  	ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
    36  
    37  	h, _ := New(WithComponent(Component{
    38  		Name:         Node["Name"].(string),
    39  		Instance:     Node["AppID"].(string),
    40  		InstanceName: Node["Description"].(string),
    41  		InstanceType: Node["Type"].(string),
    42  		Version:      Node["Version"].(string),
    43  	}), WithChecks(Config{
    44  		Name:      "http",
    45  		Timeout:   time.Second * 5,
    46  		SkipOnErr: true,
    47  		Check: func(context context.Context) error {
    48  			checks.CheckHttpStatus(ctx, "/portal/uipage.html", time.Second*5)
    49  			return nil
    50  		},
    51  	}))
    52  
    53  	h.systemInfoEnabled = true
    54  
    55  	m := h.Measure(ctx)
    56  	data, err := json.Marshal(m)
    57  	if err != nil {
    58  		return make(map[string]interface{}), err
    59  	}
    60  
    61  	jdata, err := com.ConvertbytesToMap(data)
    62  
    63  	if err != nil {
    64  		return make(map[string]interface{}), err
    65  	}
    66  
    67  	return jdata, nil
    68  }