Skip to content

Commit

Permalink
chore(mock-server): rewrite prepare.sh with javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
rasendubi committed Aug 28, 2024
1 parent ace8a3b commit f732d33
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
15 changes: 14 additions & 1 deletion mock-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion mock-server/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
45 changes: 45 additions & 0 deletions mock-server/prepare.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 0 additions & 12 deletions mock-server/prepare.sh

This file was deleted.

0 comments on commit f732d33

Please sign in to comment.