Add HTTPProber widget and API to display website statuses
This commit is contained in:
64
widgets/httpProber.go
Normal file
64
widgets/httpProber.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"ArinDash/apis/httpprober"
|
||||
"ArinDash/util"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mum4k/termdash/cell"
|
||||
"github.com/mum4k/termdash/terminal/terminalapi"
|
||||
"github.com/mum4k/termdash/widgetapi"
|
||||
"github.com/mum4k/termdash/widgets/text"
|
||||
)
|
||||
|
||||
type HTTPProberOptions struct {
|
||||
}
|
||||
|
||||
func HTTPProber() HTTPProberOptions {
|
||||
widgetOptions["HTTPProberOptions"] = createHTTPProberList
|
||||
return HTTPProberOptions{}
|
||||
}
|
||||
|
||||
func createHTTPProberList(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
||||
list := util.PanicOnErrorWithResult(text.New())
|
||||
go util.Periodic(ctx, 1*time.Minute, func() error {
|
||||
websites := httpprober.RunProbe()
|
||||
sort.Slice(websites, func(i, j int) bool {
|
||||
return websites[i].Status < websites[j].Status
|
||||
})
|
||||
sort.Slice(websites, func(i, j int) bool {
|
||||
return websites[i].URL < websites[j].URL
|
||||
})
|
||||
list.Reset()
|
||||
for _, website := range websites {
|
||||
var status []cell.Option
|
||||
if website.Status >= 500 {
|
||||
status = append(status, cell.FgColor(cell.ColorRed))
|
||||
status = append(status, cell.BgColor(cell.ColorBlack))
|
||||
} else if website.Status >= 400 {
|
||||
status = append(status, cell.FgColor(cell.ColorYellow))
|
||||
status = append(status, cell.BgColor(cell.ColorDefault))
|
||||
} else if website.Status >= 200 {
|
||||
status = append(status, cell.FgColor(cell.ColorGreen))
|
||||
status = append(status, cell.BgColor(cell.ColorDefault))
|
||||
} else {
|
||||
status = append(status, cell.FgColor(cell.ColorGray))
|
||||
status = append(status, cell.BgColor(cell.ColorDefault))
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%2s ", website.Icon), text.WriteCellOpts(status...)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%s\n", strings.Split(website.URL, "://")[1]), text.WriteCellOpts(status...)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return list
|
||||
}
|
||||
Reference in New Issue
Block a user