flnxi-Wu/lib/config.ts
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

65 lines
2.4 KiB
TypeScript

import type { NextRequest } from 'next/server'
// 尝试加载根目录的可移植部署变量文件(不会覆盖已存在的真实环境变量)
try {
// 动态引入,避免打包期间出错
const { loadPortableEnvIfPresent } = require('./configLoader')
if (typeof loadPortableEnvIfPresent === 'function') {
loadPortableEnvIfPresent()
}
} catch {}
export function resolveBaseUrl(req?: NextRequest): string {
const appPublicUrl = process.env.APP_PUBLIC_URL
const nextAuthUrl = process.env.NEXTAUTH_URL
const isLocal = (url?: string | null) => !!url && /localhost|127\.0\.0\.1/i.test(url)
if (appPublicUrl && !isLocal(appPublicUrl)) return appPublicUrl
if (req) {
const forwardedProto = req.headers.get('x-forwarded-proto')
const forwardedHost = req.headers.get('x-forwarded-host')
const originHeader = req.headers.get('origin')
const host = req.headers.get('host')
const headerBase =
(forwardedHost && `${forwardedProto || 'http'}://${forwardedHost}`) ||
(originHeader && !isLocal(originHeader) ? originHeader : null) ||
(host ? `http://${host}` : null)
if (headerBase) return headerBase
}
if (nextAuthUrl && !isLocal(nextAuthUrl)) return nextAuthUrl
return 'http://localhost:3000'
}
export const defaultLogos = {
header: process.env.DEFAULT_HEADER_LOGO_URL || '',
login: process.env.DEFAULT_LOGIN_LOGO_URL || '',
}
export const publicDefaults = {
header: process.env.NEXT_PUBLIC_DEFAULT_HEADER_LOGO_URL || '',
login: process.env.NEXT_PUBLIC_DEFAULT_LOGIN_LOGO_URL || '',
intranetUrl: process.env.NEXT_PUBLIC_INTRANET_URL || '',
}
export function buildAbsoluteUrl(path: string, req?: NextRequest): string {
const base = resolveBaseUrl(req)
// 确保 base 不为空,避免返回 "http://"
if (!base || base === 'http://' || base === 'https://') {
return path.startsWith('http://') || path.startsWith('https://')
? path
: `http://localhost:3000${path.startsWith('/') ? path : `/${path}`}`
}
if (!path) return base
if (path.startsWith('http://') || path.startsWith('https://')) return path
return `${base}${path.startsWith('/') ? path : `/${path}`}`
}
// Feature flags (can be overridden via SystemSettings in future)
export const featureFlags = {
aiEditEnabled: (process.env.AI_EDIT_ENABLED || 'false').toLowerCase() === 'true',
tunnelEnabled: (process.env.TUNNEL_ENABLED || 'false').toLowerCase() === 'true',
}