forked from tsai/budibase
9 changed files with 12 additions and 138 deletions
@ -1,35 +0,0 @@ |
|||
const redis = require("./redis") |
|||
const env = require("../../environment") |
|||
const accounts = require("../../cloud/accounts") |
|||
|
|||
const EXPIRY_SECONDS = 3600 |
|||
|
|||
const populateLicense = async tenantId => { |
|||
if (env.SELF_HOSTED) { |
|||
// get license key
|
|||
} else { |
|||
return accounts.getLicense(tenantId) |
|||
} |
|||
} |
|||
|
|||
exports.getLicense = async (tenantId, opts = { populateLicense: null }) => { |
|||
// try cache
|
|||
const client = await redis.getClient() |
|||
let license = await client.get(tenantId) |
|||
if (!license) { |
|||
const populate = opts.populateLicense |
|||
? opts.populateLicense |
|||
: populateLicense |
|||
license = await populate(tenantId) |
|||
if (license) { |
|||
client.store(tenantId, license, EXPIRY_SECONDS) |
|||
} |
|||
} |
|||
|
|||
return license |
|||
} |
|||
|
|||
exports.invalidate = async tenantId => { |
|||
const client = await redis.getClient() |
|||
await client.delete(tenantId) |
|||
} |
|||
@ -1,25 +0,0 @@ |
|||
const Redis = require("../../redis") |
|||
const utils = require("../../redis/utils") |
|||
|
|||
let client |
|||
|
|||
const init = async () => { |
|||
client = await new Redis(utils.Databases.LICENSES).init() |
|||
} |
|||
|
|||
const shutdown = async () => { |
|||
if (client) { |
|||
await client.finish() |
|||
} |
|||
} |
|||
|
|||
process.on("exit", async () => { |
|||
await shutdown() |
|||
}) |
|||
|
|||
exports.getClient = async () => { |
|||
if (!client) { |
|||
await init() |
|||
} |
|||
return client |
|||
} |
|||
@ -1,9 +0,0 @@ |
|||
const middleware = require("./middleware") |
|||
const cache = require("./cache") |
|||
const usage = require("./usage") |
|||
|
|||
module.exports = { |
|||
middleware, |
|||
cache, |
|||
usage, |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
const cache = require("../cache") |
|||
|
|||
const buildLicensingMiddleware = opts => { |
|||
return async (ctx, next) => { |
|||
if (ctx.user) { |
|||
const tenantId = ctx.user.tenantId |
|||
const license = await cache.getLicense(tenantId, opts) |
|||
if (license) { |
|||
ctx.user.license = license |
|||
} |
|||
} |
|||
|
|||
return next() |
|||
} |
|||
} |
|||
|
|||
module.exports = buildLicensingMiddleware |
|||
@ -1,41 +0,0 @@ |
|||
const { getTenantId } = require("../../context") |
|||
const cache = require("../cache") |
|||
const env = require("../../environment") |
|||
const utils = require("../../utils") |
|||
const { UsageLimitError } = require("../../errors") |
|||
|
|||
const UsageType = { |
|||
MONTHLY: "monthly", |
|||
STATIC: "static", |
|||
} |
|||
|
|||
const StaticUsageLimits = { |
|||
MAX_DEVELOPERS: "maxDevelopers", |
|||
QUERY_TIMEOUT_SECONDS: "queryTimeoutSeconds", |
|||
} |
|||
|
|||
// eslint-disable-next-line no-unused-vars
|
|||
const MonthlyUsageLimits = { |
|||
MAX_QUERY_COUNT: "maxQueryCount", |
|||
} |
|||
|
|||
exports.checkMaxDevelopers = async () => { |
|||
const developerCount = await utils.getBuildersCount() |
|||
await checkUsageLimit( |
|||
developerCount, |
|||
UsageType.STATIC, |
|||
StaticUsageLimits.MAX_DEVELOPERS |
|||
) |
|||
} |
|||
|
|||
const checkUsageLimit = async (currentUsage, usageType, usageLimit) => { |
|||
const tenantId = getTenantId() |
|||
const license = await cache.getLicense(tenantId) |
|||
const limit = license.usageLimits[usageType][usageLimit] |
|||
if (currentUsage >= limit.value) { |
|||
throw new UsageLimitError( |
|||
`Licensed ${limit.name} has been exceeded. To upgrade, visit ${env.ACCOUNT_PORTAL_URL}/portal/plans`, |
|||
limit.name |
|||
) |
|||
} |
|||
} |
|||
Loading…
Reference in new issue