From 41c37156af246ba6d07e5e091975722ef3243902 Mon Sep 17 00:00:00 2001 From: my name Date: Thu, 16 Jul 2026 02:04:04 +0200 Subject: [PATCH] now uses formatters instead of hardcoded chain off individual configs added formatter type to associate a service with their templates and filepaths --- .gitignore | 3 ++- src/formatter.ts | 9 +++++---- src/formatters.ts | 24 ++++++++++++++++++++++++ src/index.ts | 21 ++++++++++++++------- src/types.ts | 19 +++++++++++++++++++ src/validator.ts | 2 +- src/writer.ts | 3 +++ 7 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 src/formatters.ts diff --git a/.gitignore b/.gitignore index 8026d5e..a01a672 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,11 @@ .src/config.ts -/config.ts # dependencies (bun install) node_modules # output +output/ +templates/ out dist *.tgz diff --git a/src/formatter.ts b/src/formatter.ts index 45ea767..b43e26d 100644 --- a/src/formatter.ts +++ b/src/formatter.ts @@ -9,11 +9,12 @@ export function formatcaddyfile(proxies: Service[]): string { const proxyconfig = proxies.map(proxy => { const fullDomain = `${proxy.subdomain}.${proxy.host.baseDomain}`; const destination = `${proxy.host.ip}:${proxy.port}` - - return `${fullDomain} { - reverse_proxy ${destination} - }` + const extra = proxy.caddy?.extra ? `\n\t${proxy.caddy?.extra}` : ""; + + return `${fullDomain} {\n\treverse_proxy ${destination} + ${extra}\n}` }).join('\n'); return output + proxyconfig } + diff --git a/src/formatters.ts b/src/formatters.ts new file mode 100644 index 0000000..d2c417f --- /dev/null +++ b/src/formatters.ts @@ -0,0 +1,24 @@ +import { beszelformatter } from "../templates/systemd/beszel-hub.service"; +import { memosformatter } from "../templates/systemd/memos.service"; +import { llcppformatter } from "../templates/systemd/start_llcpp.service"; +import type { Formatter, Service } from "./types"; + +//collection of file templates paired with their corresponding service +export const formatters: Formatter[] = [ + { + serviceid: "memos", + format: (srvc) => memosformatter(srvc), + outputpath: (srvc) => `output/systemd/${srvc.name}.service`, + }, + { + serviceid: "llcpp", + format: (srvc) => llcppformatter(srvc), + outputpath: (srvc) => `output/systemd/${srvc.name}.service`, + }, + { + serviceid: "beszel", + format: (srvc) => beszelformatter(srvc), + outputpath: (srvc) => `output/systemd/${srvc.name}.service`, + } + +] diff --git a/src/index.ts b/src/index.ts index ee35afc..49fcc50 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,10 @@ import { services } from "./config"; import { formatcaddyfile } from "./formatter"; -import type { Service } from "./types"; +import { formatters } from "./formatters"; import { ConfigValidator } from "./validator"; import { ConfigWriter } from "./writer"; console.log("Hello via Bun!"); - -// const vpsservices: Service[] = services.filter(service => service.host.id == "vps-ovh"); -// console.log(vpsservices); - /** entry point / manager **/ @@ -16,12 +12,23 @@ async function main() { try { console.log("starting caddy config"); - ConfigValidator.validate(services); + ConfigValidator.validatecaddyfile(services); const output = formatcaddyfile(services); - await ConfigWriter.writeLocal("Caddyfile", output); + await ConfigWriter.writeLocal("output/etc/caddy/Caddyfile", output); + //check for associated formatters for each provider service definition + for ( const srvc of services ) { + const srvcformatters = formatters.filter(el => el.serviceid == srvc.name); + + for (const formatter of srvcformatters) { + const content = formatter.format(srvc); + const path = formatter.outputpath(srvc); + await ConfigWriter.writeLocal(path, content); + } + } + console.log("completed succesfully"); } catch (error) { console.error("error", error); diff --git a/src/types.ts b/src/types.ts index b35ea89..28bdfd3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,6 +14,8 @@ export interface Service { workingDir?: string; dataDir?: string; host: Host; + caddy?: CaddyConfig; + systemd?: SystemdConfig; } export interface StaticSite { @@ -22,3 +24,20 @@ export interface StaticSite { encode?: string; custom404?: boolean; } + +export interface CaddyConfig { + extra?: string; +} + +export interface SystemdConfig { + env?: Record; +} + +/** +associate a template with a service + **/ +export interface Formatter { + serviceid: string + format: (service: Service) => string; + outputpath: (service: Service) => string; +} diff --git a/src/validator.ts b/src/validator.ts index 785b99b..6f9de3a 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -4,7 +4,7 @@ import type { Service } from "./types"; should validate config, for now just checks for port collisions **/ export class ConfigValidator { - static validate(services: Service[]){ + static validatecaddyfile(services: Service[]){ const hostports = new Map>(); for (const service of services) { diff --git a/src/writer.ts b/src/writer.ts index ac39e78..b0ae898 100644 --- a/src/writer.ts +++ b/src/writer.ts @@ -1,3 +1,6 @@ +/** +uses bun to write string to file + **/ export class ConfigWriter{ static async writeLocal(filename: string, content: string) { await Bun.write(filename, content);