address comments
This commit is contained in:
+1
-2
@@ -4,8 +4,7 @@ app:
|
||||
|
||||
backend:
|
||||
baseUrl: http://localhost:7000
|
||||
bindPort: 7000
|
||||
bindHost: localhost
|
||||
listen: 0.0.0.0:7000
|
||||
cors:
|
||||
origin: http://localhost:3000
|
||||
methods: [GET, POST, PUT, DELETE]
|
||||
|
||||
@@ -57,11 +57,11 @@ export class ServiceBuilderImpl implements ServiceBuilder {
|
||||
}
|
||||
|
||||
const baseOptions = readBaseOptions(backendConfig);
|
||||
if (baseOptions.bindPort) {
|
||||
this.port = baseOptions.bindPort;
|
||||
if (baseOptions.listenPort) {
|
||||
this.port = baseOptions.listenPort;
|
||||
}
|
||||
if (baseOptions.bindHost) {
|
||||
this.host = baseOptions.bindHost;
|
||||
if (baseOptions.listenHost) {
|
||||
this.host = baseOptions.listenHost;
|
||||
}
|
||||
|
||||
const corsOptions = readCorsOptions(backendConfig);
|
||||
@@ -122,7 +122,7 @@ export class ServiceBuilderImpl implements ServiceBuilder {
|
||||
|
||||
const server = stoppable(
|
||||
app.listen(port, host, () => {
|
||||
logger.info(`Listening on port ${port}`);
|
||||
logger.info(`Listening on ${host}:${port}`);
|
||||
}),
|
||||
0,
|
||||
);
|
||||
@@ -143,31 +143,10 @@ export class ServiceBuilderImpl implements ServiceBuilder {
|
||||
logger: Logger;
|
||||
corsOptions?: cors.CorsOptions;
|
||||
} {
|
||||
let port: number;
|
||||
if (this.port !== undefined) {
|
||||
port = this.port;
|
||||
} else {
|
||||
port = parseInt(process.env.PORT ?? '', 10) || DEFAULT_PORT;
|
||||
}
|
||||
|
||||
let host: string;
|
||||
if (this.host !== undefined) {
|
||||
host = this.host;
|
||||
} else {
|
||||
host = process.env.HOST || DEFAULT_HOST;
|
||||
}
|
||||
|
||||
let logger: Logger;
|
||||
if (this.logger) {
|
||||
logger = this.logger;
|
||||
} else {
|
||||
logger = getRootLogger();
|
||||
}
|
||||
|
||||
return {
|
||||
port,
|
||||
host,
|
||||
logger,
|
||||
port: this.port ?? DEFAULT_PORT,
|
||||
host: this.host ?? DEFAULT_HOST,
|
||||
logger: this.logger ?? getRootLogger(),
|
||||
corsOptions: this.corsOptions,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ import { ConfigReader } from '@backstage/config';
|
||||
import { CorsOptions } from 'cors';
|
||||
|
||||
export type BaseOptions = {
|
||||
bindPort?: number;
|
||||
bindHost?: string;
|
||||
listenPort?: number;
|
||||
listenHost?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -31,16 +31,17 @@ export type BaseOptions = {
|
||||
* @example
|
||||
* ```json
|
||||
* {
|
||||
* baseUrl: "http://localhost:7000"
|
||||
* bindPort: 7000
|
||||
* bindHost: "0.0.0.0"
|
||||
* baseUrl: "http://localhost:7000",
|
||||
* listen: "0.0.0.0:7000"
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function readBaseOptions(config: ConfigReader): BaseOptions {
|
||||
// TODO(freben): Expand this to support more addresses and perhaps optional
|
||||
const { host, port } = parseListenAddress(config.getString('listen'));
|
||||
return removeUnknown({
|
||||
bindPort: config.getOptionalNumber('bindPort'),
|
||||
bindHost: config.getOptionalString('bindHost'),
|
||||
listenPort: port,
|
||||
listenHost: host,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -110,3 +111,16 @@ function removeUnknown<T extends object>(obj: T): T {
|
||||
Object.entries(obj).filter(([, v]) => v !== undefined),
|
||||
) as T;
|
||||
}
|
||||
|
||||
function parseListenAddress(value: string): { host?: string; port?: number } {
|
||||
const parts = value.split(':');
|
||||
if (parts.length === 1) {
|
||||
return { port: parseInt(parts[0], 10) };
|
||||
}
|
||||
if (parts.length === 2) {
|
||||
return { host: parts[0], port: parseInt(parts[1], 10) };
|
||||
}
|
||||
throw new Error(
|
||||
`Unable to parse listen address ${value}, expected <port> or <host>:<port>`,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user