Files
slideshowApp/handlers/infos.go
Adrian Zürcher 5e551b4eaf
All checks were successful
Build Slideshow App / build (amd64, , linux) (push) Successful in 1m37s
Build Slideshow App / build (amd64, .exe, windows) (push) Successful in 1m19s
Build Slideshow App / build (arm, 7, , linux) (push) Successful in 1m27s
Build Slideshow App / build (arm64, , linux) (push) Successful in 1m28s
add content-type header and error response
2026-01-31 20:14:54 +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 {
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")
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": GetInterval(),
}
err := json.NewEncoder(w).Encode(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}