2021-12-11 23:23:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-10-23 13:06:45 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v2/plugins"
|
2021-12-11 23:23:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const statusIRCMessageReceivedTimeout = 5 * time.Minute
|
|
|
|
|
|
|
|
var statusIRCMessageReceived time.Time
|
|
|
|
|
|
|
|
type (
|
|
|
|
statusResponse struct {
|
|
|
|
Checks []statusResponseCheck `json:"checks"`
|
|
|
|
OverallStatusSuccess bool `json:"overall_status_success"`
|
|
|
|
}
|
|
|
|
|
|
|
|
statusResponseCheck struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
|
|
|
|
checkFn func() error
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registerRoute(plugins.HTTPRouteRegistrationArgs{
|
2022-03-30 22:29:27 +00:00
|
|
|
Description: "Provides a status JSON to check whether the bot is living",
|
2021-12-11 23:23:06 +00:00
|
|
|
HandlerFunc: handleStatusRequest,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Module: "status",
|
2022-03-30 22:29:27 +00:00
|
|
|
Name: "Status",
|
2021-12-11 23:23:06 +00:00
|
|
|
Path: "/status.json",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Set the response status for failing checks",
|
|
|
|
Name: "fail-status",
|
|
|
|
Required: false,
|
|
|
|
Type: "integer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RequiresWriteAuth: false,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleStatusRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
failStatus := http.StatusInternalServerError
|
|
|
|
if v, err := strconv.Atoi(r.FormValue("fail-status")); err == nil {
|
|
|
|
failStatus = v
|
|
|
|
}
|
|
|
|
|
|
|
|
output := statusResponse{
|
|
|
|
OverallStatusSuccess: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chk := range []statusResponseCheck{
|
|
|
|
{
|
2021-12-31 12:42:37 +00:00
|
|
|
Name: "Chat connection alive",
|
|
|
|
Description: fmt.Sprintf("Chat connection received a message in last %s", statusIRCMessageReceivedTimeout),
|
2021-12-11 23:23:06 +00:00
|
|
|
checkFn: func() error {
|
|
|
|
if time.Since(statusIRCMessageReceived) > statusIRCMessageReceivedTimeout {
|
|
|
|
return errors.New("message lifetime expired")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Twitch Client Authorized",
|
|
|
|
Description: "Twitch Client is authorized and can fetch authorized user",
|
|
|
|
checkFn: func() error {
|
|
|
|
if twitchClient == nil {
|
|
|
|
return errors.New("not initialized")
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:47:30 +00:00
|
|
|
_, _, err := twitchClient.GetAuthorizedUser()
|
2021-12-11 23:23:06 +00:00
|
|
|
return errors.Wrap(err, "fetching username")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
err := chk.checkFn()
|
|
|
|
if err != nil {
|
|
|
|
chk.Error = err.Error()
|
|
|
|
}
|
|
|
|
chk.Success = err == nil
|
|
|
|
|
|
|
|
output.Checks = append(output.Checks, chk)
|
|
|
|
output.OverallStatusSuccess = output.OverallStatusSuccess && chk.Success
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if !output.OverallStatusSuccess {
|
|
|
|
w.WriteHeader(failStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(output); err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "encoding output").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|