Skip to content

Commit 706b984

Browse files
authored
Merge pull request #1319 from FireBurn/localhost
Allow setting host for listener
2 parents 27a1738 + 2fc7bba commit 706b984

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

api/config/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface Config {
3939
googleAnalyticsKey: string;
4040
postgres: PostgresConfig;
4141
log: LogConfig;
42+
host: string;
4243
port: number;
4344
urlPrefix: string;
4445
defaults: any;
@@ -61,6 +62,7 @@ const config: Record<Env, Config> = {
6162
name: "postcodes.io",
6263
file: "stdout",
6364
},
65+
host: "0.0.0.0",
6466
port: 8000,
6567
urlPrefix: "",
6668
defaults,
@@ -79,6 +81,7 @@ const config: Record<Env, Config> = {
7981
name: "postcodes.io",
8082
file: join(__dirname, "../test.log"),
8183
},
84+
host: "0.0.0.0",
8285
port: 8000,
8386
urlPrefix: "",
8487
defaults,
@@ -97,6 +100,7 @@ const config: Record<Env, Config> = {
97100
name: "postcodes.io",
98101
file: "perf", // Use pino.extreme
99102
},
103+
host: "0.0.0.0",
100104
port: 8000,
101105
urlPrefix: "",
102106
defaults,
@@ -109,6 +113,7 @@ export const getConfig = (env?: Env): Config => {
109113
const cfg = config[environment as Env];
110114

111115
const {
116+
HOST,
112117
PORT,
113118
POSTGRES_USER,
114119
POSTGRES_PASSWORD,
@@ -124,6 +129,7 @@ export const getConfig = (env?: Env): Config => {
124129
URL_PREFIX,
125130
} = process.env;
126131

132+
if (HOST !== undefined) cfg.host = HOST;
127133
if (PORT !== undefined) cfg.port = parseInt(PORT, 10);
128134

129135
if (POSTGRES_USER !== undefined) cfg.postgres.user = POSTGRES_USER;

api/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const config = getConfig();
44
import App from "./app";
55
const app = App(config);
66
import { logger } from "./app/lib/logger";
7+
const { host } = config;
78
const { port } = config;
89

9-
const server = app.listen(port);
10+
const server = app.listen(port, host);
1011

1112
const closeSocket = (_: unknown, socket: any) => {
1213
if (!socket.destroyed) socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
@@ -19,6 +20,6 @@ process.on("SIGTERM", () => {
1920
process.exit(0);
2021
});
2122

22-
logger.info(`Postcode API listening on port ${port}`);
23+
logger.info(`Postcode API listening on ${host} port ${port}`);
2324

2425
module.exports = app;

0 commit comments

Comments
 (0)