This commit is contained in:
hailin 2025-06-25 17:33:32 +08:00
parent d71078b262
commit 62b0dbd40b
4 changed files with 130 additions and 52 deletions

View File

@ -1,14 +1,19 @@
//import { getRuntimeEnv } from "@/lib/ipconfig" //import { getRuntimeEnv } from "@/lib/ipconfig"
import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 //import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径
import { getServerRuntimeUrl } from "@/lib/runtime-url"
export async function generateBgeM3Embedding(text: string): Promise<number[] | null> { export async function generateBgeM3Embedding(text: string): Promise<number[] | null> {
try { try {
// 取 Supabase URL 或本地默认 // 取 Supabase URL 或本地默认
const supaUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" // const supaUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000"
// 构造 Embedding 服务地址:同 host + 8001 端口 // // 构造 Embedding 服务地址:同 host + 8001 端口
const urlObj = new URL(supaUrl) // const urlObj = new URL(supaUrl)
urlObj.port = "8001" // 强制改成 8001 // urlObj.port = "8001" // 强制改成 8001
const apiUrl = `${urlObj.origin}/v1/embeddings` // const apiUrl = `${urlObj.origin}/v1/embeddings`
// console.debug("......[generateBgeM3Embedding] apiUrl =", apiUrl)
// ✅ 使用服务端真实 URL自动取协议/host→ 端口强制为 8001
const apiUrl = `${getServerRuntimeUrl("8001")}/v1/embeddings`
console.debug("......[generateBgeM3Embedding] apiUrl =", apiUrl) console.debug("......[generateBgeM3Embedding] apiUrl =", apiUrl)
const response = await fetch(apiUrl, { const response = await fetch(apiUrl, {

View File

@ -1,16 +1,24 @@
// lib/get-runtime-env.ts // lib/get-runtime-env.ts
import { headers } from "next/headers"
export function getRuntimeEnv(key: string): string | undefined { export function getRuntimeEnv(key: string): string | undefined {
/* ---------- 服务器端 (SSR) ---------- */ // 包装成闭包 → 防止构建期静态优化
const getFromEnv = () => process.env[key]
if (typeof window === "undefined") { if (typeof window === "undefined") {
const val = process.env[key] // SSR 逻辑
console.log(`[getRuntimeEnv][SSR] key=${key} -> ${val ?? "undefined"} (process.env)`) if (key === "SUPABASE_URL") {
return val const h = headers()
const proto = h.get("x-forwarded-proto") ?? (h.get("host")?.includes(":443") ? "https" : "http")
const rawHost = h.get("x-forwarded-host") ?? h.get("host")!
const hostname = rawHost.split(",")[0].split(":")[0].trim()
return `${proto}://${hostname}:8000`
}
// 默认 fallback 到 env
return getFromEnv()
} }
/* ---------- 客户端 (CSR) ---------- */ // 客户端
const val = (window as any).RUNTIME_ENV?.[key] return (window as any).RUNTIME_ENV?.[key]
console.log( }
`[getRuntimeEnv][CSR] key=${key} -> ${val ?? "undefined"} (window.RUNTIME_ENV)`
)
return val
}

View File

@ -0,0 +1,14 @@
// lib/runtime-url.ts
import { headers } from "next/headers"
/**
* base url
* 示例: getServerRuntimeUrl("8001") => http://host:8001
*/
export function getServerRuntimeUrl(port = "8000"): string {
const h = headers()
const proto = h.get("x-forwarded-proto") ?? (h.get("host")?.includes(":443") ? "https" : "http")
const rawHost = h.get("x-forwarded-host") ?? h.get("host")!
const hostname = rawHost.split(",")[0].split(":")[0].trim()
return `${proto}://${hostname}:${port}`
}

View File

@ -1,7 +1,84 @@
// import { createServerClient, type CookieOptions } from "@supabase/ssr"
// import { NextResponse, type NextRequest } from "next/server"
// //import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
// import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径
// export const createClient = (request: NextRequest) => {
// // Create an unmodified response
// let response = NextResponse.next({
// request: {
// headers: request.headers
// }
// })
// const supabase = createServerClient(
// getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
// {
// cookies: {
// get(name: string) {
// return request.cookies.get(name)?.value
// },
// set(name: string, value: string, options: CookieOptions) {
// // If the cookie is updated, update the cookies for the request and response
// request.cookies.set({
// name,
// value,
// ...options
// })
// response = NextResponse.next({
// request: {
// headers: request.headers
// }
// })
// response.cookies.set({
// name,
// value,
// ...options
// })
// },
// remove(name: string, options: CookieOptions) {
// // If the cookie is removed, update the cookies for the request and response
// request.cookies.set({
// name,
// value: "",
// ...options
// })
// response = NextResponse.next({
// request: {
// headers: request.headers
// }
// })
// response.cookies.set({
// name,
// value: "",
// ...options
// })
// }
// }
// }
// )
// return { supabase, response }
// }
import { createServerClient, type CookieOptions } from "@supabase/ssr" import { createServerClient, type CookieOptions } from "@supabase/ssr"
import { NextResponse, type NextRequest } from "next/server" import { NextResponse, type NextRequest } from "next/server"
//import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 // ✅ 用 headers 构造 host不再使用 getRuntimeEnv
function getSupabaseUrlFromRequest(req: NextRequest, port = "8000"): string {
const proto =
req.headers.get("x-forwarded-proto") ??
(req.headers.get("host")?.includes(":443") ? "https" : "http")
const rawHost = req.headers.get("x-forwarded-host") ?? req.headers.get("host")!
const hostname = rawHost.split(",")[0].split(":")[0].trim()
return `${proto}://${hostname}:${port}`
}
export const createClient = (request: NextRequest) => { export const createClient = (request: NextRequest) => {
// Create an unmodified response // Create an unmodified response
@ -12,7 +89,7 @@ export const createClient = (request: NextRequest) => {
}) })
const supabase = createServerClient( const supabase = createServerClient(
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", getSupabaseUrlFromRequest(request), // ✅ 这里改了,其他全保留
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ {
cookies: { cookies: {
@ -20,40 +97,14 @@ export const createClient = (request: NextRequest) => {
return request.cookies.get(name)?.value return request.cookies.get(name)?.value
}, },
set(name: string, value: string, options: CookieOptions) { set(name: string, value: string, options: CookieOptions) {
// If the cookie is updated, update the cookies for the request and response request.cookies.set({ name, value, ...options })
request.cookies.set({ response = NextResponse.next({ request: { headers: request.headers } })
name, response.cookies.set({ name, value, ...options })
value,
...options
})
response = NextResponse.next({
request: {
headers: request.headers
}
})
response.cookies.set({
name,
value,
...options
})
}, },
remove(name: string, options: CookieOptions) { remove(name: string, options: CookieOptions) {
// If the cookie is removed, update the cookies for the request and response request.cookies.set({ name, value: "", ...options })
request.cookies.set({ response = NextResponse.next({ request: { headers: request.headers } })
name, response.cookies.set({ name, value: "", ...options })
value: "",
...options
})
response = NextResponse.next({
request: {
headers: request.headers
}
})
response.cookies.set({
name,
value: "",
...options
})
} }
} }
} }