From 2e62aa5579151bf394cc2cbb076729d3641184ab Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 12 Apr 2024 10:02:59 +0200 Subject: [PATCH] loading, converting and preparing env data --- main.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/main.go b/main.go index 82a4418..c9a64af 100644 --- a/main.go +++ b/main.go @@ -42,10 +42,94 @@ type Rectangle struct { endpoint Endpoint } +func GetEnvFallback(key string, fallback ...string) (string, error) { + // Check for function fallback signature + length := len(fallback) + if length > 1 { + return "", errors.New("Invalid signature: Too many fallback values provided!") + } + + // Try to get environment variable + value, exists := os.LookupEnv(key) + if exists { + return value, nil + } + + // Try using fallback value instead + if length == 1 { + return fallback[0], nil + } else if length == 0 { + // No fallback value provided + return "", errors.New("Panic: No fallback value provided!") + } + + // Could not get environment variable + return "", errors.New("Could not get environment variable: " + key) +} + +func ConvertFallback(input string, fallback ...int) (int, error) { + // Check for function fallback signature + length := len(fallback) + if length > 1 { + return 0, errors.New("Invalid signature: Too many fallback values provided!") + } + + // Try to convert input to int + value, err := strconv.Atoi(input) + if err == nil { + return value, nil + } + + // Try using fallback value instead + if length == 1 { + return fallback[0], nil + } else if length == 0 { + // No fallback value provided + return 0, errors.New("Panic: No fallback value provided!") + } + + // Could not convert input + return 0, err +} + +func LoadMatrixData() (string, int, int) { + // Remote server info + API_SERVER_IP, _ := GetEnvFallback(os.Getenv("API_SERVER_IP"), "localhost") + API_SERVER_PORT, _ := GetEnvFallback(os.Getenv("API_SERVER_PORT"), "8080") + + // LED panel info + PUBLIC_LED_WIDTH, _ := ConvertFallback(os.Getenv("PUBLIC_LED_WIDTH"), 64) + PUBLIC_LED_HEIGHT, _ := ConvertFallback(os.Getenv("PUBLIC_LED_HEIGHT"), 64) + + // Matrix arrangement info + PUBLIC_LED_CHAIN, _ := ConvertFallback(os.Getenv("PUBLIC_LED_CHAIN"), 1) + PUBLIC_LED_PARALLEL, _ := ConvertFallback(os.Getenv("PUBLIC_LED_PARALLEL"), 1) + + // Prepare remote server url + url := fmt.Sprintf("http://%s:%s", API_SERVER_IP, API_SERVER_PORT) + + // Calculate combined resolution + width := PUBLIC_LED_WIDTH * PUBLIC_LED_CHAIN + height := PUBLIC_LED_HEIGHT * PUBLIC_LED_PARALLEL + + return url, width, height +} + func main() { // Load .env file if it exists err := godotenv.Load(".env") if err != nil { log.Fatal("Error loading .env file") } + + // Load env server data + url, _, _ := LoadMatrixData() + + // Initialize resty client + client := resty.New() + + // Initialize custom matrix data + col := Color{R: 255, G: 0, B: 255, endpoint: COLOR} + rct := Rectangle{X: 0, Y: 0, W: 10, H: 10, endpoint: RECTANGLE} + upd := Update{endpoint: UPDATE} }