Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Setter opp egen api-proxy-endepunkt for kall mot Unleash
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeofnorway committed Oct 8, 2023
1 parent fae3d1b commit 38c7212
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 47 deletions.
17 changes: 0 additions & 17 deletions .nais/unleash-apitoken.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ apiVersion: unleash.nais.io/v1
kind: ApiToken
metadata:
name: nav-dekoratoren
namespace: navno
namespace: personbruker
labels:
team: navno
team: personbruker
spec:
unleashInstance:
apiVersion: unleash.nais.io/v1
kind: RemoteUnleash
name: navno
secretName: nav-dekoratoren-unleash-api-token

# Specify which environment the API token should be created for.
# Can be one of: development, or production.
environment: development
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ apiVersion: unleash.nais.io/v1
kind: ApiToken
metadata:
name: nav-dekoratoren
namespace: navno
namespace: personbruker
labels:
team: navno
team: personbruker
spec:
unleashInstance:
apiVersion: unleash.nais.io/v1
Expand Down
60 changes: 39 additions & 21 deletions src/server/api-handlers/features.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
import { RequestHandler } from 'express';
import { startUnleash } from 'unleash-client';
import { initialize, Unleash } from 'unleash-client';

let unleashInstance: Unleash;
const featurePrefix = 'dekoratoren';
const expectedFeatures = ['skjermdeling', 'chatbotscript'];

type Features = { [key: string]: boolean };

const initializeUnleash = async () => {
const { UNLEASH_SERVER_API_TOKEN, UNLEASH_SERVER_API_URL } = process.env;
if (!UNLEASH_SERVER_API_TOKEN || !UNLEASH_SERVER_API_URL) {
console.error('Missing UNLEASH_SERVER_API_TOKEN or UNLEASH_SERVER_API_URL');
return false;
}

try {
unleashInstance = initialize({
url: `${UNLEASH_SERVER_API_URL}/api/`,
appName: 'nav-dekoratoren',
customHeaders: { Authorization: UNLEASH_SERVER_API_TOKEN },
});
} catch (e) {
console.error('Error initializing unleash', e);
}

return true;
};

initializeUnleash();

export const getFeaturesHandler: RequestHandler = async (req, res) => {
if (!unleashInstance) {
await initializeUnleash();
}

// Cant easily fetch feature toggles when running locally
// so just mock this.
const { UNLEASH_SERVER_API_TOKEN, UNLEASH_SERVER_API_URL } = process.env;

if (process.env.NODE_ENV === 'development') {
const features = {
skjermdeling: true,
Expand All @@ -15,24 +45,12 @@ export const getFeaturesHandler: RequestHandler = async (req, res) => {
return;
}

if (!UNLEASH_SERVER_API_TOKEN || !UNLEASH_SERVER_API_URL) {
res.status(500).send('Missing UNLEASH_SERVER_API_TOKEN or UNLEASH_SERVER_API_URL');
return;
}
console.log(unleashInstance.isEnabled('dekoratoren.skjermdeling'));

try {
const unleash = await startUnleash({
url: UNLEASH_SERVER_API_URL,
appName: 'nav-dekoratoren',
customHeaders: { Authorization: UNLEASH_SERVER_API_TOKEN },
});
const features = expectedFeatures.reduce((acc: Features, feature: string) => {
acc[feature] = unleashInstance.isEnabled(`${featurePrefix}.${feature}`);
return acc;
}, {});

return {
skjermdeling: unleash.isEnabled('dekoratoren.skjermdeling'),
chatbotscript: unleash.isEnabled('dekoratoren.chatbotscript'),
};
} catch (e) {
console.error(`Failed to fetch feature toggles from unleash - ${e}`);
res.status(500).send(`Failed to fetch feature toggles from unleash - ${e}`);
}
res.json(features);
};
4 changes: 2 additions & 2 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getCspHandler } from './api-handlers/csp';
import { getTaskAnalyticsConfigHandler } from './api-handlers/ta';
import { mockSessionHandler, refreshMockSessionHandler } from './mock/mockSession';
import { mockAuthHandler } from './mock/mockAuth';
// import { getFeaturesHandler } from './api-handlers/features';
import { getFeaturesHandler } from './api-handlers/features';

require('console-stamp')(console, '[HH:MM:ss.l]');

Expand Down Expand Up @@ -122,7 +122,7 @@ app.get(createPaths('/env'), (req, res, next) => {
// Api endpoints
app.get(createPaths('/api/meny'), getMenuHandler);
app.get(createPaths('/api/sok'), getSokHandler);
// app.get(createPaths('/api/features'), getFeaturesHandler);
app.get(createPaths('/api/features'), getFeaturesHandler);
app.get(createPaths('/api/driftsmeldinger'), getDriftsmeldingerHandler);
app.get(createPaths('/api/auth'), mockAuthHandler);
app.get(createPaths('/api/oauth2/session'), mockSessionHandler);
Expand Down

0 comments on commit 38c7212

Please sign in to comment.