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

@ -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}`);
});