support custom port for web and api via cli args

This commit is contained in:
liamcottle
2024-04-24 16:17:09 +12:00
parent 46d6760698
commit d24f684a2f
2 changed files with 41 additions and 1 deletions

View File

@ -129,6 +129,8 @@ node src/index.js
# Server running at http://127.0.0.1:8080 # Server running at http://127.0.0.1:8080
``` ```
> Note: You can also use a custom port with `--port 8123`
## Upgrading ## Upgrading
Run the following commands from inside the `meshtastic-map` repo. Run the following commands from inside the `meshtastic-map` repo.

View File

@ -1,6 +1,8 @@
const path = require('path'); const path = require('path');
const express = require('express'); const express = require('express');
const protobufjs = require("protobufjs"); const protobufjs = require("protobufjs");
const commandLineArgs = require("command-line-args");
const commandLineUsage = require("command-line-usage");
// create prisma db client // create prisma db client
const { PrismaClient } = require("@prisma/client"); const { PrismaClient } = require("@prisma/client");
@ -11,6 +13,42 @@ BigInt.prototype.toJSON = function() {
return this.toString(); 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 // load protobufs
const root = new protobufjs.Root(); const root = new protobufjs.Root();
root.resolvePath = (origin, target) => path.join(__dirname, "protos", target); root.resolvePath = (origin, target) => path.join(__dirname, "protos", target);
@ -377,7 +415,7 @@ app.get('/api/v1/waypoints', async (req, res) => {
}); });
// start express server // start express server
const listener = app.listen(8080, () => { const listener = app.listen(port, () => {
const port = listener.address().port; const port = listener.address().port;
console.log(`Server running at http://127.0.0.1:${port}`); console.log(`Server running at http://127.0.0.1:${port}`);
}); });