Files
slideshowApp/handlers/infos.go
2026-01-17 22:41:42 +01:00

54 lines
967 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) {
port := env.Port.GetValue()
if port == "" {
port = "8080"
}
speed := env.IntervalDefault.GetValue()
if speed == "" {
speed = "10"
}
data := map[string]string{
"ip": GetLocalIP(),
"port": port,
"speed": speed,
}
json.NewEncoder(w).Encode(data)
}