From 78140927ad1186b7c5a7eb450f30000f83369252 Mon Sep 17 00:00:00 2001 From: Aarnav Tale Date: Sat, 30 Mar 2024 19:00:59 -0400 Subject: [PATCH] fix: use either HEADSCALE_URL or config.server_url --- app/root.tsx | 6 +++--- app/utils/config.ts | 19 ++++++++++++++++++- app/utils/headscale.ts | 12 ++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/root.tsx b/app/root.tsx index bcc3497..a59d3ac 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -22,11 +22,11 @@ export const links: LinksFunction = () => [ ] export async function loader() { - await getContext() + const context = await getContext() registerConfigWatcher() - if (!process.env.HEADSCALE_URL) { - throw new Error('The HEADSCALE_URL environment variable is required') + if (context.headscaleUrl.length === 0) { + throw new Error('No headscale URL was provided either by the HEADSCALE_URL environment variable or the config file') } if (!process.env.COOKIE_SECRET) { diff --git a/app/utils/config.ts b/app/utils/config.ts index 5c083d0..86a8a99 100644 --- a/app/utils/config.ts +++ b/app/utils/config.ts @@ -161,6 +161,7 @@ type Context = { hasConfigWrite: boolean; hasAcl: boolean; hasAclWrite: boolean; + headscaleUrl: string; } export let context: Context @@ -172,13 +173,29 @@ export async function getContext() { hasConfig: await hasConfig(), hasConfigWrite: await hasConfigW(), hasAcl: await hasAcl(), - hasAclWrite: await hasAclW() + hasAclWrite: await hasAclW(), + headscaleUrl: await getHeadscaleUrl() } } return context } +async function getHeadscaleUrl() { + if (process.env.HEADSCALE_URL) { + return process.env.HEADSCALE_URL + } + + try { + const config = await getConfig() + if (config.server_url) { + return config.server_url + } + } catch {} + + return '' +} + async function checkSock() { try { await access('/var/run/docker.sock', constants.R_OK) diff --git a/app/utils/headscale.ts b/app/utils/headscale.ts index efea5b1..13d6c7a 100644 --- a/app/utils/headscale.ts +++ b/app/utils/headscale.ts @@ -1,3 +1,5 @@ +import { getContext } from './config' + export class HeadscaleError extends Error { status: number @@ -15,9 +17,9 @@ export class FatalError extends Error { } } -/* eslint-disable @typescript-eslint/no-non-null-assertion */ export async function pull(url: string, key: string) { - const prefix = process.env.HEADSCALE_URL! + const context = await getContext() + const prefix = context.headscaleUrl const response = await fetch(`${prefix}/api/${url}`, { headers: { Authorization: `Bearer ${key}` @@ -32,7 +34,8 @@ export async function pull(url: string, key: string) { } export async function post(url: string, key: string, body?: unknown) { - const prefix = process.env.HEADSCALE_URL! + const context = await getContext() + const prefix = context.headscaleUrl const response = await fetch(`${prefix}/api/${url}`, { method: 'POST', body: body ? JSON.stringify(body) : undefined, @@ -49,7 +52,8 @@ export async function post(url: string, key: string, body?: unknown) { } export async function del(url: string, key: string) { - const prefix = process.env.HEADSCALE_URL! + const context = await getContext() + const prefix = context.headscaleUrl const response = await fetch(`${prefix}/api/${url}`, { method: 'DELETE', headers: {