Skip to content
This repository has been archived by the owner on Oct 22, 2022. It is now read-only.

Commit

Permalink
Cleans up config and adds better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-miller-0 committed Dec 11, 2021
1 parent 2d0dad5 commit d2157af
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.config.json
dist
node_modules
*.log
*.log
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This module contains both HTTP server and MQTT broker, so it can be used as a si
You can run this `node.js` module in a variety of ways. First, clone this repo and run

```
npm i && npm run build
npm i
```

You can now start the process with
Expand Down Expand Up @@ -55,6 +55,21 @@ To watch logs in real time, start your pm2 process and run
tail -f <LOG_DEST>
```

### Configuration

The config parmaeters set in `config.js` are referenced when starting the application. These come with defaults and
also look at a local file `.config.json` (if it exists). This local `.config.json` is not tracked in git and it is
where you can define params that are inspected in `config.js`.

*Sample `.config.json`:*

```
{
"LOG_DEST": "./lattice.log",
"MQTT_PASSWORD": "superdupersecretpassword"
}
```

### Running with Docker

> **NOTE**: The Docker scripts are written using the default ports (specified in `config.js`: 1883 for MQTT, 3000 for http). If you want to change these ports, please also update the `Dockerfile` and the `docker-run` script in `package.json`.
Expand Down
26 changes: 13 additions & 13 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const local = require('./.config.json');

module.exports = {
APP_HOST: '0.0.0.0',
APP_PORT: 3000,
LOG_DEST: '/tmp/lattice-connector.log',
LOG_LEVEL: 'error', // trace, debug, info, warn, error
MQTT: {
CLIENT_ID: 'lattice-connector-endpoint',
USERNAME: 'connector',
PASSWORD: 'connectorpasswordpleasechangeme',
BROKER_PORT: 1883,
},
TIMEOUT_ITER_MS: 500,
TIMEOUT_TOTAL_MS: 60000,
}
APP_HOST: local.APP_HOST || '0.0.0.0',
APP_PORT: local.APP_PORT || 3000,
LOG_DEST: local.LOG_DEST || '/tmp/lattice-connector.log',
LOG_LEVEL: local.LOG_LEVEL || 'error', // trace, debug, info, warn, error
MQTT_CLIENT_ID: local.MQTT_CLIENT_ID || 'lattice-connector-endpoint',
MQTT_USERNAME: local.MQTT_USERNAME || 'connector',
MQTT_PASSWORD: local.MQTT_PASSWORD || 'connectorpasswordpleasechangeme',
MQTT_BROKER_PORT: local.MQTT_BROKER_POR || 1883,
TIMEOUT_ITER_MS: local.TIMEOUT_ITER_MSG || 500,
TIMEOUT_TOTAL_MS: local.TIMEOUT_TOTAL_MSG || 60000,
};
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "lattice-connect",
"version": "0.1.1",
"version": "0.2.0",
"description": "A small HTTP server + MQTT broker designed to bridge the web with Lattices in the field",
"main": "dist/index.js",
"scripts": {
"build": "babel src -d dist",
"lint": "eslint src",
"start": "npx pm2 start dist/index.js --name lattice-connect --watch",
"start": "npm run build && npx pm2 start dist/index.js --name lattice-connect --watch",
"stop": "npx pm2 stop lattice-connect",
"rm": "npx pm2 delete lattice-connect && pkill node",
"logs": "npx pm2 logs lattice-connect",
Expand Down
28 changes: 18 additions & 10 deletions src/broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@
// connect to this broker.
import logger from './logger';

const aedes = require('aedes')();
const aedes = require('aedes');
const net = require('net');

aedes.on('client', (_client) => {
logger.debug(`BROKER: New client (${_client.id}) attempting connection.`);
const instance = aedes();
let connCount = 0;

instance.on('client', (_client) => {
logger.debug(`BROKER (conns=${connCount}): New client (${_client.id}) attempting connection.`);
});

instance.on('clientReady', (_client) => {
connCount += 1;
logger.info(`BROKER (conns=${connCount}): Client (${_client.id}) connected.`);
});

aedes.on('clientReady', (_client) => {
logger.debug(`BROKER: Client (${_client.id}) connected.`);
instance.on('clientError', (_client, error) => {
logger.error(`BROKER (conns=${connCount}): Error from client ${_client.id}: ${error.message}`);
});

aedes.on('subscribe', (_subscriptions, _client) => {
logger.debug(`BROKER: Client (${_client.id}) subscribed to topics: ${JSON.stringify(_subscriptions)}`);
instance.on('subscribe', (_subscriptions, _client) => {
logger.debug(`BROKER (conns=${connCount}): Client (${_client.id}) subscribed to topics: ${JSON.stringify(_subscriptions)}`);
});

aedes.on('publish', (_packet, _client) => {
logger.trace(`BROKER: Client (${_client}) published message: ${JSON.stringify(_packet)}`)
instance.on('publish', (_packet, _client) => {
logger.trace(`BROKER (conns=${connCount}): Client (${_client}) published message: ${JSON.stringify(_packet)}`);
});

const broker = net.createServer(aedes.handle);
const broker = net.createServer(instance.handle);
export default broker;
8 changes: 4 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import logger from './logger';
const config = require('cconfig')();

const connectOptions = {
clientId: config.MQTT.CLIENT_ID,
username: config.MQTT.USERNAME,
password: config.MQTT.PASSWORD,
clientId: config.MQTT_CLIENT_ID,
username: config.MQTT_USERNAME,
password: config.MQTT_PASSWORD,
};

const brokerURI = `mqtt://${config.APP_HOST}:${config.MQTT.BROKER_PORT}`;
const brokerURI = `mqtt://${config.APP_HOST}:${config.MQTT_BROKER_PORT}`;
const client = mqtt.connect(brokerURI, connectOptions);

client.on('connect', () => {
Expand Down
9 changes: 4 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ process.on('unhandledRejection', (reason, promise) => {
//----------------------------------
function startBroker() {
logger.info('Starting broker', broker)
broker.listen(config.MQTT.BROKER_PORT, () => {
logger.info('MQTT broker server started on port ', config.MQTT.BROKER_PORT);
broker.listen(config.MQTT_BROKER_PORT, () => {
logger.info('MQTT broker server started on port ', config.MQTT_BROKER_PORT);
});
}
logger.info('broker?', broker);

if (broker.closed === false) {
broker.close(() => {
logger.info('closed?');
Expand All @@ -54,10 +54,9 @@ if (broker.closed === false) {
startBroker();
}


// 2. Create the REST server
//----------------------------------
logger.info('app', app)
logger.info('app', app);
app.listen(config.APP_PORT, config.APP_HOST, () => {
logger.info(`signing-api-proxy started listening on ${config.APP_PORT}`);
});

0 comments on commit d2157af

Please sign in to comment.