Files
slideshowApp/handlers/infos.go
2026-02-03 09:18:55 +01:00

59 lines
1.1 KiB
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 {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ipStr := ipnet.IP.String()
if ipnet.IP.IsLinkLocalUnicast() {
continue
}
return ipStr
}
}
}
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
}
}