Skip to content

Commit

Permalink
Issue #68: Add per-site options for flexibility in checking status
Browse files Browse the repository at this point in the history
  • Loading branch information
daledavies committed Mar 3, 2023
1 parent 4e7ac32 commit 2ef9c12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions jumpapp/classes/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct(private Config $config, array $sitearray, private ar
$this->iconname = $sitearray['icon'] ?? null;
$this->tags = $sitearray['tags'] ?? $this->tags;
$this->description = isset($sitearray['description']) ? $sitearray['description'] : $sitearray['name'];
$this->status = $sitearray['status'] ?? null;
}

/**
Expand Down
16 changes: 13 additions & 3 deletions jumpapp/classes/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ public function get_status(): string {
* @return string
*/
private function do_request(): string {
// Grab some details if they exist from the site options.
$url = $this->site->status->url ?? $this->site->url;
$method = !in_array(($this->site->status->request_method ?? null), ['HEAD', 'GET']) ? 'HEAD' : $this->site->status->request_method;
// Try to make a request and see what we get back.
try {
if ($this->client->request('HEAD', $this->site->url)) {
if ($this->client->request($method, $url)) {
return self::STATUS_ONLINE;
}
} catch (\GuzzleHttp\Exception\ConnectException) {
// Catch instances where we cant connect.
return self::STATUS_OFFLINE;
} catch (\GuzzleHttp\Exception\BadResponseException) {
// Catch 4xx and 5xx errors.
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
// This exception is thrown on 4xx and 5xx errors, however we want to ensure we dont
// show an error status in the UI if the response code is in the list of allowed codes.
// E.g. the server response with "418 I'm a teapot".
$status = $e->getResponse()->getStatusCode();
if (in_array($status, ((array)$this->site->status->allowed_status_codes ?? []))) {
return self::STATUS_ONLINE;
}
return self::STATUS_ERROR;
} catch (\Exception) {
// If anything went wrong or we had some other status code.
Expand Down
13 changes: 12 additions & 1 deletion jumpapp/sites/sites.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@
"name": "Pi-hole",
"url" : "https://pi-hole.net/",
"nofollow": false,
"tags": ["stuff", "things"]
"tags": ["home", "things"]
},
{
"name": "Teapot",
"url" : "https://www.google.com/pagedoesnotexist",
"nofollow": false,
"tags": ["stuff", "things"],
"status": {
"allowed_status_codes": [418],
"request_method": "GET",
"url": "https://www.google.com/teapot"
}
}
]
}

0 comments on commit 2ef9c12

Please sign in to comment.