Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

8.6 KB
Raw Blame
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";

const boolString = (_default = false) =>
	z
		.string()
		.optional()
		.default(_default ? "true" : "false")
		.transform((v) => v === "true")
		.pipe(z.boolean());

function createServerEnv() {
	return createEnv({
		server: {
			/// General configuration
			DATABASE_URL: z.string().describe("MySQL database URL"),
			WEB_URL: z
				.string()
				.describe("Public URL of the server eg. https://cap.so"),
			NEXTAUTH_SECRET: z.string().describe("32 byte base64 string"),
			NEXTAUTH_URL: z.string().describe("Should be the same as WEB_URL"),
			DATABASE_ENCRYPTION_KEY: z
				.string()
				.optional()
				.refine(
					(key) => !key || [16, 32].includes(Buffer.from(key, "hex").length),
					"Must be 32 or 64 hex characters, e.g. from `openssl rand -hex 32`",
				)
				.describe(
					"32 byte hex string for encrypting values like AWS access keys",
				),

			// Cap uses Resend for email sending, including sending login code emails
			RESEND_API_KEY: z.string().optional(),
			RESEND_FROM_DOMAIN: z.string().optional(),

			/// S3 configuration
			// Though they are prefixed with `CAP_AWS`, these don't have to be
			// for AWS, and can instead be for any S3-compatible service
			CAP_AWS_BUCKET: z.string(),
			CAP_AWS_REGION: z.string(),
			CAP_AWS_ACCESS_KEY: z.string().optional(),
			CAP_AWS_SECRET_KEY: z.string().optional(),
			S3_PUBLIC_ENDPOINT: z
				.string()
				.optional()
				.describe("Public endpoint for accessing S3"),
			S3_INTERNAL_ENDPOINT: z
				.string()
				.optional()
				.describe(
					"Internal endpoint for accessing S3. This is useful if accessing S3 over public internet is more expensive than via your hosting environment's local network.",
				),
			S3_PATH_STYLE: boolString(true).describe(
				"Whether the bucket should be accessed using path-style URLs (common for non-AWS providers, eg. '/{bucket}/{key}') or virtual-hosted-style URLs (eg. '{bucket}.s3.amazonaws.com/{key}').",
			),

			/// CloudFront configuration
			// Configure these if you'd like to serve assets from the default bucket via CloudFront
			// In this case, CAP_AWS_BUCKET_URL should be your CloudFront distribution's URL
			CAP_AWS_BUCKET_URL: z
				.string()
				.optional()
				.describe("Public URL of the S3 bucket"),
			CAP_CLOUDFRONT_DISTRIBUTION_ID: z.string().optional(),
			CLOUDFRONT_KEYPAIR_ID: z.string().optional(),
			CLOUDFRONT_KEYPAIR_PRIVATE_KEY: z.string().optional(),

			/// Google Auth
			// Provide these to allow Google login
			GOOGLE_CLIENT_ID: z.string().optional(),
			GOOGLE_CLIENT_SECRET: z.string().optional(),

			APPLE_CLIENT_ID: z.string().optional(),
			APPLE_CLIENT_SECRET: z.string().optional(),

			/// WorkOS SSO
			// Provide these to use WorkOS for enterprise SSO
			WORKOS_CLIENT_ID: z.string().optional(),
			WORKOS_API_KEY: z.string().optional(),

			/// Generic OIDC
			OIDC_ISSUER: z.string().optional(),
			OIDC_CLIENT_ID: z.string().optional(),
			OIDC_CLIENT_SECRET: z.string().optional(),
			OIDC_NAME: z.string().optional(),

			/// Reverse-proxy (trusted header) auth
			// Sign in via an authenticating reverse proxy (e.g. OpenHost). Set all
			// three values to enable. SECURITY: keep the origin private; the proxy
			// must strip client-sent trust/secret headers and set both only after auth.
			TRUSTED_PROXY_AUTH_HEADER: z
				.string()
				.optional()
				.describe(
					"Header an authenticating reverse proxy sets to 'true' on owner-authenticated requests (e.g. 'x-openhost-is-owner'). Unset disables the provider.",
				),
			TRUSTED_PROXY_AUTH_EMAIL: z
				.string()
				.email()
				.optional()
				.describe(
					"Email of the Cap account to sign in when the trusted header is present.",
				),
			TRUSTED_PROXY_AUTH_SECRET: z
				.string()
				.optional()
				.describe(
					"Required to enable trusted-proxy auth: a non-blank shared secret the proxy must send as 'x-trusted-proxy-secret'. Unset or blank disables the provider. Keep this server-side, never in browser code.",
				),

			/// Settings
			CAP_VIDEOS_DEFAULT_PUBLIC: boolString(true).describe(
				"Should videos be public or private by default",
			),
			CAP_CHROME_EXTENSION_ID: z
				.string()
				.optional()
				.describe("Chrome Web Store extension id allowed to receive auth keys"),
			CAP_ALLOWED_SIGNUP_DOMAINS: z
				.string()
				.optional()
				.describe("Comma-separated list of permitted signup domains"),
			CAP_BLOCKED_SIGNUP_DOMAINS: z
				.string()
				.optional()
				.describe(
					"Comma-separated domains or full email addresses refused at sign-in",
				),

			/// AI providers
			ASSEMBLY_API_KEY: z.string().optional().describe("Audio transcription"),
			ASSEMBLY_BASE_URL: z
				.string()
				.optional()
				.describe(
					"Transcription API base URL (eg. https://api.eu.assemblyai.com)",
				),
			ANTHROPIC_API_KEY: z.string().optional().describe("AI chat"),
			OPENAI_API_KEY: z.string().optional().describe("AI summaries"),
			GROQ_API_KEY: z.string().optional().describe("AI summaries"),
			AI_PROVIDER: z
				.union([
					z.literal("assemblyai"),
					z.literal("openai"),
					z.literal("anthropic"),
					z.literal("groq"),
					z.literal("openai-compatible"),
				])
				.optional()
				.describe(
					"Preferred LLM provider. When unset, providers are auto-detected from the API keys above ('assemblyai' is never auto-detected and must be opted into explicitly).",
				),
			AI_MODEL: z
				.string()
				.optional()
				.describe(
					"Model for AI generation (titles, summaries, chapters, transcript translation). Required for 'openai-compatible'; overrides the provider default otherwise.",
				),
			AI_CHAT_MODEL: z
				.string()
				.optional()
				.describe("Model for AI chat (the support messenger)"),
			AI_STREAM_MODEL: z
				.string()
				.optional()
				.describe("Model for streaming AI chat (docs Ask AI)"),
			AI_BASE_URL: z
				.string()
				.optional()
				.describe(
					"Base URL for the 'openai-compatible' provider (eg. Ollama, OpenRouter, LM Studio), or an alternative AssemblyAI LLM gateway (eg. the EU gateway)",
				),
			AI_API_KEY: z
				.string()
				.optional()
				.describe("API key for the 'openai-compatible' provider"),
			REPLICATE_API_TOKEN: z
				.string()
				.optional()
				.describe("Replicate API token for audio enhancement"),
			SUPERMEMORY_API_KEY: z.string().optional(),
			SUPERMEMORY_KNOWLEDGE_TAG: z.string().optional(),

			/// Cap Cloud
			// These are only needed for Cap Cloud (https://cap.so)
			STRIPE_SECRET_KEY: z.string().optional(),
			STRIPE_SAML_SSO_PRICE_ID: z.string().optional(),
			STRIPE_WEBHOOK_SECRET: z.string().optional(),
			DISCORD_FEEDBACK_WEBHOOK_URL: z.string().optional(),
			DISCORD_LOGS_WEBHOOK_URL: z.string().optional(),
			SLACK_CLIENT_ID: z.string().optional(),
			SLACK_CLIENT_SECRET: z.string().optional(),
			SLACK_SIGNING_SECRET: z.string().optional(),

			/// Tinybird analytics
			TINYBIRD_HOST: z.string().optional(),
			TINYBIRD_TOKEN: z.string().optional(),
			VERCEL_ENV: z
				.union([
					z.literal("production"),
					z.literal("preview"),
					z.literal("development"),
				])
				.optional(),
			VERCEL_TEAM_ID: z.string().optional(),
			VERCEL_PROJECT_ID: z.string().optional(),
			VERCEL_AUTH_TOKEN: z.string().optional(),
			VERCEL_URL_HOST: z.string().optional(),
			VERCEL_BRANCH_URL_HOST: z.string().optional(),
			VERCEL_PROJECT_PRODUCTION_URL_HOST: z.string().optional(),
			VERCEL_AWS_ROLE_ARN: z.string().optional(),
			OPENPANEL_CLIENT_SECRET: z.string().optional(),
			DUB_API_KEY: z.string().optional(),

			/// Media Server
			MEDIA_SERVER_URL: z
				.string()
				.optional()
				.describe("URL of the media server for FFmpeg processing"),
			MEDIA_SERVER_WEBHOOK_SECRET: z
				.string()
				.optional()
				.describe("Secret for authenticating media server webhook callbacks"),
			MEDIA_SERVER_WEBHOOK_URL: z
				.string()
				.optional()
				.describe(
					"Base URL for media server webhooks (use host.docker.internal for Docker setups)",
				),
			LOOM_IMPORTER_URL: z
				.string()
				.optional()
				.describe(
					"URL of the dedicated Loom importer; Loom downloads use the media server when unset",
				),

			/// Ignore
			NODE_ENV: z.string(),
		},
		experimental__runtimeEnv: {
			S3_PUBLIC_ENDPOINT: process.env.CAP_AWS_ENDPOINT,
			S3_INTERNAL_ENDPOINT: process.env.CAP_AWS_ENDPOINT,
			...process.env,
			NODE_ENV: process.env.NODE_ENV ?? "production",
			VERCEL_URL_HOST: process.env.VERCEL_URL,
			VERCEL_BRANCH_URL_HOST: process.env.VERCEL_BRANCH_URL,
			VERCEL_PROJECT_PRODUCTION_URL_HOST:
				process.env.VERCEL_PROJECT_PRODUCTION_URL,
		},
	});
}

let _cached: ReturnType<typeof createServerEnv> | undefined;
export const serverEnv = () => {
	if (_cached) return _cached;
	_cached = createServerEnv();
	return _cached;
};