- 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
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { buildConvexUrl, isLocalHost } from "@otunes/convex/server-url"
|
|
|
|
export type ServerConfig = {
|
|
/** Raw hostname the user entered, e.g. `music.example.com` or `192.168.86.25:3000` */
|
|
hostname: string
|
|
/** Base URL for the Next.js server, e.g. `https://music.example.com` */
|
|
serverUrl: string
|
|
/** Convex deployment URL, e.g. `https://convex.music.example.com` */
|
|
convexUrl: string
|
|
}
|
|
|
|
const SERVER_CONFIG_KEY = "otunes.server-config"
|
|
const LEGACY_SERVER_CONFIG_KEY = "otunes:server-config"
|
|
|
|
function normalizeHostnameInput(input: string): string {
|
|
return input.trim().replace(/^https?:\/\//i, "").replace(/\/+$/, "")
|
|
}
|
|
|
|
function buildServerUrl(hostname: string): string {
|
|
const protocol = isLocalHost(hostname) ? "http" : "https"
|
|
return `${protocol}://${hostname}`
|
|
}
|
|
|
|
export function parseServerHostname(input: string): ServerConfig | null {
|
|
const hostname = normalizeHostnameInput(input)
|
|
if (!hostname) return null
|
|
|
|
try {
|
|
const serverUrl = buildServerUrl(hostname)
|
|
const convexUrl = buildConvexUrl(hostname)
|
|
new URL(serverUrl)
|
|
new URL(convexUrl)
|
|
return { hostname, serverUrl, convexUrl }
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function loadServerConfig(
|
|
getItem: (key: string) => Promise<string | null>
|
|
): Promise<ServerConfig | null> {
|
|
const raw = await getItem(SERVER_CONFIG_KEY)
|
|
if (!raw) return null
|
|
try {
|
|
const parsed = JSON.parse(raw) as ServerConfig
|
|
if (parsed.hostname && parsed.serverUrl && parsed.convexUrl) {
|
|
return parsed
|
|
}
|
|
} catch {
|
|
return null
|
|
}
|
|
return null
|
|
}
|
|
|
|
export async function saveServerConfig(
|
|
setItem: (key: string, value: string) => Promise<void>,
|
|
config: ServerConfig
|
|
): Promise<void> {
|
|
await setItem(SERVER_CONFIG_KEY, JSON.stringify(config))
|
|
}
|
|
|
|
export async function clearServerConfig(
|
|
removeItem: (key: string) => Promise<void>
|
|
): Promise<void> {
|
|
await removeItem(SERVER_CONFIG_KEY)
|
|
}
|
|
|
|
export { SERVER_CONFIG_KEY, LEGACY_SERVER_CONFIG_KEY }
|