65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
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
|
|
}
|