
In Previous Part-1 we configured the destination through which we will access feature flags. Now we will develop an application. We could use a Simple NodeJs application to access BTP instance, but again thought of developing using sap cloud sdk as described by SAP.
How to develop a simple nodeJS application, you can follow one of my existing blog posts or any other out there in internet.
Run NodeJs in SAP BTP and Locally, Part - 1
(** Note: The blog was written earlier, you may have to adjust the npm packages and little modifications in the code.)
Let’s Start with some command to install packages –
Setting Up the Project
1. Install NestJS: (documentation on NestJS - link )
npm i -g @nestjs/cli
2. Scaffold an Application:
nest new featureflagsapcloudsdk
cd featureflagsapcloudsdk
3. Install OpenAPI Generator:
npm install -D -cloud-sdk/openapi-generator
4. Download and Set Up FeatureFlagsAPI.yaml:
5. Create a file options-per-service.json and store in the same folder ‘resources/service-specs’.
{
"resources/service-specs/FeatureFlagsAPI.yaml": {}
}
6. Install SAP Cloud SDK OpenAPI Package:
npm i -cloud-sdk/openapi
7. Generate Typed Client:
npx openapi-generator --input resources/service-specs --outputDir src/generated
This will generate the below like folders and files –
All the calls or routing will be navigating from app.controller.ts file. These files will be generated by-default.
8. Implementing the Application
Modify app.controller.ts and app.service.ts to include methods for accessing the feature flag.
These applications are based on typescript which generated by sap cloud sdk, but for the demo purposes we are avoiding the types. Mostly we are providing <any> as assigning and return type.
app.controller.ts:
app.service.ts:
Add “import { EvaluateV2Api } from './generated/FeatureFlagsAPI';”
9. Deploying the Application
Now let’s create a manifest file to deploy and bind with other resources –
Destination Featureflagdest already
** I am not very much worried about the authentication here now. So only using the Destination service.
manifest.yaml will look like below –
Before deploying use the below Command, which is must each time you make any changes to any files.
nest build
Now deploy manifest file to BTP Cloud foundry using Command
cf push
10. Run the Application
In BTP, Application is in running state and started.
Execute the module with the path /feature-flag
Here is the output when flag is set to true
When the flag is false
Code Snippet –
app.controller.ts
import { Controller, Get, HttpException } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) { }
@Get('/feature-flag')
getFeatureFlag(): any {
return this.appService.getFlag()
.then(response => {
return response;
})
.catch(error => {
throw new HttpException(
`Failed to get Feature Flags - ${error.message}`,
500
)
})
}
}
app.service.ts
import { Injectable, Get ,HttpException } from '@nestjs/common';
import { EvaluateV2Api } from './generated/FeatureFlagsAPI';
@Injectable()
export class AppService {
async getFlag(): Promise<any> {
return EvaluateV2Api
.getV2EvaluateById('<feature_flag_name>', { identifier: '<tenant_id>'})
.setBasePath('https://feature-flags.cfapps.<region>.hana.ondemand.com/api')
.execute({ destinationName: '<destination_name>' });
}
}
manifest.yaml
applications:
- name: featureflagsdk
path: .
buildpacks:
- nodejs_buildpack
memory: 256M
command: npm run start:prod
random-route: true
services:
- featureflagdest
By following this posts, you will get the guidance of how to access the BTP Feature flags and use in your different scenarios.
If you have any questions or faced challenges, feel free to drop a comment below.
References –
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 | |
4 |