From 38c7212752d1e5fa1b700b222751560319ae435c Mon Sep 17 00:00:00 2001 From: Terje Karlsen Date: Fri, 6 Oct 2023 14:06:19 +0200 Subject: [PATCH] Setter opp egen api-proxy-endepunkt for kall mot Unleash --- .nais/unleash-apitoken.yaml | 17 ------ ...ken.dev.yml => unleash-api-token.dev.yaml} | 7 +-- ...n.prod.yml => unleash-api-token.prod.yaml} | 4 +- src/server/api-handlers/features.ts | 60 ++++++++++++------- src/server/server.ts | 4 +- 5 files changed, 45 insertions(+), 47 deletions(-) delete mode 100644 .nais/unleash-apitoken.yaml rename .nais/unleash/{unleash-api-token.dev.yml => unleash-api-token.dev.yaml} (64%) rename .nais/unleash/{unleash-api-token.prod.yml => unleash-api-token.prod.yaml} (88%) diff --git a/.nais/unleash-apitoken.yaml b/.nais/unleash-apitoken.yaml deleted file mode 100644 index 05596b624..000000000 --- a/.nais/unleash-apitoken.yaml +++ /dev/null @@ -1,17 +0,0 @@ -apiVersion: unleash.nais.io/v1 -kind: ApiToken -metadata: - name: nav-decoratoren - namespace: navno - labels: - team: navno -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: {{unleashEnvironment}} diff --git a/.nais/unleash/unleash-api-token.dev.yml b/.nais/unleash/unleash-api-token.dev.yaml similarity index 64% rename from .nais/unleash/unleash-api-token.dev.yml rename to .nais/unleash/unleash-api-token.dev.yaml index 4163f2f8c..6eb232e23 100644 --- a/.nais/unleash/unleash-api-token.dev.yml +++ b/.nais/unleash/unleash-api-token.dev.yaml @@ -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 diff --git a/.nais/unleash/unleash-api-token.prod.yml b/.nais/unleash/unleash-api-token.prod.yaml similarity index 88% rename from .nais/unleash/unleash-api-token.prod.yml rename to .nais/unleash/unleash-api-token.prod.yaml index 7b51a3221..edb8115eb 100644 --- a/.nais/unleash/unleash-api-token.prod.yml +++ b/.nais/unleash/unleash-api-token.prod.yaml @@ -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 diff --git a/src/server/api-handlers/features.ts b/src/server/api-handlers/features.ts index e5c1f5c53..1c2708807 100644 --- a/src/server/api-handlers/features.ts +++ b/src/server/api-handlers/features.ts @@ -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, @@ -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); }; diff --git a/src/server/server.ts b/src/server/server.ts index 6430c0e0c..f582616e4 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -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]'); @@ -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);