mirror of
https://github.com/tale/headplane.git
synced 2026-08-28 07:57:03 +00:00
feat: watch config file for external edits
This commit is contained in:
+2
-1
@@ -10,7 +10,7 @@ import {
|
|||||||
import { ErrorPopup } from '~/components/Error'
|
import { ErrorPopup } from '~/components/Error'
|
||||||
import Toaster from '~/components/Toaster'
|
import Toaster from '~/components/Toaster'
|
||||||
import stylesheet from '~/tailwind.css?url'
|
import stylesheet from '~/tailwind.css?url'
|
||||||
import { getContext } from '~/utils/config'
|
import { getContext, registerConfigWatcher } from '~/utils/config'
|
||||||
|
|
||||||
export const meta: MetaFunction = () => [
|
export const meta: MetaFunction = () => [
|
||||||
{ title: 'Headplane' },
|
{ title: 'Headplane' },
|
||||||
@@ -23,6 +23,7 @@ export const links: LinksFunction = () => [
|
|||||||
|
|
||||||
export async function loader() {
|
export async function loader() {
|
||||||
await getContext()
|
await getContext()
|
||||||
|
registerConfigWatcher()
|
||||||
|
|
||||||
if (!process.env.HEADSCALE_URL) {
|
if (!process.env.HEADSCALE_URL) {
|
||||||
throw new Error('The HEADSCALE_URL environment variable is required')
|
throw new Error('The HEADSCALE_URL environment variable is required')
|
||||||
|
|||||||
+17
-3
@@ -1,6 +1,7 @@
|
|||||||
import { access, constants, readFile, stat, writeFile } from 'node:fs/promises'
|
import { access, constants, readFile, writeFile } from 'node:fs/promises'
|
||||||
import { resolve } from 'node:path'
|
import { resolve } from 'node:path'
|
||||||
|
|
||||||
|
import { type FSWatcher, watch } from 'chokidar'
|
||||||
import { type Document, parseDocument } from 'yaml'
|
import { type Document, parseDocument } from 'yaml'
|
||||||
|
|
||||||
type Duration = `${string}s` | `${string}h` | `${string}m` | `${string}d` | `${string}y`
|
type Duration = `${string}s` | `${string}h` | `${string}m` | `${string}d` | `${string}y`
|
||||||
@@ -120,8 +121,8 @@ type Config = {
|
|||||||
|
|
||||||
let config: Document
|
let config: Document
|
||||||
|
|
||||||
export async function getConfig() {
|
export async function getConfig(force = false) {
|
||||||
if (!config) {
|
if (!config || force) {
|
||||||
const path = resolve(process.env.CONFIG_FILE ?? '/etc/headscale/config.yaml')
|
const path = resolve(process.env.CONFIG_FILE ?? '/etc/headscale/config.yaml')
|
||||||
const data = await readFile(path, 'utf8')
|
const data = await readFile(path, 'utf8')
|
||||||
config = parseDocument(data)
|
config = parseDocument(data)
|
||||||
@@ -140,6 +141,19 @@ export async function patchConfig(partial: Record<string, unknown>) {
|
|||||||
await writeFile(path, config.toString(), 'utf8')
|
await writeFile(path, config.toString(), 'utf8')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let watcher: FSWatcher
|
||||||
|
|
||||||
|
export function registerConfigWatcher() {
|
||||||
|
if (watcher) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = resolve(process.env.CONFIG_FILE ?? '/etc/headscale/config.yaml')
|
||||||
|
watcher = watch(path).on('change', async () => {
|
||||||
|
await getConfig(true)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
hasDockerSock: boolean;
|
hasDockerSock: boolean;
|
||||||
hasConfig: boolean;
|
hasConfig: boolean;
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
"@remix-run/node": "^2.8.1",
|
"@remix-run/node": "^2.8.1",
|
||||||
"@remix-run/react": "^2.8.1",
|
"@remix-run/react": "^2.8.1",
|
||||||
"@remix-run/serve": "^2.8.1",
|
"@remix-run/serve": "^2.8.1",
|
||||||
|
"chokidar": "^3.6.0",
|
||||||
"clsx": "^2.1.0",
|
"clsx": "^2.1.0",
|
||||||
"isbot": "^4.1.0",
|
"isbot": "^4.1.0",
|
||||||
"oauth4webapi": "^2.10.3",
|
"oauth4webapi": "^2.10.3",
|
||||||
|
|||||||
Generated
+3
@@ -32,6 +32,9 @@ dependencies:
|
|||||||
'@remix-run/serve':
|
'@remix-run/serve':
|
||||||
specifier: ^2.8.1
|
specifier: ^2.8.1
|
||||||
version: 2.8.1(typescript@5.4.3)
|
version: 2.8.1(typescript@5.4.3)
|
||||||
|
chokidar:
|
||||||
|
specifier: ^3.6.0
|
||||||
|
version: 3.6.0
|
||||||
clsx:
|
clsx:
|
||||||
specifier: ^2.1.0
|
specifier: ^2.1.0
|
||||||
version: 2.1.0
|
version: 2.1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user