diff --git a/README.md b/README.md index 97e4c7e..9ae7f65 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,8 @@ node src/index.js # Server running at http://127.0.0.1:8080 ``` +> Note: You can also use a custom port with `--port 8123` + ## Upgrading Run the following commands from inside the `meshtastic-map` repo. diff --git a/src/index.js b/src/index.js index 79ff34c..8cda479 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,8 @@ const path = require('path'); const express = require('express'); const protobufjs = require("protobufjs"); +const commandLineArgs = require("command-line-args"); +const commandLineUsage = require("command-line-usage"); // create prisma db client const { PrismaClient } = require("@prisma/client"); @@ -11,6 +13,42 @@ BigInt.prototype.toJSON = function() { return this.toString(); } +const optionsList = [ + { + name: 'help', + alias: 'h', + type: Boolean, + description: 'Display this usage guide.' + }, + { + name: "port", + type: Number, + description: "Port to serve web ui and api from.", + }, +]; + +// parse command line args +const options = commandLineArgs(optionsList); + +// show help +if(options.help){ + const usage = commandLineUsage([ + { + header: 'Meshtastic Map', + content: 'A map of all Meshtastic nodes heard via MQTT.', + }, + { + header: 'Options', + optionList: optionsList, + }, + ]); + console.log(usage); + return; +} + +// get options and fallback to default values +const port = options["port"] ?? 8080; + // load protobufs const root = new protobufjs.Root(); root.resolvePath = (origin, target) => path.join(__dirname, "protos", target); @@ -377,7 +415,7 @@ app.get('/api/v1/waypoints', async (req, res) => { }); // start express server -const listener = app.listen(8080, () => { +const listener = app.listen(port, () => { const port = listener.address().port; console.log(`Server running at http://127.0.0.1:${port}`); });