-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhealthCheck.go
More file actions
52 lines (39 loc) · 1 KB
/
healthCheck.go
File metadata and controls
52 lines (39 loc) · 1 KB
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
44
45
46
47
48
49
50
51
52
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
)
// Will add more stats pending feedback
type HealthState struct {
Hostname string
PID int
}
func handleHealthCheck(res http.ResponseWriter, req *http.Request) {
ip, _, _ := net.SplitHostPort(req.RemoteAddr)
log.Println("Health Check From", ip)
// Having to do this makes me think I'm bad at Go
hostname, _ := os.Hostname()
healthstate := HealthState{
hostname,
os.Getpid(),
}
payload, err := json.Marshal(healthstate)
res.Header().Set("Server", "301 Redirect Server")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "application/json")
res.Write(payload)
}
func healthCheckServer(port int) {
healthstring := fmt.Sprintf(":%d", port)
fmt.Println("Listening for health checks on", port)
healthCheck := http.NewServeMux()
healthCheck.HandleFunc("/", handleHealthCheck)
go func() { http.ListenAndServe(healthstring, healthCheck) }()
}