diff --git a/mock-server/README.md b/mock-server/README.md index 29ef9b12..6ab62d25 100644 --- a/mock-server/README.md +++ b/mock-server/README.md @@ -2,4 +2,17 @@ This is a simple mock server for use in client tests. It just serves a bunch of static files from `sdk-test-data` at the appropriate locations. -See `prepare.sh` for the list of files served. +The mock server may serve multiple "environments" at the same time at different prefixes, so the base URL format is `http://localhost:8378/{env_name}/api`. + +We currently have three: +- `ufc` is the default `flags-v1.json`. +- `obfuscated` is the obfuscated version of `ufc`. +- `bandits` is bandit flags and bandit models. + +See `prepare.js` for the full list of files and environments served. + +# Q&A + +## Why port 8378? + +8378 is "test" in T9. diff --git a/mock-server/package.json b/mock-server/package.json index 21332d2e..3aa3c894 100644 --- a/mock-server/package.json +++ b/mock-server/package.json @@ -1,7 +1,8 @@ { "private": true, + "type": "module", "scripts": { - "prestart": "./prepare.sh", + "prestart": "node ./prepare.js", "start": "http-server -p 8378 -a 127.0.0.1 --cors" }, "dependencies": { diff --git a/mock-server/prepare.js b/mock-server/prepare.js new file mode 100644 index 00000000..d9ee7b5b --- /dev/null +++ b/mock-server/prepare.js @@ -0,0 +1,45 @@ +// This is a prepare script that is automatically run before "start" command. +// +// It is repsonsible to copy files from sdk-test-data into the proper +import fs from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +// The format here is: +// env name -> { api path -> file relative to sdk-test-data } +const envs = { + ufc: { + 'api/flag-config/v1/config': 'ufc/flags-v1.json', + }, + obfuscated: { + 'api/flag-config/v1/config': 'ufc/flags-v1-obfuscated.json', + }, + bandit: { + 'api/flag-config/v1/config': 'ufc/bandit-flags-v1.json', + 'api/flag-config/v1/bandits': 'ufc/bandit-models-v1.json', + } +} + + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const sdkTestDataPath = path.join(__dirname, '../sdk-test-data/') +const publicPath = path.join(__dirname, './public/') + +try { + await fs.rm(path.join(__dirname, 'public'), {recursive: true}); +} catch { + // ignore if it's not present +} + +for (const [env, links] of Object.entries(envs)) { + for (const [target, source] of Object.entries(links)) { + const sourcePath = path.join(sdkTestDataPath, source); + const targetPath = path.join(publicPath, env, target); + + console.log(sourcePath, '->', targetPath); + + await fs.mkdir(path.dirname(targetPath), { recursive: true }); + console.log('mkdir', path.dirname(targetPath)); + await fs.copyFile(sourcePath, targetPath); + } +} diff --git a/mock-server/prepare.sh b/mock-server/prepare.sh deleted file mode 100755 index f1eaafbc..00000000 --- a/mock-server/prepare.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env sh -set -e - -cd "$(dirname "$0")" -rm -rf ./public -mkdir -p ./public/ufc/api/flag-config/v1/ -mkdir -p ./public/obfuscated/api/flag-config/v1/ -mkdir -p ./public/bandit/api/flag-config/v1/ -ln -s ../../../../../../sdk-test-data/ufc/flags-v1.json ./public/ufc/api/flag-config/v1/config -ln -s ../../../../../../sdk-test-data/ufc/flags-v1-obfuscated.json ./public/obfuscated/api/flag-config/v1/config -ln -s ../../../../../../sdk-test-data/ufc/bandit-flags-v1.json ./public/bandit/api/flag-config/v1/config -ln -s ../../../../../../sdk-test-data/ufc/bandit-models-v1.json ./public/bandit/api/flag-config/v1/bandits