Skip to content

Commit

Permalink
Config: Improve consistency between plugin and server mode (#477)
Browse files Browse the repository at this point in the history
* Settings: Add rendering settings in standalone mode

* refactor configuration code

* clean up comments

* clean up

* fix + clean up EOF

* apply review feedback

* update populateRenderingConfigFromEnv
  • Loading branch information
AgnesToulet committed Nov 9, 2023
1 parent ed089d4 commit c13be0d
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 349 deletions.
95 changes: 6 additions & 89 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import * as fs from 'fs';
import { Browser, computeExecutablePath } from '@puppeteer/browsers';
import { RenderGRPCPluginV2 } from './plugin/v2/grpc_plugin';
import { HttpServer } from './service/http-server';
import { populateServiceConfigFromEnv, ServiceConfig, defaultServiceConfig } from './service/config';
import { populatePluginConfigFromEnv, PluginConfig, defaultPluginConfig } from './plugin/v2/config';
import { ConsoleLogger, PluginLogger } from './logger';
import * as minimist from 'minimist';
import { defaultPluginConfig, defaultServiceConfig, readJSONFileSync, PluginConfig, ServiceConfig } from './config';
import { serve } from './node-plugin';
import { createSanitizer } from './sanitizer/Sanitizer';

Expand Down Expand Up @@ -86,92 +87,8 @@ main().catch((err) => {
process.exit(1);
});

function populatePluginConfigFromEnv(config: PluginConfig, env: NodeJS.ProcessEnv) {
// Plugin env variables that needs to be initiated early
if (env['GF_PLUGIN_GRPC_HOST']) {
config.plugin.grpc.host = env['GF_PLUGIN_GRPC_HOST'] as string;
}

if (env['GF_PLUGIN_GRPC_PORT']) {
config.plugin.grpc.port = parseInt(env['GF_PLUGIN_GRPC_PORT'] as string, 10);
}

if (env['GF_PLUGIN_RENDERING_CHROME_BIN']) {
config.rendering.chromeBin = env['GF_PLUGIN_RENDERING_CHROME_BIN'];
}
}

function populateServiceConfigFromEnv(config: ServiceConfig, env: NodeJS.ProcessEnv) {
if (env['BROWSER_TZ']) {
config.rendering.timezone = env['BROWSER_TZ'];
} else if (env['TZ']) {
config.rendering.timezone = env['TZ'];
}

if (env['HTTP_HOST']) {
config.service.host = env['HTTP_HOST'];
}

if (env['HTTP_PORT']) {
config.service.port = parseInt(env['HTTP_PORT'] as string, 10);
}

if (env['AUTH_TOKEN']) {
const authToken = env['AUTH_TOKEN'] as string;
config.service.security.authToken = authToken.includes(' ') ? authToken.split(' ') : authToken;
}

if (env['LOG_LEVEL']) {
config.service.logging.level = env['LOG_LEVEL'] as string;
}

if (env['IGNORE_HTTPS_ERRORS']) {
config.rendering.ignoresHttpsErrors = env['IGNORE_HTTPS_ERRORS'] === 'true';
}

if (env['CHROME_BIN']) {
config.rendering.chromeBin = env['CHROME_BIN'];
}
function readJSONFileSync(filePath: string) {
const rawdata = fs.readFileSync(filePath, 'utf8');
return JSON.parse(rawdata);
};

if (env['ENABLE_METRICS']) {
config.service.metrics.enabled = env['ENABLE_METRICS'] === 'true';
}

if (env['RENDERING_MODE']) {
config.rendering.mode = env['RENDERING_MODE'] as string;
}

if (env['RENDERING_CLUSTERING_MODE']) {
config.rendering.clustering.mode = env['RENDERING_CLUSTERING_MODE'] as string;
}

if (env['RENDERING_CLUSTERING_MAX_CONCURRENCY']) {
config.rendering.clustering.maxConcurrency = parseInt(env['RENDERING_CLUSTERING_MAX_CONCURRENCY'] as string, 10);
}

if (env['RENDERING_CLUSTERING_TIMEOUT']) {
config.rendering.clustering.timeout = parseInt(env['RENDERING_CLUSTERING_TIMEOUT'] as string, 10);
}

if (env['RENDERING_VERBOSE_LOGGING']) {
config.rendering.verboseLogging = env['RENDERING_VERBOSE_LOGGING'] === 'true';
}

if (env['RENDERING_DUMPIO']) {
config.rendering.dumpio = env['RENDERING_DUMPIO'] === 'true';
}

if (env['RENDERING_VIEWPORT_PAGE_ZOOM_LEVEL']) {
config.rendering.pageZoomLevel = parseFloat(env['RENDERING_VIEWPORT_PAGE_ZOOM_LEVEL'] as string);
}

if (env['RENDERING_ARGS']) {
const args = env['RENDERING_ARGS'] as string;
if (args.length > 0) {
const argsList = args.split(',');
if (argsList.length > 0) {
config.rendering.args = argsList;
}
}
}
}
2 changes: 1 addition & 1 deletion src/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fs from 'fs';
import * as promClient from 'prom-client';
import * as Jimp from 'jimp';
import { Logger } from '../logger';
import { RenderingConfig } from '../config';
import { RenderingConfig } from '../config/rendering';
import { ImageRenderOptions, RenderOptions } from '../types';

export interface Metrics {
Expand Down
2 changes: 1 addition & 1 deletion src/browser/clustered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Cluster as PuppeteerCluster } from 'puppeteer-cluster';
import { ImageRenderOptions, RenderOptions } from '../types';
import { Browser, RenderResponse, RenderCSVResponse, Metrics } from './browser';
import { Logger } from '../logger';
import { RenderingConfig, ClusteringConfig } from '../config';
import { RenderingConfig, ClusteringConfig } from '../config/rendering';

enum RenderType {
CSV = 'csv',
Expand Down
2 changes: 1 addition & 1 deletion src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderingConfig } from '../config';
import { RenderingConfig } from '../config/rendering';
import { Logger } from '../logger';
import { Browser, Metrics } from './browser';
import { ClusteredBrowser } from './clustered';
Expand Down
2 changes: 1 addition & 1 deletion src/browser/reusable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as puppeteer from 'puppeteer';
import { ImageRenderOptions, RenderOptions } from '../types';
import { Browser, RenderResponse, RenderCSVResponse, Metrics } from './browser';
import { Logger } from '../logger';
import { RenderingConfig } from '../config';
import { RenderingConfig } from '../config/rendering';

export class ReusableBrowser extends Browser {
browser: puppeteer.Browser;
Expand Down
159 changes: 0 additions & 159 deletions src/config.ts

This file was deleted.

0 comments on commit c13be0d

Please sign in to comment.