47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// API health is deliberately independent of streaming URLs and playback.
|
|
func playerHealth(body []byte) error {
|
|
var response struct {
|
|
PlayabilityStatus struct {
|
|
Status string `json:"status"`
|
|
} `json:"playabilityStatus"`
|
|
}
|
|
if len(body) > playerResponseLimit || json.Unmarshal(body, &response) != nil {
|
|
return errors.New("invalid player response")
|
|
}
|
|
if response.PlayabilityStatus.Status != "OK" {
|
|
return errors.New("player API is unhealthy: missing or non-OK playability status")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func healthYoutube(stdout, stderr io.Writer, runner playerRunner) int {
|
|
cfg, err := parseHealthConfig(os.LookupEnv)
|
|
if err != nil {
|
|
fmt.Fprintln(stderr, "error:", err)
|
|
return 2
|
|
}
|
|
body, err := requestPlayer(cfg.VideoID, cfg.SOCKSProxy, runner)
|
|
if err == nil {
|
|
err = playerHealth(body)
|
|
}
|
|
if err != nil {
|
|
fmt.Fprintln(stderr, "error:", err)
|
|
return 1
|
|
}
|
|
if _, err := fmt.Fprintln(stdout, "YouTube Player API: OK (not a playback check)"); err != nil {
|
|
fmt.Fprintln(stderr, "error: cannot write health result")
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|