mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 16:50:01 +00:00
[core] Add status / health check API
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
f36ae7e05f
commit
11e55fc5cb
2 changed files with 110 additions and 0 deletions
3
irc.go
3
irc.go
|
@ -96,6 +96,9 @@ func (i ircHandler) ExecutePart(channel string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
|
func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
|
||||||
|
// We've received a message, update status check
|
||||||
|
statusIRCMessageReceived = time.Now()
|
||||||
|
|
||||||
go func(m *irc.Message) {
|
go func(m *irc.Message) {
|
||||||
configLock.RLock()
|
configLock.RLock()
|
||||||
defer configLock.RUnlock()
|
defer configLock.RUnlock()
|
||||||
|
|
107
status.go
Normal file
107
status.go
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/Luzifer/twitch-bot/plugins"
|
||||||
|
)
|
||||||
|
|
||||||
|
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{
|
||||||
|
HandlerFunc: handleStatusRequest,
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Module: "status",
|
||||||
|
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{
|
||||||
|
{
|
||||||
|
Name: "IRC connection alive",
|
||||||
|
Description: fmt.Sprintf("IRC connection received a message in last %s", statusIRCMessageReceivedTimeout),
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := twitchClient.GetAuthorizedUsername()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue