Skip to content

Commit

Permalink
feat: introduce feature toggles (#1121)
Browse files Browse the repository at this point in the history
feat: introduce feature toggles
  • Loading branch information
fcastill authored Jun 20, 2019
2 parents 239811a + af67629 commit c951db5
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@
# FLOGO_LIB_VERSION=latest
## only if FLOGO_LIB_VERSION is not set:
# FLOGO_WEB_LIB_VERSION=latest

### Feature toggles
### Enable/Disable features
### Syntax: FLOGO_FEATURES_<name of the flag

## Enable a flag
# FLOGO_FEATURES_STREAMS=1

### Disable a flag
### Give an empty value to disable the flag
### NOTE these values are always considered as strings and providing a value different than the empty string will be
### considered as a truthy value. This means that values like false, 'false' or 0 won't work.
# FLOGO_FEATURES_FLAG_X=''
2 changes: 2 additions & 0 deletions apps/client/src/flogo/flogo.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
FLOGO_VERSION,
HOSTNAME,
initializer,
FEATURE_TOGGLES_APP_INITIALIZER,
} from '@flogo-web/lib-client/core';
import { LanguageService, createTranslateLoader } from '@flogo-web/lib-client/language';
import { ContribInstallerService } from '@flogo-web/lib-client/contrib-installer';
Expand Down Expand Up @@ -76,6 +77,7 @@ import { resourcePlugins } from '../plugins';
deps: [TranslateService],
multi: true,
},
FEATURE_TOGGLES_APP_INITIALIZER,
{ provide: APP_BASE_HREF, useValue: '/' },
appRoutingProviders,
ContribInstallerService,
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/api/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ export function createRouter(container: Container): Router {
mountServices(router);
mountEngine(router);
mountTestRunner(router);

router.get('/_/features', context => {
context.body = config.features;
});

return router;
}
3 changes: 3 additions & 0 deletions apps/server/src/config/app-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from 'path';

import './load-env';
import { parseFeatureToggles } from './parse-features-toggles';
const features = parseFeatureToggles(process.env);

const rootPath = path.resolve(__dirname, '..');
const PUBLIC_DIR =
Expand Down Expand Up @@ -41,6 +43,7 @@ const config = {
logsPath: path.join(LOCAL_DIR, 'logs'),
logLevel,
localPath: LOCAL_DIR,
features,
defaultAppJsonPath: path.join(rootPath, 'config', 'sample-app.json'),
defaultFlogoDescriptorPath:
process.env.FLOGO_WEB_DEFAULT_DESCRIPTOR ||
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { config } from './app-config';

export { isFeatureEnabled } from './is-feature-enabled';
5 changes: 5 additions & 0 deletions apps/server/src/config/is-feature-enabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { config } from './app-config';

export function isFeatureEnabled(featureName: string) {
return config.features && config.features[featureName];
}
13 changes: 13 additions & 0 deletions apps/server/src/config/parse-features-toggles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const FEATURES_PREFIX = 'FLOGO_FEATURES_';
const FEATURES_PREFIX_LENGTH = FEATURES_PREFIX.length;

function featuresReducer(features, [envKey, envValue]: [string, any]) {
if (envKey.startsWith(FEATURES_PREFIX)) {
features[envKey.slice(FEATURES_PREFIX_LENGTH)] = envValue;
}
return features;
}

export function parseFeatureToggles(fromEnv: NodeJS.ProcessEnv) {
return Object.entries(fromEnv).reduce(featuresReducer, {});
}
36 changes: 36 additions & 0 deletions libs/lib-client/core/src/services/feature-toggles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Injectable, APP_INITIALIZER, FactoryProvider } from '@angular/core';
import { tap, catchError } from 'rxjs/operators';
import { EMPTY } from 'rxjs';
import { RestApiService } from './restapi';

@Injectable({ providedIn: 'root' })
export class FeatureToggleService {
features: { [featureName: string]: any };

isFeatureEnabled(featureName: string) {
return this.features && !!this.features[featureName];
}
}

// needed to be exported because of AoT
export function featureToggleServiceFactory(
apiService: RestApiService,
featureService: FeatureToggleService
) {
return () => {
return apiService
.get('_/features')
.pipe(
tap(features => (featureService.features = features)),
catchError(() => EMPTY)
)
.toPromise();
};
}

export const FEATURE_TOGGLES_APP_INITIALIZER: FactoryProvider = {
provide: APP_INITIALIZER,
useFactory: featureToggleServiceFactory,
deps: [RestApiService, FeatureToggleService],
multi: true,
};
2 changes: 2 additions & 0 deletions libs/lib-client/core/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export * from './service-url-config.model';
export * from './app-resource.service';
export * from './log.service';
export * from './activated-resource-route';

export * from './feature-toggles';

0 comments on commit c951db5

Please sign in to comment.