Skip to content

Commit

Permalink
Refactor [github] initialization (#1861)
Browse files Browse the repository at this point in the history
This creates a new convenience class which consolidates all the Github initialization. It supports dependency injection and facilitates refactoring the persistence along the lines of #1205.

Also ref #1848
  • Loading branch information
paulmelnikow authored Aug 9, 2018
1 parent 7a664ca commit c11d97a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
8 changes: 1 addition & 7 deletions lib/github-auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const path = require('path')
const crypto = require('crypto')
const log = require('./log')
const queryString = require('query-string')
Expand All @@ -14,12 +13,7 @@ const mapKeys = require('lodash.mapkeys')
// with a JsonSave object.
let githubUserTokens = { data: [] }

function scheduleAutosaving(config) {
const { dir: persistenceDir } = config
const githubUserTokensFile = path.resolve(
persistenceDir,
'github-user-tokens.json'
)
function scheduleAutosaving(githubUserTokensFile) {
autosave(githubUserTokensFile, { data: [] })
.then(save => {
githubUserTokens = save
Expand Down
29 changes: 9 additions & 20 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const { isDeprecated, getDeprecatedBadge } = require('./lib/deprecation-helpers'
const { checkErrorResponse } = require('./lib/error-helper');
const analytics = require('./lib/analytics');
const config = require('./lib/server-config');
const githubAuth = require('./lib/github-auth');
const GithubApiProvider = require('./services/github/github-api-provider');
const { setRoutes: setGithubAdminRoutes } = require('./services/github/auth/admin');
const GithubConstellation = require('./services/github/github-constellation');
const sysMonitor = require('./lib/sys/monitor');
const log = require('./lib/log');
const { makeMakeBadgeFn } = require('./lib/make-badge');
Expand Down Expand Up @@ -113,7 +111,6 @@ const {
} = require('./lib/pypi-helpers.js');

const serverStartTime = new Date((new Date()).toGMTString());
const githubApiProvider = new GithubApiProvider({ baseUrl: config.services.github.baseUri });

const camp = require('camp').start({
documentRoot: path.join(__dirname, 'public'),
Expand All @@ -124,17 +121,19 @@ const camp = require('camp').start({
key: config.ssl.key,
});

const githubConstellation = new GithubConstellation({
persistence: config.persistence,
service: config.services.github,
});
const { apiProvider: githubApiProvider } = githubConstellation;

function reset() {
clearRequestCache();
clearRegularUpdateCache();
}

function stop(callback) {
githubAuth.cancelAutosaving();
if (githubDebugInterval) {
clearInterval(githubDebugInterval);
githubDebugInterval = null;
}
githubConstellation.stop();
analytics.cancelAutosaving();
camp.close(callback);
}
Expand All @@ -161,21 +160,11 @@ analytics.load();
analytics.scheduleAutosaving();
analytics.setRoutes(camp);

setGithubAdminRoutes(camp);
githubAuth.scheduleAutosaving({ dir: config.persistence.dir });
if (serverSecrets && serverSecrets.gh_client_id) {
githubAuth.setRoutes(camp);
}
if (serverSecrets && serverSecrets.shieldsSecret) {
sysMonitor.setRoutes(camp);
}

let githubDebugInterval;
if (config.services.github.debug.enabled) {
githubDebugInterval = setInterval(() => {
log(githubAuth.serializeDebugInfo());
}, 1000 * config.services.github.debug.intervalSeconds);
}
githubConstellation.initialize(camp);

suggest.setRoutes(config.cors.allowedOrigin, githubApiProvider, camp);

Expand Down
55 changes: 55 additions & 0 deletions services/github/github-constellation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const path = require('path')
const githubAuth = require('../../lib/github-auth')
const serverSecrets = require('../../lib/server-secrets')
const log = require('../../lib/log')
const GithubApiProvider = require('./github-api-provider')
const { setRoutes: setAdminRoutes } = require('./auth/admin')

// Convenience class with all the stuff related to the Github API and its
// authorization tokens, to simplify server initialization.
class GithubConstellation {
constructor(config) {
this._debugEnabled = config.service.debug.enabled
this._debugIntervalSeconds = config.service.debug.intervalSeconds
this._userTokensPath = path.resolve(
config.persistence.dir,
'github-user-tokens.json'
)

const baseUrl = process.env.GITHUB_URL || 'https://api.github.com'
this.apiProvider = new GithubApiProvider({ baseUrl })
}

scheduleDebugLogging() {
if (this._debugEnabled) {
this.debugInterval = setInterval(() => {
log(githubAuth.serializeDebugInfo())
}, 1000 * this._debugIntervalSeconds)
}
}

async initialize(server) {
this.scheduleDebugLogging()

githubAuth.scheduleAutosaving(this._userTokensPath)
// TODO Catch errors and send them to Sentry.

setAdminRoutes(server)

if (serverSecrets && serverSecrets.gh_client_id) {
githubAuth.setRoutes(server)
}
}

stop() {
githubAuth.cancelAutosaving()
if (this.debugInterval) {
clearInterval(this.debugInterval)
this.debugInterval = undefined
}
}
}

module.exports = GithubConstellation

0 comments on commit c11d97a

Please sign in to comment.