Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,8 @@
export type GetCertDto = {
configured: boolean;
records?: {
A?: { name: string; value: string };
AAAA?: { name: string; value: string };
CNAME?: { name: string; value: string };
};
};
@@ -0,0 +1,108 @@
import { loadWebConfig } from "@toolctrl/config";
import { AppConfiguration, getAppConfiguration } from "~/utils/db/appConfiguration.db.server";
import FlyDomainsServer from "./FlyDomains.server";
import { GetCertDto } from "../dtos/GetCertDto";
async function getConfig({ appConfiguration, request }: { appConfiguration?: AppConfiguration; request: Request }) {
if (!appConfiguration) {
appConfiguration = await getAppConfiguration({ request });
}
const domains = appConfiguration.portals?.domains;
if (!domains?.enabled) {
throw new Error("Custom domains are not enabled");
}
switch (domains.provider) {
case "fly": {
if (!loadWebConfig().config.portalServer.flyToken) {
throw new Error("PORTAL_SERVER_FLY_TOKEN is not defined");
}
return domains;
}
default:
throw new Error(`Unknown custom domain provider: ${domains.provider}`);
}
}
async function addCert({ hostname, request }: { hostname: string; request: Request }) {
const domainsConfig = await getConfig({ request });
const flyToken = loadWebConfig().config.portalServer.flyToken ?? "";
switch (domainsConfig.provider) {
case "fly": {
await FlyDomainsServer.addCert({
appId: domainsConfig.portalAppId,
hostname,
apiToken: flyToken,
});
}
}
}
async function getCert({ hostname, request }: { hostname: string | null; request: Request }) {
if (!hostname) {
return null;
}
const domainsConfig = await getConfig({ request });
const flyToken = loadWebConfig().config.portalServer.flyToken ?? "";
switch (domainsConfig.provider) {
case "fly": {
const cer = await FlyDomainsServer.getCert({
appId: domainsConfig.portalAppId,
hostname,
apiToken: flyToken,
}).catch(() => null);
if (!cer) {
return null;
}
const certificate: GetCertDto = {
configured: cer.data.app.certificate.configured,
records: {
A: {
name: getApexDomain(hostname),
value: domainsConfig.records?.A ?? "",
},
AAAA: {
name: getApexDomain(hostname),
value: domainsConfig.records?.AAAA ?? "",
},
CNAME: {
name: cer.data.app.certificate.dnsValidationHostname,
value: cer.data.app.certificate.dnsValidationTarget,
},
},
};
return certificate;
}
}
return null;
}
function getApexDomain(hostname: string) {
// if root domain, return @ otherwise remove the root domain
return hostname === getRootDomain(hostname) ? "@" : hostname.replace("." + getRootDomain(hostname), "");
}
function getRootDomain(hostname: string) {
const parts = hostname.split(".");
return parts.slice(-2).join(".");
}
async function delCert({ hostname, request }: { hostname: string; request: Request }) {
const domainsConfig = await getConfig({ request });
const flyToken = loadWebConfig().config.portalServer.flyToken ?? "";
switch (domainsConfig.provider) {
case "fly": {
return FlyDomainsServer.delCert({
appId: domainsConfig.portalAppId,
hostname,
apiToken: flyToken,
});
}
}
}
export default {
getConfig,
addCert,
getCert,
delCert,
};
@@ -0,0 +1,193 @@
import { createScopedLogger } from "~/utils/logger.server";
const flyDomainsLogger = createScopedLogger("domains.fly");
const endpoint = "https://api.fly.io/graphql";
async function addCert({ appId, hostname, apiToken }: { appId: string; hostname: string; apiToken: string }) {
flyDomainsLogger.info({ appId, hostname }, "FlyApi.addCert");
const query = `
mutation($appId: ID!, $hostname: String!) {
addCertificate(appId: $appId, hostname: $hostname) {
certificate {
configured
acmeDnsConfigured
acmeAlpnConfigured
certificateAuthority
certificateRequestedAt
dnsProvider
dnsValidationInstructions
dnsValidationHostname
dnsValidationTarget
hostname
id
source
}
}
}`;
const variables = { appId, hostname };
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiToken}`,
},
body: JSON.stringify({ query, variables }),
});
const data = await response.json();
flyDomainsLogger.debug({ data }, "FlyApi.addCert response");
return data;
}
export type FlyGetCertDto = {
data: {
app: {
certificate: {
configured: boolean;
acmeDnsConfigured: boolean;
acmeAlpnConfigured: boolean;
certificateAuthority: string;
createdAt: string;
dnsProvider: string;
dnsValidationInstructions: string;
dnsValidationHostname: string;
dnsValidationTarget: string;
hostname: string;
id: string;
source: string;
clientStatus: string;
issued: {
nodes: Array<any>;
};
};
};
};
};
async function getCert({ appId, hostname, apiToken }: { appId: string; hostname: string; apiToken: string }): Promise<FlyGetCertDto> {
const query = `
query($appName: String!, $hostname: String!) {
app(name: $appName) {
certificate(hostname: $hostname) {
configured
acmeDnsConfigured
acmeAlpnConfigured
certificateAuthority
createdAt
dnsProvider
dnsValidationInstructions
dnsValidationHostname
dnsValidationTarget
hostname
id
source
clientStatus
issued {
nodes {
type
expiresAt
}
}
}
}
}`;
const variables = { appName: appId, hostname };
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiToken}`,
},
body: JSON.stringify({ query, variables }),
});
const data = await response.json();
if (data.errors && data.errors.length) {
throw new Error(data.errors[0].message);
}
return data;
}
// async function checkCert({ appId, hostname, apiToken }: { appId: string; hostname: string; apiToken: string }) {
// const query = `
// query($appName: String!, $hostname: String!) {
// app(name: $appName) {
// certificate(hostname: $hostname) {
// check
// configured
// acmeDnsConfigured
// acmeAlpnConfigured
// certificateAuthority
// createdAt
// dnsProvider
// dnsValidationInstructions
// dnsValidationHostname
// dnsValidationTarget
// hostname
// id
// source
// clientStatus
// issued {
// nodes {
// type
// expiresAt
// }
// }
// }
// }
// }`;
// const variables = { appName: appId, hostname };
// const response = await fetch(endpoint, {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// Authorization: `Bearer ${apiToken}`,
// },
// body: JSON.stringify({ query, variables }),
// });
// if (response.status !== 200) {
// return null;
// }
// const data = await response.json();
// return data;
// }
async function delCert({ appId, hostname, apiToken }: { appId: string; hostname: string; apiToken: string }) {
const query = `
mutation($appId: ID!, $hostname: String!) {
deleteCertificate(appId: $appId, hostname: $hostname) {
app {
name
}
certificate {
hostname
id
}
}
}
`;
const variables = { appId: appId, hostname: hostname };
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiToken}`,
},
body: JSON.stringify({ query, variables }),
});
const data = await response.json();
flyDomainsLogger.debug({ data }, "FlyApi.delCert response");
return data;
}
export default {
addCert,
getCert,
// checkCert,
delCert,
};