forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 1
/
website.service.js
98 lines (90 loc) · 2.11 KB
/
website.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict'
const { BaseService } = require('..')
const {
queryParamSchema,
exampleQueryParams,
renderWebsiteStatus,
} = require('../website-status')
const documentation = `
<p>
The badge is of the form
<code>https://img.shields.io/website/PROTOCOL/URLREST.svg</code>.
</p>
<p>
The whole URL is obtained by concatenating the <code>PROTOCOL</code>
(<code>http</code> or <code>https</code>, for example) with the
<code>URLREST</code> (separating them with <code>://</code>).
</p>
<p>
The existence of a specific path on the server can be checked by appending
a path after the domain name, e.g.
<code>https://img.shields.io/website/http/www.website.com/path/to/page.html.svg</code>.
</p>
<p>
The messages and colors for the up and down states can also be customized.
</p>
`
module.exports = class Website extends BaseService {
static get category() {
return 'monitoring'
}
static get route() {
return {
base: 'website',
pattern: ':protocol(https|http)/:hostAndPath+',
queryParamSchema,
}
}
static get examples() {
return [
{
title: 'Website',
namedParams: {
protocol: 'https',
hostAndPath: 'shields.io',
},
queryParams: exampleQueryParams,
staticPreview: renderWebsiteStatus({ isUp: true }),
documentation,
},
]
}
static get defaultBadgeData() {
return {
label: 'website',
}
}
async handle(
{ protocol, hostAndPath },
{
up_message: upMessage,
down_message: downMessage,
up_color: upColor,
down_color: downColor,
}
) {
let isUp
try {
const {
res: { statusCode },
} = await this._request({
url: `${protocol}://${hostAndPath}`,
options: {
method: 'HEAD',
},
})
// We consider all HTTP status codes below 310 as success.
isUp = statusCode < 310
} catch (e) {
// Catch all errors thrown by the request.
isUp = false
}
return renderWebsiteStatus({
isUp,
upMessage,
downMessage,
upColor,
downColor,
})
}
}