fix: use either HEADSCALE_URL or config.server_url

This commit is contained in:
Aarnav Tale 2024-03-30 19:00:59 -04:00
parent a47fb61549
commit 78140927ad
No known key found for this signature in database
3 changed files with 29 additions and 8 deletions

View File

@ -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) {

View File

@ -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)

View File

@ -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<T>(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<T>(url: string, key: string) {
}
export async function post<T>(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<T>(url: string, key: string, body?: unknown) {
}
export async function del<T>(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: {