From 36a6da574dd487e764b43902dd6bddc1863dcc00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Thu, 21 Jan 2016 14:46:31 +0100 Subject: [PATCH] Added support for online/offline website badge. --- server.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ try.html | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/server.js b/server.js index 2b65b56..50edee9 100644 --- a/server.js +++ b/server.js @@ -5122,6 +5122,52 @@ cache(function(data, match, sendBadge, request) { }); })); +// Test if a webpage is online +camp.route(/^\/website(-(([^-]|--)*?)-(([^-]|--)*)(-(([^-]|--)+)-(([^-]|--)+))?)?\/(.+)\/(.+)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var onlineMessage = escapeFormat(match[2] != null ? match[2] : "online"); + var offlineMessage = escapeFormat(match[4] != null ? match[4] : "offline"); + var onlineColor = escapeFormat(match[7] != null ? match[7] : "brightgreen"); + var offlineColor = escapeFormat(match[9] != null ? match[9] : "red"); + var userProtocol = match[11]; + var userURI = match[12]; + var format = match[13]; + var withProtocolURI = userProtocol + "://" + userURI; + var options = { + method: 'HEAD', + uri: withProtocolURI, + }; + var badgeData = getBadgeData('website', data); + badgeData.colorscheme = undefined; + request(options, function(err, res) { + try { + // We consider all HTTP status codes below 310 as success. + if (err != null || res.statusCode >= 310) { + badgeData.text[1] = offlineMessage; + if (sixHex(offlineColor)) { + badgeData.colorB = '#' + offlineColor; + } else { + badgeData.colorscheme = offlineColor; + } + sendBadge(format, badgeData); + return; + } else { + badgeData.text[1] = onlineMessage; + if (sixHex(onlineColor)) { + badgeData.colorB = '#' + onlineColor; + } else { + badgeData.colorscheme = onlineColor; + } + sendBadge(format, badgeData); + return; + } + } catch(e) { + badge({text: ['error', 'bad badge'], colorscheme: 'red'}, + makeSend(format, ask.res, end)); + } + }); +})); + // Any badge. camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/, function(data, match, end, ask) { diff --git a/try.html b/try.html index a728acc..ff11424 100644 --- a/try.html +++ b/try.html @@ -387,6 +387,10 @@ Pixel-perfect   Retina-ready   Fast   Consistent   Hackable https://img.shields.io/chrome-web-store/d/nimelepbpejjlbmoobocpfnjhihnpked.svg + Website: + + https://img.shields.io/website-up-down-green-red/http/shields.io.svg +

Version

@@ -1122,6 +1126,48 @@ is where the current server got started. https://img.shields.io/vso/build/TEAM_NAME/PROJECT_ID/BUILD_DEFINITION_ID.

+
+

The badge is of the form https://img.shields.io/website[OPTIONS]/PROTOCOL/URLREST.svg, + the simplest case being https://img.shields.io/website/http/www.website.com.svg. + More options are described below. +

+

The whole URL is obtained by concatenating the PROTOCOL (http or https, for example) + with the URLREST (separating them with ://). +

+

+ The existance of a specific path on the server can be checked by appending a path after the domain name, e.g. + https://img.shields.io/website/http/www.website.com/path/to/page.html.svg. +

+

The URLREST should be URLEncoded: + + + +

+

[OPTIONS] can be: +

+
+ + + +
Dashes -- + → + - Dash +
Underscores __ + → + _ Underscore
+
_ or Space   + → +   Space +
+

+