diff --git a/scripts/config.sh b/scripts/config.sh index ec275ae..29ee41a 100755 --- a/scripts/config.sh +++ b/scripts/config.sh @@ -2,8 +2,8 @@ # It isn't enough to modify this yet. # You also need to manually update the toes.service file. -HOST="${HOST:-toes@$(hostname).local}" -URL="${URL:-http://$(hostname).local}" +HOST="${HOST:-toes@toes.local}" +URL="${URL:-http://toes.local}" DEST="${DEST:-~/toes}" DATA_DIR="${DATA_DIR:-~/data}" APPS_DIR="${APPS_DIR:-~/apps}" diff --git a/src/cli/http.ts b/src/cli/http.ts index 90290ba..1cdaad1 100644 --- a/src/cli/http.ts +++ b/src/cli/http.ts @@ -1,7 +1,7 @@ import type { Manifest } from '@types' -import { hostname } from 'os' +import { LOCAL_HOST } from '%config' -const DEFAULT_HOST = process.env.DEV ? 'http://localhost:3000' : `http://${hostname()}.local` +const DEFAULT_HOST = process.env.DEV ? 'http://localhost:3000' : `http://${LOCAL_HOST}` const normalizeUrl = (url: string) => url.startsWith('http://') || url.startsWith('https://') ? url : `http://${url}` diff --git a/src/lib/config.ts b/src/lib/config.ts new file mode 100644 index 0000000..fe800d9 --- /dev/null +++ b/src/lib/config.ts @@ -0,0 +1,4 @@ +import { hostname } from 'os' + +export const HOSTNAME = hostname() +export const LOCAL_HOST = `${HOSTNAME}.local` diff --git a/src/server/apps.ts b/src/server/apps.ts index d388574..2119c1a 100644 --- a/src/server/apps.ts +++ b/src/server/apps.ts @@ -4,7 +4,7 @@ import type { Subprocess } from 'bun' import { DEFAULT_EMOJI } from '@types' import { buildAppUrl, toSubdomain } from '@urls' import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, symlinkSync, unlinkSync, writeFileSync } from 'fs' -import { hostname } from 'os' +import { LOCAL_HOST } from '%config' import { join, resolve } from 'path' import { loadAppEnv } from '../tools/env' import { publishApp, unpublishAll, unpublishApp } from './mdns' @@ -16,7 +16,7 @@ export type { AppState } from '@types' export const APPS_DIR = process.env.APPS_DIR ?? resolve(join(process.env.DATA_DIR ?? '.', 'apps')) export const TOES_DIR = process.env.TOES_DIR ?? join(process.env.DATA_DIR ?? '.', 'toes') -const defaultHost = process.env.NODE_ENV === 'production' ? `${hostname()}.local` : 'localhost' +const defaultHost = process.env.NODE_ENV === 'production' ? LOCAL_HOST : 'localhost' export const TOES_URL = process.env.TOES_URL ?? `http://${defaultHost}:${process.env.PORT || 3000}` const HEALTH_CHECK_FAILURES_BEFORE_RESTART = 3 diff --git a/src/server/mdns.ts b/src/server/mdns.ts index 9879abb..75897d8 100644 --- a/src/server/mdns.ts +++ b/src/server/mdns.ts @@ -1,6 +1,7 @@ import type { Subprocess } from 'bun' import { toSubdomain } from '@urls' -import { hostname, networkInterfaces } from 'os' +import { LOCAL_HOST } from '%config' +import { networkInterfaces } from 'os' import { hostLog } from './tui' const _publishers = new Map() @@ -24,7 +25,7 @@ export function cleanupStalePublishers() { if (!isEnabled) return try { - const result = Bun.spawnSync(['pkill', '-f', `avahi-publish.*${hostname()}\\.local`]) + const result = Bun.spawnSync(['pkill', '-f', `avahi-publish.*${LOCAL_HOST}`]) if (result.exitCode === 0) { hostLog('mDNS: cleaned up stale avahi-publish processes') } @@ -41,7 +42,7 @@ export function publishApp(name: string) { return } - const host = `${toSubdomain(name)}.${hostname()}.local` + const host = `${toSubdomain(name)}.${LOCAL_HOST}` try { const proc = Bun.spawn(['avahi-publish', '-a', host, '-R', ip], { @@ -68,7 +69,7 @@ export function unpublishApp(name: string) { proc.kill() _publishers.delete(name) - hostLog(`mDNS: unpublished ${toSubdomain(name)}.${hostname()}.local`) + hostLog(`mDNS: unpublished ${toSubdomain(name)}.${LOCAL_HOST}`) } export function unpublishAll() { @@ -76,7 +77,7 @@ export function unpublishAll() { for (const [name, proc] of _publishers) { proc.kill() - hostLog(`mDNS: unpublished ${toSubdomain(name)}.${hostname()}.local`) + hostLog(`mDNS: unpublished ${toSubdomain(name)}.${LOCAL_HOST}`) } _publishers.clear() }