Files
slideshowApp/handlers/infos.go
Adrian Zürcher e05c3a06ae
All checks were successful
Build Slideshow App / build (amd64, .exe, windows) (push) Successful in 2m0s
Build Slideshow App / build (amd64, , linux) (push) Successful in 2m20s
Build Slideshow App / build (arm, 7, , linux) (push) Successful in 1m57s
Build Slideshow App / build (arm64, , linux) (push) Successful in 1m58s
some changes
2026-01-31 20:37:03 +01:00

50 lines
992 B
Go

package handlers
import (
"encoding/json"
"net"
"net/http"
"slideshowApp/env"
)
// Helper to get the local network IP address
func GetLocalIP() string {
if env.Host.GetValue() != "0.0.0.0" && env.Host.GetValue() != "localhost" {
return env.Host.GetValue()
}
if ip := getActiveIP(); ip != "" {
return ip
}
return "localhost"
}
func getActiveIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return ""
}
defer conn.Close()
addr := conn.LocalAddr()
if udpAddr, ok := addr.(*net.UDPAddr); ok {
return udpAddr.IP.String()
}
return ""
}
func InfoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
data := map[string]string{
"ip": GetLocalIP(),
"port": env.Port.GetValue(),
"speed": GetInterval(),
}
err := json.NewEncoder(w).Encode(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}