-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #631 from cj123/healthcheck-endpoint
add a health check endpoint
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package servermanager | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/cj123/assetto-server-manager/pkg/udp" | ||
) | ||
|
||
var LaunchTime = time.Now() | ||
|
||
type HealthCheck struct { | ||
raceControl *RaceControl | ||
} | ||
|
||
func NewHealthCheck(raceControl *RaceControl) *HealthCheck { | ||
return &HealthCheck{raceControl: raceControl} | ||
} | ||
|
||
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 | ||
|
||
EventInProgress bool | ||
EventIsCritical bool | ||
NumConnectedDrivers int | ||
MaxClientsOverride int | ||
} | ||
|
||
func (h *HealthCheck) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
event := h.raceControl.process.Event() | ||
|
||
_ = json.NewEncoder(w).Encode(HealthCheckResponse{ | ||
OK: true, | ||
OS: runtime.GOOS + "/" + runtime.GOARCH, | ||
Version: BuildVersion, | ||
IsPremium: IsPremium == "true", | ||
IsHosted: IsHosted, | ||
MaxClientsOverride: MaxClientsOverride, | ||
NumCPU: runtime.NumCPU(), | ||
NumGoroutines: runtime.NumGoroutine(), | ||
Uptime: time.Since(LaunchTime).String(), | ||
GoVersion: runtime.Version(), | ||
|
||
EventInProgress: h.raceControl.process.IsRunning(), | ||
EventIsCritical: !event.IsPractice() && (event.IsChampionship() || event.IsRaceWeekend() || h.raceControl.SessionInfo.Type == udp.SessionTypeRace || h.raceControl.SessionInfo.Type == udp.SessionTypeQualifying), | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters