вторник, 17 октября 2023 г.

Flagsmith - Setup - Feature Flagging Solution

Flagsmith makes it easy to create and manage features flags across web, mobile, and server side applications. Just wrap a section of code with a flag, and then use Flagsmith to toggle that feature on or off for different Environments, Users or user Segments 

 

*For Windows 11 with WSL enabled.

Install Docker

Download link 

Keep in mind to enable WSL when installing Docker

Setup Flagsmith Server

git clone https://github.com/Flagsmith/flagsmith

cd flagsmith

docker compose up -d

Open Flagsmith UI Console in the browser to log in for the first time: http://localhost:8000/singup

 

Install NodeJS

sudo apt install curl

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 

source ~/.bashrc

nvm ls-remote

nvm install 18.18.2 (put the version you need)

npm install flagsmith

npm install xhr2

Set your environmentID

 

Find your apikey

 

Create NodeJS small app index.js

const flagsmith = require("flagsmith"); //Add this line if you're using flagsmith via npm
global.XMLHttpRequest = require('xhr2');
//const featureFlagAPI = "https://api.flagsmith.com/api/v1/flags/";
const featureFlagAPI = "http://localhost:8000/api/v1/flags/";
const apiKey = "*******************************";

function print_state(){
if (flagsmith.default.hasFeature("awesome")) {
console.log('awesome feature is enabled')
}
else
console.log('awesome - is disabled')
}

// Start function
const start = async function() {

console.log('Calling init ...')
await flagsmith.default.init({
environmentID:"*****************",
api:"http://localhost:8000/api/v1/"
});
;
await updateFeatureFlag('awesome', false);
flagsmith.default.startListening(1000)
while (true) {
await new Promise(resolve => setTimeout(resolve, 1000));
print_state()
}
}


async function updateFeatureFlag(flagId, isEnabled) {
try {
console.log('Updated has started')
const response = await fetch(`${featureFlagAPI}${flagId}/`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
enabled: isEnabled
})
});

const data = await response.text();
console.log(data);
} catch (error) {
console.error('An error occurred:', error);
}
}

// Usage
start()


 

Testing Feature Flagging

start your app:
 
node index.js 



  you'll in the terminal running:
awesome - is disabled
awesome - is disabled
awesome - is disabled
awesome - is disabled
 
 
 
 Go to Flagsmith UI Console and enable you awesome flag



   you'll see that in the terminal it will change the message to:
awesome feature is enabled
awesome feature is enabled
awesome feature is enabled
awesome feature is enabled






Комментариев нет:

Отправить комментарий