Files
slideshowApp/handlers/infos.go
Adrian Zürcher 4d9e0836e5 first commit
2026-01-16 07:51:28 +01:00

52 lines
1.0 KiB
Go

package handlers
import (
"encoding/json"
"net"
"net/http"
"slideshowApp/env"
"strings"
)
// 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()
}
addrs, err := net.InterfaceAddrs()
if err != nil {
return "localhost"
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
if env.Env.GetValue() == "development" && !strings.Contains(ipnet.IP.String(), "192.168") {
continue
}
return ipnet.IP.String()
}
}
}
return "localhost"
}
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)
}