Files
shields/server.js
Paul Melnikow 54a36e9474 Refactor cache-header handling and config, create NonMemoryCachingBaseService, rewrite [flip] (#2360)
There's a lot going on in this PR, though it's all interdependent, so the only way I can see to break it up into smaller pieces would be serially.

1. I completely refactored the functions for managing cache headers. These have been added to `services/cache-headers.js`, and in some ways set the stage for the rest of this PR.

    - There are ample higher-level test of the functionality via `request-handler`. Refactoring these tests was deferred. Cache headers were previously dealt with in three places:
        - `request-handler.js`, for the dynamic badges. This function now calls `setCacheHeaders`.
        - `base-static.js`, for the static badges. This method now calls the wordy `serverHasBeenUpSinceResourceCached` and `setCacheHeadersForStaticResource`.
        - The bitFlip badge in `server.js`. 👈 This is what set all this in motion. This badge has been refactored to a new-style service based on a new `NoncachingBaseService` which does not use the Shields in-memory cache that the dynamic badges user.
    - I'm open to clearer names for `NoncachingBaseService`, which is kind of terrible. Absent alternatives, I wrote a short essay of clarification in the docstring. 😝 

2. In the process of writing `NoncachingBaseService`, I discovered it takes several lines of code to instantiate and invoke a service. These would be duplicated in three or four places in production code, and in lots and lots of tests. I kept the line that goes from regex to namedParams (for reasons) and moved the rest into a static method called `invoke()`, which instantiates and invokes the service. This _replaced_ the instance method `invokeHandler`.
    - I gently reworked the unit tests to use `invoke` instead of `invokeHandler`– generally for the better.
    - I made a small change to `BaseStatic`. Now it invokes `handle()` async as the dynamic badges do. This way it could use `BaseService.invoke()`.

3. There was logic in `request-handler` for processing environment variables, validating them, and setting defaults. This could have been lifted whole-hog to `services/cache-headers.js`, though I didn't do that. Instead I moved it to `server-config.js`. Ideally `server-config` is the only module that should access `process.env`. This puts the defaults and config validation in one place, decouples the config schema from the entire rest of the application, and significantly simplifies our ability to test different configs, particularly on small units of code. (We were doing this well enough before in `request-handler.spec`, though it required mutating the environment, which was kludgy.) Some of the `request-handler` tests could be rewritten at a higher level, with lower-level data-driven tests directly against `cache-headers`.
2018-12-01 13:57:34 -05:00

259 lines
7.0 KiB
JavaScript

'use strict'
const { DOMParser } = require('xmldom')
const jp = require('jsonpath')
const path = require('path')
const xpath = require('xpath')
const yaml = require('js-yaml')
const Raven = require('raven')
const serverSecrets = require('./lib/server-secrets')
Raven.config(process.env.SENTRY_DSN || serverSecrets.sentry_dsn).install()
Raven.disableConsoleAlerts()
const { loadServiceClasses } = require('./services')
const { checkErrorResponse } = require('./lib/error-helper')
const analytics = require('./lib/analytics')
const config = require('./lib/server-config')
const GithubConstellation = require('./services/github/github-constellation')
const PrometheusMetrics = require('./lib/sys/prometheus-metrics')
const sysMonitor = require('./lib/sys/monitor')
const log = require('./lib/log')
const { staticBadgeUrl } = require('./lib/make-badge-url')
const makeBadge = require('./gh-badges/lib/make-badge')
const suggest = require('./lib/suggest')
const {
makeBadgeData: getBadgeData,
setBadgeColor,
} = require('./lib/badge-data')
const {
handleRequest: cache,
clearRequestCache,
} = require('./lib/request-handler')
const { clearRegularUpdateCache } = require('./lib/regular-update')
const { makeSend } = require('./lib/result-sender')
const camp = require('camp').start({
documentRoot: path.join(__dirname, 'public'),
port: config.bind.port,
hostname: config.bind.address,
secure: config.ssl.isSecure,
cert: config.ssl.cert,
key: config.ssl.key,
})
const githubConstellation = new GithubConstellation({
persistence: config.persistence,
service: config.services.github,
})
const metrics = new PrometheusMetrics(config.metrics.prometheus)
const { apiProvider: githubApiProvider } = githubConstellation
function reset() {
clearRequestCache()
clearRegularUpdateCache()
}
async function stop() {
await githubConstellation.stop()
analytics.cancelAutosaving()
return new Promise(resolve => {
camp.close(resolve)
})
}
module.exports = {
camp,
reset,
stop,
}
log(`Server is starting up: ${config.baseUri}`)
analytics.load()
analytics.scheduleAutosaving()
analytics.setRoutes(camp)
if (serverSecrets && serverSecrets.shieldsSecret) {
sysMonitor.setRoutes(camp)
}
githubConstellation.initialize(camp)
metrics.initialize(camp)
suggest.setRoutes(config.cors.allowedOrigin, githubApiProvider, camp)
camp.notfound(/\.(svg|png|gif|jpg|json)/, (query, match, end, request) => {
const format = match[1]
const badgeData = getBadgeData('404', query)
badgeData.text[1] = 'badge not found'
badgeData.colorscheme = 'red'
// Add format to badge data.
badgeData.format = format
const svg = makeBadge(badgeData)
makeSend(format, request.res, end)(svg)
})
camp.notfound(/.*/, (query, match, end, request) => {
end(null, { template: '404.html' })
})
// Vendors.
loadServiceClasses().forEach(serviceClass =>
serviceClass.register(
{ camp, handleRequest: cache, githubApiProvider },
{
handleInternalErrors: config.handleInternalErrors,
cacheHeaders: config.cacheHeaders,
profiling: config.profiling,
}
)
)
// User defined sources - JSON response
camp.route(
/^\/badge\/dynamic\/(json|xml|yaml)\.(svg|png|gif|jpg|json)$/,
cache(config.cacheHeaders, {
queryParams: ['uri', 'url', 'query', 'prefix', 'suffix'],
handler: function(query, match, sendBadge, request) {
const type = match[1]
const format = match[2]
const prefix = query.prefix || ''
const suffix = query.suffix || ''
const pathExpression = query.query
let requestOptions = {}
const badgeData = getBadgeData('custom badge', query)
if ((!query.uri && !query.url) || !query.query) {
setBadgeColor(badgeData, 'red')
badgeData.text[1] = !query.query
? 'no query specified'
: 'no url specified'
sendBadge(format, badgeData)
return
}
let url
try {
url = encodeURI(decodeURIComponent(query.url || query.uri))
} catch (e) {
setBadgeColor(badgeData, 'red')
badgeData.text[1] = 'malformed url'
sendBadge(format, badgeData)
return
}
switch (type) {
case 'json':
requestOptions = {
headers: {
Accept: 'application/json',
},
json: true,
}
break
case 'xml':
requestOptions = {
headers: {
Accept: 'application/xml, text/xml',
},
}
break
case 'yaml':
requestOptions = {
headers: {
Accept:
'text/x-yaml, text/yaml, application/x-yaml, application/yaml, text/plain',
},
}
break
}
request(url, requestOptions, (err, res, data) => {
try {
if (
checkErrorResponse(badgeData, err, res, {
404: 'resource not found',
})
) {
return
}
badgeData.colorscheme = 'brightgreen'
let innerText = []
switch (type) {
case 'json':
data = typeof data === 'object' ? data : JSON.parse(data)
data = jp.query(data, pathExpression)
if (!data.length) {
throw Error('no result')
}
innerText = data
break
case 'xml':
data = new DOMParser().parseFromString(data)
data = xpath.select(pathExpression, data)
if (!data.length) {
throw Error('no result')
}
data.forEach((i, v) => {
innerText.push(
pathExpression.indexOf('@') + 1 ? i.value : i.firstChild.data
)
})
break
case 'yaml':
data = yaml.safeLoad(data)
data = jp.query(data, pathExpression)
if (!data.length) {
throw Error('no result')
}
innerText = data
break
}
badgeData.text[1] =
(prefix || '') + innerText.join(', ') + (suffix || '')
} catch (e) {
setBadgeColor(badgeData, 'lightgrey')
badgeData.text[1] = e.message
} finally {
sendBadge(format, badgeData)
}
})
},
})
)
// Any badge, old version. This route must be registered last.
camp.route(/^\/([^/]+)\/(.+).png$/, (queryParams, match, end, ask) => {
const [, label, message] = match
const { color } = queryParams
const redirectUrl = staticBadgeUrl({
label,
message,
color,
format: 'png',
})
ask.res.statusCode = 301
ask.res.setHeader('Location', redirectUrl)
// The redirect is permanent.
const cacheDuration = (365 * 24 * 3600) | 0 // 1 year
ask.res.setHeader('Cache-Control', `max-age=${cacheDuration}`)
ask.res.end()
})
if (config.redirectUri) {
camp.route(/^\/$/, (data, match, end, ask) => {
ask.res.statusCode = 302
ask.res.setHeader('Location', config.redirectUri)
ask.res.end()
})
}