-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
healthcheck.go
114 lines (95 loc) · 3.25 KB
/
healthcheck.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package servermanager
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/JustaPenguin/assetto-server-manager/pkg/udp"
)
var LaunchTime = time.Now()
type HealthCheck struct {
raceControl *RaceControl
process ServerProcess
store Store
}
func NewHealthCheck(raceControl *RaceControl, store Store, process ServerProcess) *HealthCheck {
return &HealthCheck{
store: store,
raceControl: raceControl,
process: process,
}
}
type HealthCheckResponse struct {
OK bool
Version string
IsPremium bool
IsHosted bool
OS string
NumCPU int
NumGoroutines int
Uptime string
GoVersion string
AssettoIsInstalled bool
StrackerIsInstalled bool
CarDirectoryIsWritable bool
TrackDirectoryIsWritable bool
WeatherDirectoryIsWritable bool
SetupsDirectoryIsWritable bool
ConfigDirectoryIsWritable bool
ResultsDirectoryIsWritable bool
ServerName string
ServerID ServerID
EventInProgress bool
EventIsCritical bool
EventIsChampionship bool
EventIsRaceWeekend bool
EventIsPractice bool
NumConnectedDrivers int
MaxClientsOverride int
}
func (h *HealthCheck) ServeHTTP(w http.ResponseWriter, r *http.Request) {
event := h.process.Event()
opts, err := h.store.LoadServerOptions()
var serverName string
if err == nil {
serverName = opts.Name
}
_ = json.NewEncoder(w).Encode(HealthCheckResponse{
OK: true,
OS: runtime.GOOS + "/" + runtime.GOARCH,
Version: BuildVersion,
IsPremium: Premium(),
IsHosted: IsHosted,
MaxClientsOverride: MaxClientsOverride,
NumCPU: runtime.NumCPU(),
NumGoroutines: runtime.NumGoroutine(),
Uptime: time.Since(LaunchTime).String(),
GoVersion: runtime.Version(),
ServerName: serverName,
ServerID: serverID,
EventInProgress: h.raceControl.process.IsRunning(),
EventIsCritical: !event.IsPractice() && (event.IsChampionship() || event.IsRaceWeekend() || h.raceControl.SessionInfo.Type == udp.SessionTypeRace || h.raceControl.SessionInfo.Type == udp.SessionTypeQualifying),
EventIsChampionship: event.IsChampionship(),
EventIsRaceWeekend: event.IsRaceWeekend(),
EventIsPractice: event.IsPractice(),
NumConnectedDrivers: h.raceControl.ConnectedDrivers.Len(),
AssettoIsInstalled: IsAssettoInstalled(),
StrackerIsInstalled: IsStrackerInstalled(),
ConfigDirectoryIsWritable: IsDirWriteable(filepath.Join(ServerInstallPath, "cfg")) == nil,
CarDirectoryIsWritable: IsDirWriteable(filepath.Join(ServerInstallPath, "content", "cars")) == nil,
TrackDirectoryIsWritable: IsDirWriteable(filepath.Join(ServerInstallPath, "content", "tracks")) == nil,
WeatherDirectoryIsWritable: IsDirWriteable(filepath.Join(ServerInstallPath, "content", "weather")) == nil,
SetupsDirectoryIsWritable: IsDirWriteable(filepath.Join(ServerInstallPath, "setups")) == nil,
ResultsDirectoryIsWritable: IsDirWriteable(filepath.Join(ServerInstallPath, "results")) == nil,
})
}
func IsDirWriteable(dir string) error {
file := filepath.Join(dir, ".test-write")
if err := ioutil.WriteFile(file, []byte(""), 0600); err != nil {
return err
}
return os.Remove(file)
}