Skip to content

Commit

Permalink
Merge pull request #81 from ralight/unix-sockets
Browse files Browse the repository at this point in the history
Allow services to bind to a unix socket if port == 0.
  • Loading branch information
cleancoderocker authored Jan 13, 2022
2 parents 531e2e2 + 3479510 commit 0965e2e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
16 changes: 11 additions & 5 deletions packages/repository/src/mongoDB/MongoDBConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const logger = require('@cedalo/logger').create({ name: 'MongoDBConnection' });

const defaultOptions = {
host: process.env.MONGO_HOST || 'localhost',
port: parseInt(process.env.MONGO_PORT, 10) || 27017,
port: !!process.env.MONGO_PORT ? parseInt(process.env.MONGO_PORT, 10) : 27017,
database: process.env.MONGO_DATABASE || 'test',
username: process.env.MONGO_USERNAME,
password: process.env.MONGO_PASSWORD
Expand Down Expand Up @@ -47,10 +47,16 @@ const mapLegacyOptions = ({

const authMechanism = `authMechanism=SCRAM-SHA-1`;

const buildUrl = ({ host, port, database }, auth) =>
auth
? `mongodb://${auth}@${host}:${port}/${database}?${authMechanism}`
: `mongodb://${host}:${port}/${database}`;
const buildUrl = ({ host, port, database }, auth) => {
/* port === 0 means options.host is a unix socket */
const hostPath = port
? `${host}:${port}`
: encodeURIComponent(host);
const url = auth
? `mongodb://${auth}@${hostPath}/${database}?${authMechanism}`
: `mongodb://${hostPath}/${database}`;
return url;
}

const buildAuth = (username, password) =>
username && password ? `${username}:${password}` : undefined;
Expand Down
24 changes: 17 additions & 7 deletions packages/rest-server-core/src/DefaultApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,23 @@ module.exports = class DefaultApp {
this._server = http.createServer(this.app);
}
this._server.timeout = 10000;
this._server.listen(port, ipaddress, () => {
// eslint-disable-next-line
logger.info(
`${this.app.locals.pkg.name} started at ${new Date()}. IP address: ${ipaddress}, port: ${port}`
);
resolve();
});
if(port === 0) {
this._server.listen(ipaddress, () => {
// eslint-disable-next-line
logger.info(
`${this.app.locals.pkg.name} started at ${new Date()}. Socket path: ${ipaddress}`
);
resolve();
});
} else {
this._server.listen(port, ipaddress, () => {
// eslint-disable-next-line
logger.info(
`${this.app.locals.pkg.name} started at ${new Date()}. IP address: ${ipaddress}, port: ${port}`
);
resolve();
});
}
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/rest-server-core/src/server/RESTServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = class RESTServer {
_initializeConfiguration(config) {
let configuration = Object.assign({}, DEFAULT_CONFIG, config);
configuration.http.port = process.env.RESTSERVER_PORT || configuration.http.port;
configuration.http.ipaddress = process.env.RESTSERVER_HOST || configuration.http.ipaddress;
configuration = this._postConfig(configuration);
return configuration;
}
Expand Down

0 comments on commit 0965e2e

Please sign in to comment.