feat: allow binding service host and port
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user