forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuds.go
43 lines (33 loc) · 791 Bytes
/
uds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// +build !windows
package server
import (
"net"
"net/http"
"os"
"github.com/andig/evcc/core"
)
// SocketPath is the unix domain socket path
const SocketPath = "/tmp/evcc"
// remoteIfExists deletes file if it exists or fails
func remoteIfExists(file string) {
_, err := os.Stat(file)
if err == nil {
err = os.Remove(file)
}
if err != nil && !os.IsNotExist(err) {
log.FATAL.Fatal(err)
}
}
// HealthListener attaches listener to unix domain socket and runs listener
func HealthListener(site core.SiteAPI) {
remoteIfExists(SocketPath)
l, err := net.Listen("unix", SocketPath)
if err != nil {
log.FATAL.Fatal(err)
}
defer l.Close()
mux := http.NewServeMux()
httpd := http.Server{Handler: mux}
mux.HandleFunc("/health", HealthHandler(site))
_ = httpd.Serve(l)
}