Skip to content

Commit

Permalink
fix: load config.yaml at runtime
Browse files Browse the repository at this point in the history
Changed the import strategy for the router config options to dynamic
i.e. at runtime, instead of importing at build time
  • Loading branch information
deshmukhmayur committed May 24, 2023
1 parent 96d832f commit 574872b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 76 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
## MongoDB connection
MONGODB_URI=

## Proxy Route configurations
# ROUTE_CONFIG_PATH=config.yaml

## API Token options
# API_TOKEN_PREFIX=apik
# API_TOKEN_PREFIX=apik # `_` is automatically appended to the prefix

# ENABLE_API_KEY_AUTH=false
File renamed without changes.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"license": "Apache-2.0",
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.0",
"@rollup/plugin-yaml": "^4.0.1",
"@types/body-parser": "^1.19.2",
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.13",
Expand Down Expand Up @@ -52,8 +51,8 @@
"mongodb": "^5.4.0",
"mongoose": "^7.1.0",
"nanoid": "^4.0.2",
"passport": "^0.6.0",
"pino": "^8.14.1",
"pino-http": "^8.3.3"
"pino-http": "^8.3.3",
"yaml": "^2.3.0"
}
}
60 changes: 7 additions & 53 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import typescript from "@rollup/plugin-typescript";
import yaml from "@rollup/plugin-yaml";

export default {
input: "./src/index.ts",
output: {
dir: "./dist",
},
plugins: [typescript(), yaml()],
plugins: [typescript()],
};
17 changes: 6 additions & 11 deletions src/@types/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
/// <reference types="http-proxy-middleware" />

declare module '*/config/routes.yaml' {
const config: YamlConfig;
export default config;
declare type YamlConfig = {
routes: Routes;
};

type Routes = {
[path: string]: Options;
};

export type YamlConfig = {
routes: Routes;
};
}
type Routes = {
[path: string]: Options;
};
2 changes: 1 addition & 1 deletion src/resolvers/apikeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getKey = async (req: Request, res: Response) => {

const createKey = async (req: Request, res: Response) => {
/* Generate a new key */
const key = generateToken(API_TOKEN_PREFIX);
const key = generateToken(API_TOKEN_PREFIX + '_');
const hashKey = generateHash(key);
const body = req.body;

Expand Down
2 changes: 0 additions & 2 deletions src/resolvers/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export default function getProxyResolver(options: Options) {
const resolver = (req: Request, res: Response, next: NextFunction) => {
const proxy = createProxyMiddleware({
...options,
logLevel: 'debug',
secure: false,
headers: {
...options.headers,
'x-forwarded-user': res.locals.userId,
Expand Down
9 changes: 7 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import cors from 'cors';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import apiKeyRouter from './resolvers/apikeys';
import { ENABLE_API_KEY_AUTH } from './setup/env';
import { ENABLE_API_KEY_AUTH, ROUTE_CONFIG_PATH } from './setup/env';
import { pinoMiddleware } from './setup/logger';
import { auth } from './middlewares/auth';
import config from '../config/routes.yaml';
import getProxyResolver from './resolvers/proxy';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { parse } from 'yaml';

const server = express();

Expand All @@ -28,6 +30,9 @@ if (ENABLE_API_KEY_AUTH) {
}

/* Creating proxy handlers for all the routes defined in config.routes */
const config: YamlConfig = parse(
readFileSync(resolve(ROUTE_CONFIG_PATH)).toString()
);
Object.entries(config.routes).forEach((route) => {
const [path, options] = route;
server.use(path, [auth], getProxyResolver(options));
Expand Down
3 changes: 2 additions & 1 deletion src/setup/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const {
NODE_ENV = 'development',
PORT = '8080',
MONGODB_URI = 'mongodb://localhost:27017/auth-proxy',
API_TOKEN_PREFIX = 'apik'
API_TOKEN_PREFIX = 'apik',
ROUTE_CONFIG_PATH = 'config.yaml',
} = process.env;

export const ENABLE_API_KEY_AUTH = process.env.ENABLE_API_KEY_AUTH === 'true';

0 comments on commit 574872b

Please sign in to comment.