unify accounts_api and quota_api config options

This commit is contained in:
ansuz 2022-08-30 17:23:10 +05:30
parent af59854aca
commit e78e57c039
4 changed files with 21 additions and 11 deletions

View file

@ -174,7 +174,7 @@ var queryQuotaServer = function (Env, cb) {
var body = JSON.stringify(rawBody);
var options = {
host: Env.quota_api,
host: undefined,
path: '/api/getquota',
method: 'POST',
headers: {
@ -184,11 +184,22 @@ var queryQuotaServer = function (Env, cb) {
};
var H = Https;
if (Env.quota_api === 'localhost:3002') {
H = Http;
options.host = 'localhost';
options.port = 3002;
if (typeof(Env.accounts_api) === 'string') {
try {
let url = new URL(Env.accounts_api);
if (!['https:', 'http:'].includes(url.protocol)) { throw new Error("INVALID_PROTOCOL"); }
if (url.protocol === 'http:') { H = Http; }
if (typeof(url.port) === 'number') { options.port = url.port; }
options.host = url.host;
Env.Log.info("USING_CUSTOM_ACCOUNTS_API", {
value: Env.accounts_api,
});
} catch (err) {
Env.Log.error("INVALID_CUSTOM_QUOTA_API", {
error: err.message,
value: Env.accounts_api,
});
}
}
var req = H.request(options, function (response) {
@ -230,7 +241,7 @@ Quota.updateCachedLimits = function (Env, _cb) {
Quota.applyCustomLimits(Env);
if (!Env.allowSubscriptions && !Env.quota_api) { return void cb(); }
if (!Env.allowSubscriptions && !Env.accounts_api) { return void cb(); }
Quota.queryQuotaServer(Env, function (err, json) {
if (err) { return void cb(err); }
if (!json) { return void cb(); }

View file

@ -74,7 +74,6 @@ module.exports.create = function (config) {
NO_SANDBOX: NO_SANDBOX,
httpSafePort: httpSafePort,
accounts_api: config.accounts_api || undefined, // this simplifies integration with an accounts page
quota_api: config.quota_api || undefined, // hourly check quota
shouldUpdateNode: !isRecentVersion(),

View file

@ -203,7 +203,7 @@ RPC.create = function (Env, cb) {
var updateLimitInterval = function () {
Quota.updateCachedLimits(Env, function (e) {
// failure is expected if they have not specified a quota API endpoint
if (!Env.quota_api) { return; }
if (!Env.accounts_api) { return; }
if (e) {
Env.WARN('limitUpdate', e);
}
@ -211,7 +211,7 @@ RPC.create = function (Env, cb) {
};
Quota.applyCustomLimits(Env);
updateLimitInterval();
if (Env.quota_api) {
if (Env.accounts_api) {
Env.intervals.quotaUpdate = setInterval(updateLimitInterval, 3600*1000);
}

View file

@ -304,7 +304,7 @@ var send500 = function (res, path) {
};
app.get('/api/updatequota', function (req, res) {
if (!Env.quota_api) {
if (!Env.accounts_api) {
res.status(404);
return void send404(res);
}