From 891376a6aac463b8b510b90f0b611ff864e02947 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 13 Apr 2025 00:15:12 -0700 Subject: [PATCH] Load config file from NanoPow home directory and use it to set execution options unless relevant environment variables are present. --- src/bin/cli.ts | 2 +- src/bin/server.ts | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 3355a6f..6cb38db 100755 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -8,7 +8,7 @@ import type { WorkGenerateResponse, WorkValidateResponse } from '#types' process.title = 'NanoPow CLI' -process.env.NANO_POW_DEBUG = '' +delete process.env.NANO_POW_DEBUG process.env.NANO_POW_EFFORT = '' process.env.NANO_POW_PORT = '5041' diff --git a/src/bin/server.ts b/src/bin/server.ts index e4784be..dacacdc 100755 --- a/src/bin/server.ts +++ b/src/bin/server.ts @@ -7,7 +7,7 @@ import { subtle } from 'node:crypto' import { lookup } from 'node:dns/promises' import { readFile, unlink, writeFile } from 'node:fs/promises' import * as http from 'node:http' -import { hostname } from 'node:os' +import { homedir, hostname } from 'node:os' import { join } from 'node:path' import type { NanoPowOptions, WorkGenerateRequest, WorkGenerateResponse, WorkValidateRequest, WorkValidateResponse } from '#types' @@ -15,9 +15,9 @@ process.title = 'NanoPow Server' const MAX_REQUEST_SIZE = 1024 const MAX_BODY_SIZE = 158 -const DEBUG: boolean = !!(process.env.NANO_POW_DEBUG || false) -const EFFORT: number = +(process.env.NANO_POW_EFFORT || 8) -const PORT: number = +(process.env.NANO_POW_PORT || 5040) +let DEBUG: boolean = !!(process.env.NANO_POW_DEBUG || false) +let EFFORT: number = +(process.env.NANO_POW_EFFORT || 8) +let PORT: number = +(process.env.NANO_POW_PORT || 5040) let browser: Browser let page: Page @@ -26,6 +26,30 @@ function log (...args: any[]): void { if (DEBUG) console.log(new Date(Date.now()).toLocaleString(), 'NanoPow', args) } +async function loadConfig () { + const contents = await readFile(join(homedir(), '.nano-pow', 'config'), 'utf-8') + if (typeof contents === 'string') { + const config = contents.split('\n') + for (const line of config) { + const debugMatch = line.match(/^[ \t]*debug[ \t]*(true|false)[ \t]*(#.*)?$/i) + if (Array.isArray(debugMatch)) { + DEBUG = !!(process.env.NANO_POW_DEBUG) || debugMatch?.[1] === 'true' || false + } + + const effortMatch = line.match(/^[ \t]*effort[ \t]*(\d{1,2})[ \t]*(#.*)?$/i) + if (Array.isArray(effortMatch)) { + EFFORT = +(process.env.NANO_POW_EFFORT || effortMatch?.[1] || 8) + } + + const portMatch = line.match(/^[ \t]*port[ \t]*(\d{1,5})[ \t]*(#.*)?$/i) + if (Array.isArray(portMatch)) { + PORT = +(process.env.NANO_POW_PORT || portMatch?.[1] || 5040) + } + } + } +} +await loadConfig() + async function respond (res: http.ServerResponse, data: Buffer[]): Promise { let statusCode: number = 500 let headers: http.OutgoingHttpHeaders = { 'Content-Type': 'application/json' } -- 2.34.1