feat: allow binding service host and port

This commit is contained in:
Eduardo Dorado
2026-05-21 09:48:38 -03:00
parent 46ed03ac31
commit b0ec79507d
2 changed files with 29 additions and 3 deletions
+21 -3
View File
@@ -5,8 +5,8 @@ const { createCanvas } = require("canvas");
const { jsPDF } = require("jspdf");
const { TextDecoder } = require("util");
const HOST = "127.0.0.1";
const PORT = 3040;
const DEFAULT_HOST = "127.0.0.1";
const DEFAULT_PORT = 3040;
const PAGE_PADDING_DOTS = 24;
const DEFAULT_RENDER_OPTIONS = {
width: 4,
@@ -18,6 +18,22 @@ const DEFAULT_RENDER_OPTIONS = {
let bwipPromise;
function getCliOption(name) {
const prefix = `--${name}=`;
const option = process.argv.find((argument) => argument.startsWith(prefix));
return option ? option.slice(prefix.length) : undefined;
}
function getServerConfig() {
const port = Number(getCliOption("port") || process.env.PORT || DEFAULT_PORT);
return {
host: getCliOption("host") || process.env.HOST || DEFAULT_HOST,
port: Number.isFinite(port) && port > 0 ? port : DEFAULT_PORT,
};
}
function normalizeZpl(zpl) {
return zpl.replace(/\\n/g, "\n").replace(/\r\n/g, "\n");
}
@@ -660,9 +676,10 @@ function buildServer() {
async function start() {
const app = buildServer();
const { host, port } = getServerConfig();
try {
await app.listen({ host: HOST, port: PORT });
await app.listen({ host, port });
} catch (error) {
app.log.error(error);
process.exit(1);
@@ -676,4 +693,5 @@ if (require.main === module) {
module.exports = {
buildServer,
convertZplToPdf,
getServerConfig,
};