Files
otunes/packages/convex/server-url.ts
T
joshuafhiggins Verified 1cea1fe745 feat(docker): bundle Convex backend with oTunes
- Run the web app and Convex backend in one container with generated per-install keys
- Derive Convex URLs from the server address and update self-hosting docs
2026-10-08 15:24:00 -04:00

48 lines
1.4 KiB
TypeScript

/** Port the self-hosted Convex backend listens on when there is no `convex.` subdomain. */
export const LOCAL_CONVEX_PORT = "3210"
export function parseHostPort(hostname: string): {
host: string
port?: string
} {
const colonIndex = hostname.lastIndexOf(":")
if (colonIndex === -1) {
return { host: hostname }
}
const host = hostname.slice(0, colonIndex)
const port = hostname.slice(colonIndex + 1)
if (!/^\d+$/.test(port)) {
return { host: hostname }
}
return { host, port }
}
export function isLocalHost(hostname: string): boolean {
const host = parseHostPort(hostname).host.toLowerCase()
return (
host === "localhost" ||
host === "127.0.0.1" ||
host.endsWith(".local") ||
/^\d{1,3}(\.\d{1,3}){3}$/.test(host)
)
}
/**
* Convex URL for an oTunes server reached at `hostname` (host with optional port).
* Local hosts use the Convex port directly, everything else uses the
* `convex.` subdomain, e.g. `music.example.com` -> `https://convex.music.example.com`.
*/
export function buildConvexUrl(
hostname: string,
protocol: "http" | "https" = isLocalHost(hostname) ? "http" : "https"
): string {
const { host, port } = parseHostPort(hostname)
if (isLocalHost(hostname)) {
return `${protocol}://${host}:${LOCAL_CONVEX_PORT}`
}
const convexHost = `convex.${host}`
return port
? `${protocol}://${convexHost}:${port}`
: `${protocol}://${convexHost}`
}