]> zoso.dev Git - nano-pow.git/commitdiff
Load config file from NanoPow home directory and use it to set execution options...
authorChris Duncan <chris@zoso.dev>
Sun, 13 Apr 2025 07:15:12 +0000 (00:15 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 13 Apr 2025 07:15:12 +0000 (00:15 -0700)
src/bin/cli.ts
src/bin/server.ts

index 3355a6f461d6ff8d908d40063eb733d6b21eaec4..6cb38db1451b15487fd6f5efb2725fd5aa64b4a4 100755 (executable)
@@ -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'
 
index e4784bef99c51c983184b9726170eeb18808a0bd..dacacdc6a871b88da111532010baa444a4dd90d8 100755 (executable)
@@ -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<void> {
        let statusCode: number = 500
        let headers: http.OutgoingHttpHeaders = { 'Content-Type': 'application/json' }