fix: use either HEADSCALE_URL or config.server_url
This commit is contained in:
parent
a47fb61549
commit
78140927ad
@ -22,11 +22,11 @@ export const links: LinksFunction = () => [
|
|||||||
]
|
]
|
||||||
|
|
||||||
export async function loader() {
|
export async function loader() {
|
||||||
await getContext()
|
const context = await getContext()
|
||||||
registerConfigWatcher()
|
registerConfigWatcher()
|
||||||
|
|
||||||
if (!process.env.HEADSCALE_URL) {
|
if (context.headscaleUrl.length === 0) {
|
||||||
throw new Error('The HEADSCALE_URL environment variable is required')
|
throw new Error('No headscale URL was provided either by the HEADSCALE_URL environment variable or the config file')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!process.env.COOKIE_SECRET) {
|
if (!process.env.COOKIE_SECRET) {
|
||||||
|
|||||||
@ -161,6 +161,7 @@ type Context = {
|
|||||||
hasConfigWrite: boolean;
|
hasConfigWrite: boolean;
|
||||||
hasAcl: boolean;
|
hasAcl: boolean;
|
||||||
hasAclWrite: boolean;
|
hasAclWrite: boolean;
|
||||||
|
headscaleUrl: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export let context: Context
|
export let context: Context
|
||||||
@ -172,13 +173,29 @@ export async function getContext() {
|
|||||||
hasConfig: await hasConfig(),
|
hasConfig: await hasConfig(),
|
||||||
hasConfigWrite: await hasConfigW(),
|
hasConfigWrite: await hasConfigW(),
|
||||||
hasAcl: await hasAcl(),
|
hasAcl: await hasAcl(),
|
||||||
hasAclWrite: await hasAclW()
|
hasAclWrite: await hasAclW(),
|
||||||
|
headscaleUrl: await getHeadscaleUrl()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return context
|
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() {
|
async function checkSock() {
|
||||||
try {
|
try {
|
||||||
await access('/var/run/docker.sock', constants.R_OK)
|
await access('/var/run/docker.sock', constants.R_OK)
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
import { getContext } from './config'
|
||||||
|
|
||||||
export class HeadscaleError extends Error {
|
export class HeadscaleError extends Error {
|
||||||
status: number
|
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) {
|
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}`, {
|
const response = await fetch(`${prefix}/api/${url}`, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${key}`
|
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) {
|
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}`, {
|
const response = await fetch(`${prefix}/api/${url}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: body ? JSON.stringify(body) : undefined,
|
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) {
|
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}`, {
|
const response = await fetch(`${prefix}/api/${url}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user