mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
feat: add serverUrl configuration support with settings.json fallback
Implement serverUrl initialization following the cliApiToken design pattern: - Environment variable HAPI_SERVER_URL (highest priority, allows temporary override) - settings.json serverUrl field (user persistent configuration) - Default value 'http://localhost:3006' (fallback) Changes: - Add serverUrl field to Settings interface (persistence.ts) - Convert serverUrl to private property with getter/setter (configuration.ts) - Create initializeServerUrl module to load from settings (serverUrlInit.ts) - Integrate serverUrl initialization in tokenInit flow (tokenInit.ts) - Skip auto-start when serverUrl is configured in settings (autoStartServer.ts)
This commit is contained in:
@@ -12,7 +12,7 @@ import packageJson from '../package.json'
|
||||
import { getCliArgs } from '@/utils/cliArgs'
|
||||
|
||||
class Configuration {
|
||||
public readonly serverUrl: string
|
||||
private _serverUrl: string
|
||||
private _cliApiToken: string
|
||||
public readonly isDaemonProcess: boolean
|
||||
|
||||
@@ -29,7 +29,7 @@ class Configuration {
|
||||
|
||||
constructor() {
|
||||
// Server configuration
|
||||
this.serverUrl = process.env.HAPI_SERVER_URL || 'http://localhost:3006'
|
||||
this._serverUrl = process.env.HAPI_SERVER_URL || 'http://localhost:3006'
|
||||
this._cliApiToken = process.env.CLI_API_TOKEN || ''
|
||||
|
||||
// Check if we're running as daemon based on process args
|
||||
@@ -64,6 +64,14 @@ class Configuration {
|
||||
}
|
||||
}
|
||||
|
||||
get serverUrl(): string {
|
||||
return this._serverUrl
|
||||
}
|
||||
|
||||
_setServerUrl(url: string): void {
|
||||
this._serverUrl = url
|
||||
}
|
||||
|
||||
get cliApiToken(): string {
|
||||
return this._cliApiToken
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ interface Settings {
|
||||
machineIdConfirmedByServer?: boolean
|
||||
daemonAutoStartWhenRunningHappy?: boolean
|
||||
cliApiToken?: string
|
||||
// Server URL for API connections (priority: env HAPI_SERVER_URL > this > default)
|
||||
serverUrl?: string
|
||||
}
|
||||
|
||||
const defaultSettings: Settings = {}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Server URL initialization module
|
||||
*
|
||||
* Handles HAPI_SERVER_URL initialization with priority:
|
||||
* 1. Environment variable (highest - allows temporary override)
|
||||
* 2. Settings file (~/.hapi/settings.json)
|
||||
* 3. Default value (http://localhost:3006)
|
||||
*/
|
||||
|
||||
import { configuration } from '@/configuration'
|
||||
import { readSettings } from '@/persistence'
|
||||
|
||||
/**
|
||||
* Initialize server URL
|
||||
* Must be called before any API operations
|
||||
*/
|
||||
export async function initializeServerUrl(): Promise<void> {
|
||||
// 1. Environment variable has highest priority (allows temporary override)
|
||||
if (process.env.HAPI_SERVER_URL) {
|
||||
return
|
||||
}
|
||||
|
||||
// 2. Read from settings file
|
||||
const settings = await readSettings()
|
||||
if (settings.serverUrl) {
|
||||
configuration._setServerUrl(settings.serverUrl)
|
||||
return
|
||||
}
|
||||
|
||||
// 3. Default value already set in configuration constructor
|
||||
}
|
||||
@@ -12,12 +12,16 @@ import { stdin as input, stdout as output } from 'node:process'
|
||||
import chalk from 'chalk'
|
||||
import { configuration } from '@/configuration'
|
||||
import { readSettings, updateSettings } from '@/persistence'
|
||||
import { initializeServerUrl } from '@/ui/serverUrlInit'
|
||||
|
||||
/**
|
||||
* Initialize CLI API token
|
||||
* Must be called before any API operations
|
||||
*/
|
||||
export async function initializeToken(): Promise<void> {
|
||||
// Initialize server URL first (env > settings.json > default)
|
||||
await initializeServerUrl()
|
||||
|
||||
// 1. Environment variable has highest priority (allows temporary override)
|
||||
if (configuration.cliApiToken) {
|
||||
return
|
||||
|
||||
@@ -96,8 +96,16 @@ async function shouldAutoStartServer(): Promise<boolean> {
|
||||
return false
|
||||
}
|
||||
|
||||
// Condition 2: cliApiToken exists in settings.json (server was previously started)
|
||||
// Condition 2: Check settings.json
|
||||
const settings = await readSettings()
|
||||
|
||||
// 2a: serverUrl is set in settings.json (user configured a specific server)
|
||||
if (settings.serverUrl) {
|
||||
logger.debug('[AUTO-START] serverUrl is set in settings.json, skipping auto-start')
|
||||
return false
|
||||
}
|
||||
|
||||
// 2b: cliApiToken exists in settings.json (server was previously started)
|
||||
if (!settings.cliApiToken) {
|
||||
logger.debug('[AUTO-START] No cliApiToken in settings, skipping auto-start')
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user