Parcourir la source

Merge pull request #314 from JeckDev/release/v0.7.0

feat: add alternate link (error-url) for monitor widget sites
Svilen Markov il y a 5 mois
Parent
commit
a83389bb7d
2 fichiers modifiés avec 19 ajouts et 6 suppressions
  1. 5 0
      docs/configuration.md
  2. 14 6
      internal/glance/widget-monitor.go

+ 5 - 0
docs/configuration.md

@@ -1220,6 +1220,7 @@ Properties for each site:
 | title | string | yes | |
 | url | string | yes | |
 | check-url | string | no | |
+| error-url | string | no | |
 | icon | string | no | |
 | allow-insecure | boolean | no | false |
 | same-tab | boolean | no | false |
@@ -1237,6 +1238,10 @@ The public facing URL of a monitored service, the user will be redirected here.
 
 The URL which will be requested and its response will determine the status of the site. If not specified, the `url` property is used.
 
+`error-url`
+
+If the monitored service returns an error, the user will be redirected here. If not specified, the `url` property is used.
+
 `icon`
 
 Optional URL to an image which will be used as the icon for the site. Can be an external URL or internal via [server configured assets](#assets-path). You can also directly use [Simple Icons](https://simpleicons.org/) via a `si:` prefix or [Dashboard Icons](https://github.com/walkxcode/dashboard-icons) via a `di:` prefix:

+ 14 - 6
internal/glance/widget-monitor.go

@@ -20,6 +20,8 @@ type monitorWidget struct {
 	Sites      []struct {
 		*SiteStatusRequest `yaml:",inline"`
 		Status             *siteStatus     `yaml:"-"`
+		URL                string          `yaml:"-"`
+		ErrorURL           string          `yaml:"error-url"`
 		Title              string          `yaml:"title"`
 		Icon               customIconField `yaml:"icon"`
 		SameTab            bool            `yaml:"same-tab"`
@@ -58,10 +60,16 @@ func (widget *monitorWidget) update(ctx context.Context) {
 		status := &statuses[i]
 		site.Status = status
 
-		if !slices.Contains(site.AltStatusCodes, status.Code) && (status.Code >= 400 || status.TimedOut || status.Error != nil) {
+		if !slices.Contains(site.AltStatusCodes, status.Code) && (status.Code >= 400 || status.Error != nil) {
 			widget.HasFailing = true
 		}
 
+		if status.Error != nil && site.ErrorURL != "" {
+			site.URL = site.ErrorURL
+		} else {
+			site.URL = site.DefaultURL
+		}
+
 		site.StatusText = statusCodeToText(status.Code, site.AltStatusCodes)
 		site.StatusStyle = statusCodeToStyle(status.Code, site.AltStatusCodes)
 	}
@@ -88,12 +96,12 @@ func statusCodeToText(status int, altStatusCodes []int) string {
 	if status == 401 {
 		return "Unauthorized"
 	}
-	if status >= 400 {
-		return "Client Error"
-	}
 	if status >= 500 {
 		return "Server Error"
 	}
+	if status >= 400 {
+		return "Client Error"
+	}
 
 	return strconv.Itoa(status)
 }
@@ -107,7 +115,7 @@ func statusCodeToStyle(status int, altStatusCodes []int) string {
 }
 
 type SiteStatusRequest struct {
-	URL           string `yaml:"url"`
+	DefaultURL    string `yaml:"url"`
 	CheckURL      string `yaml:"check-url"`
 	AllowInsecure bool   `yaml:"allow-insecure"`
 }
@@ -124,7 +132,7 @@ func fetchSiteStatusTask(statusRequest *SiteStatusRequest) (siteStatus, error) {
 	if statusRequest.CheckURL != "" {
 		url = statusRequest.CheckURL
 	} else {
-		url = statusRequest.URL
+		url = statusRequest.DefaultURL
 	}
 	request, err := http.NewRequest(http.MethodGet, url, nil)
 	if err != nil {