first commit
This commit is contained in:
51
handlers/infos.go
Normal file
51
handlers/infos.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user