From 521cf5d7c5a2528bb367ac9fc72e09199b8305fb Mon Sep 17 00:00:00 2001 From: Terje Karlsen Date: Sat, 7 Oct 2023 09:05:55 +0200 Subject: [PATCH] Attempt another unleash boot --- src/server/api-handlers/features.ts | 52 +++++++++++++++++------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/src/server/api-handlers/features.ts b/src/server/api-handlers/features.ts index 2ccfce1bc..d5de7693d 100644 --- a/src/server/api-handlers/features.ts +++ b/src/server/api-handlers/features.ts @@ -1,10 +1,34 @@ import { RequestHandler } from 'express'; -import { startUnleash } from 'unleash-client'; +import { startUnleash, initialize, Unleash } from 'unleash-client'; + +let unleashInstance: Unleash; + +const initializeUnleash = async () => { + const { UNLEASH_SERVER_API_TOKEN, UNLEASH_SERVER_API_URL } = process.env; + if (!UNLEASH_SERVER_API_TOKEN || !UNLEASH_SERVER_API_URL) { + return false; + } + + unleashInstance = initialize({ + url: UNLEASH_SERVER_API_URL, + appName: 'nav-dekoratoren', + customHeaders: { Authorization: UNLEASH_SERVER_API_TOKEN }, + }); + + console.log(unleashInstance.isSynchronized() ? 'Unleash is synchronized' : 'Unleash is not synchronized'); + + return true; +}; export const getFeaturesHandler: RequestHandler = async (req, res) => { // Cant easily fetch feature toggles when running locally // so just mock this. - const { UNLEASH_SERVER_API_TOKEN, UNLEASH_SERVER_API_URL } = process.env; + + if (!unleashInstance) { + initializeUnleash(); + } + + // res.status(500).send('Missing UNLEASH_SERVER_API_TOKEN or UNLEASH_SERVER_API_URL'); if (process.env.NODE_ENV === 'development') { const features = { @@ -15,24 +39,8 @@ 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; - } - - try { - const unleash = await startUnleash({ - url: UNLEASH_SERVER_API_URL, - appName: 'nav-dekoratoren', - customHeaders: { Authorization: UNLEASH_SERVER_API_TOKEN }, - }); - - return { - skjermdeling: true, - chatbotscript: true, - }; - } catch (e) { - console.error(`Failed to fetch feature toggles from unleash - ${e}`); - res.status(500).send(`Failed to fetch feature toggles from unleash - ${e}`); - } + return { + skjermdeling: true, + chatbotscript: true, + }; };