feat: Introduce getLicense graphQL query

This commit is contained in:
Faruk AYDIN
2023-02-14 22:01:34 +01:00
parent 1847ad5622
commit 1dfb22d02e
7 changed files with 67 additions and 4 deletions

View File

@@ -50,6 +50,7 @@
"knex": "^2.4.0",
"lodash.get": "^4.4.2",
"luxon": "2.5.2",
"memory-cache": "^0.2.0",
"morgan": "^1.10.0",
"multer": "1.4.5-lts.1",
"nodemailer": "6.7.0",
@@ -103,6 +104,7 @@
"@types/http-errors": "^1.8.1",
"@types/jsonwebtoken": "^8.5.8",
"@types/lodash.get": "^4.4.6",
"@types/memory-cache": "^0.2.2",
"@types/morgan": "^1.9.3",
"@types/multer": "1.4.7",
"@types/node": "^16.10.2",

View File

@@ -32,6 +32,7 @@ type AppConfig = {
bullMQDashboardPassword: string;
telemetryEnabled: boolean;
requestBodySizeLimit: string;
licenseKey: string;
};
const host = process.env.HOST || 'localhost';
@@ -40,7 +41,7 @@ const port = process.env.PORT || '3000';
const serveWebAppSeparately =
process.env.SERVE_WEB_APP_SEPARATELY === 'true' ? true : false;
let apiUrl = (new URL(`${protocol}://${host}:${port}`)).toString();
let apiUrl = new URL(`${protocol}://${host}:${port}`).toString();
apiUrl = apiUrl.substring(0, apiUrl.length - 1);
// use apiUrl by default, which has less priority over the following cases
@@ -48,14 +49,14 @@ let webAppUrl = apiUrl;
if (process.env.WEB_APP_URL) {
// use env. var. if provided
webAppUrl = (new URL(process.env.WEB_APP_URL)).toString();
webAppUrl = new URL(process.env.WEB_APP_URL).toString();
webAppUrl = webAppUrl.substring(0, webAppUrl.length - 1);
} else if (serveWebAppSeparately) {
// no env. var. and serving separately, sign of development
webAppUrl = 'http://localhost:3001'
webAppUrl = 'http://localhost:3001';
}
let webhookUrl = (new URL(process.env.WEBHOOK_URL || apiUrl)).toString();
let webhookUrl = new URL(process.env.WEBHOOK_URL || apiUrl).toString();
webhookUrl = webhookUrl.substring(0, webhookUrl.length - 1);
const appEnv = process.env.APP_ENV || 'development';
@@ -91,6 +92,7 @@ const appConfig: AppConfig = {
webhookUrl,
telemetryEnabled: process.env.TELEMETRY_ENABLED === 'false' ? false : true,
requestBodySizeLimit: '1mb',
licenseKey: process.env.LICENSE_KEY,
};
if (!appConfig.encryptionKey) {

View File

@@ -0,0 +1,11 @@
import checkLicense from '../../helpers/checkLicense.ee';
const getLicense = async () => {
const license = await checkLicense();
return {
type: license ? 'ee' : 'ce',
};
};
export default getLicense;

View File

@@ -10,6 +10,7 @@ import getExecutions from './queries/get-executions';
import getExecutionSteps from './queries/get-execution-steps';
import getDynamicData from './queries/get-dynamic-data';
import getCurrentUser from './queries/get-current-user';
import getLicense from './queries/get-license.ee';
import healthcheck from './queries/healthcheck';
const queryResolvers = {
@@ -25,6 +26,7 @@ const queryResolvers = {
getExecutionSteps,
getDynamicData,
getCurrentUser,
getLicense,
healthcheck,
};

View File

@@ -29,6 +29,7 @@ type Query {
parameters: JSONObject
): JSONObject
getCurrentUser: User
getLicense: GetLicense
healthcheck: AppHealth
}
@@ -453,6 +454,10 @@ type AppHealth {
version: String
}
type GetLicense {
type: String
}
schema {
query: Query
mutation: Mutation

View File

@@ -0,0 +1,31 @@
import axios from 'axios';
import appConfig from '../config/app';
import memoryCache from 'memory-cache';
const CACHE_DURATION = 1000 * 60 * 60 * 24; // 24 hours in milliseconds
const checkLicense = async () => {
const licenseKey = appConfig.licenseKey;
if (!licenseKey) {
return false;
}
const url = 'https://license.automatisch.io/api/v1/licenses/verify';
const cachedResponse = memoryCache.get(url);
if (cachedResponse) {
return cachedResponse;
} else {
try {
const { data } = await axios.post(url, { licenseKey });
memoryCache.put(url, data.verified, CACHE_DURATION);
return data.verified;
} catch (error) {
return false;
}
}
};
export default checkLicense;