63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import adminApiGuard from "@/src/functions/backend/auth/admin-api-guard";
|
|
import syncCaddyIngress from "@/src/functions/backend/caddy/sync-caddy-ingress";
|
|
import runSetupScript from "@/src/functions/backend/setup/run-setup-script";
|
|
import { TlsData } from "@/src/data/tls-data";
|
|
import grabDirNames from "@/src/utils/grab-dir-names";
|
|
import type {
|
|
APIResponseObject,
|
|
BunextAPIRouteHandler,
|
|
} from "@moduletrace/bunext/types";
|
|
|
|
const INSTALL_CADDY_SCRIPT = `install-caddy.sh`;
|
|
const INSTALL_CADDY_TIMEOUT_MS = 20 * 60 * 1000;
|
|
|
|
const {
|
|
WGUI_LIB_DIR,
|
|
WGUI_CADDY_BIN_DIR,
|
|
WGUI_ACME_HOME,
|
|
WGUI_ACME_ENV_FILE,
|
|
} = grabDirNames();
|
|
|
|
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
|
|
req,
|
|
}) => {
|
|
if (req.method !== "POST") {
|
|
return { success: false };
|
|
}
|
|
|
|
const unauthorized = await adminApiGuard({ req });
|
|
|
|
if (unauthorized) {
|
|
return unauthorized;
|
|
}
|
|
|
|
const install = runSetupScript({
|
|
script: INSTALL_CADDY_SCRIPT,
|
|
timeout_ms: INSTALL_CADDY_TIMEOUT_MS,
|
|
env: {
|
|
WGUI_LIB_DIR,
|
|
CADDY_VERSION: process.env.CADDY_VERSION,
|
|
CADDY_BIN_DIR: WGUI_CADDY_BIN_DIR,
|
|
CADDY_HTTPS_PORT: String(TlsData.CaddyHttpsPort),
|
|
CADDY_HTTP_PORT: String(TlsData.CaddyHttpPort),
|
|
ACME_HOME: WGUI_ACME_HOME,
|
|
ACME_ENV_FILE: WGUI_ACME_ENV_FILE,
|
|
},
|
|
});
|
|
|
|
if (!install.success) {
|
|
return {
|
|
success: false,
|
|
msg: install.output,
|
|
};
|
|
}
|
|
|
|
const sync = await syncCaddyIngress({ reason: "caddy installed" });
|
|
|
|
return {
|
|
success: sync.success,
|
|
msg: [install.output, sync.msg].filter(Boolean).join("\n\n"),
|
|
debug: { warnings: sync.warnings },
|
|
};
|
|
};
|